Approximate floating point equality comparisons for the Swift Programming Language.


Approximate is a Swift package that provides implementations of floating point comparisons for the Swift ecosystem. It is generally not correct behavior to compare two floating point numbers for equality or inequality using exact equality comparisons, due to the imprecise nature of floating point arithmetic. In its place one uses approximate comparisons instead. Floating point numbers are counterintuitive creatures, so one needs to approach them differently than integers.


To use Approximate in your projects, add the following line to the dependencies list in your swift package manifest

dependencies: [
    .package(url: "", .branch("master")),

and the following line to each of your desired targets

.product(name: "Approximate", package: "Approximate")

For example, your Swift package manifest may look like

// swift-tools-version:5.6
import PackageDescription

let package = Package(
    name: "my-app",
    dependencies: [
        .package(url: "", from: "1.0.1"),
    targets: [
        .target(name: "my-app", dependencies: [
            .product(name: "Approximate", package: "Approximate"),

Finally import the library

import Approximate

and use it in your project.


This library provides the three standard forms of approximate equality comparisons for floating point numbers in Swift. These are absolute difference equality, units in last place (ULPS) equality, and relative equality.


Some references going in depth about comparing floating point numbers and their tricky subtleties.


View Github