# is-close

Check if two numbers are equal within a given tolerance

## Stats

StarsIssuesVersionUpdatedCreatedSize
is-close
1.3.36 years ago6 years ago

is-close
Check if either:
1. two numbers are equal within a given tolerance
1. the values in two arrays of numbers are equal within a given tolerance

## `isClose(a, b, rtol, atol, equalNaN, isCloseScaler) ⇒ boolean`|`Array<boolean>`

Check if `a` is approximately equal to `b`. This check uses similar logic to Python math.isClose. By default values are considered to be close if: ``` abs(a - b) <= max(rtol max(abs(a), abs(b)), atol). ```
Returns: `boolean` - Returns true if both `a` and `b` are considered to be close. | Parameter | Type | Description | | ---------------- | --- | --- | | `a` | `number` | First value to compare | | `b` | `number` | Second value to compare | | `rtol` | `number [optional]` | The relative tolerance parameter
default = 1e-09
| | `atol` | `number [optional]` | The absolute tolerance parameter
default = 0.0 | | `equalNaN` | `boolean [optional]` | Whether to compare NaNs as equal.
If true, when `a` is NaN and `b` is NaN they will be considered equal.
default = false | | `isCloseScaler` | `IsCloseScaler [optional]` | Method used to scale the absolute difference into a relative difference.
default = IsCloseScalers.maxAbsAOrB |

Returns: `Array<boolean>` - Returns an array of booleans corresponding to the result of calling isClose() on the individual values in the arrays `a` and `b`. | Parameter | Type | Description | | ---------------- | --- | --- | | `a` | `Array<number>` | First value to compare | | `b` | `Array<number>` | Second value to compare | | `rtol` | `number [optional]` | The relative tolerance parameter
default = 1e-09 | | `atol` | `number [optional]` | The absolute tolerance parameter
default = 0.0 | | `equalNaN` | `boolean [optional]` | Whether to compare NaNs as equal.
default = false | | `isCloseScaler` | `IsCloseScaler [optional]` | Method used to scale the absolute difference into a relative difference.
default = IsCloseScalers.maxAbsAOrB |

## `IsCloseScalers#maxAbsAOrB(a, b) => number`

Returns: `number` - Relative difference scaling: Max(|a|,|b|)

## `IsCloseScalers#maxAOrB(a, b) => number`

Returns: `number` - Relative difference scaling: Max(a,b)

## `IsCloseScalers#minAbsAOrB(a, b) => number`

Returns: `number` - Relative difference scaling: Min(|a|,|b|)

## `IsCloseScalers#minAOrB(a, b) => number`

Returns: `number` - Relative difference scaling: Min(a,b)

## `IsCloseScalers#meanAbs(a, b) => number`

Returns: `number` - Relative difference scaling: Mean: (|a|+|b|)/2, using Welford's method

## `IsCloseScalers#mean(a, b) => number`

Returns: `number` - Relative difference scaling: Mean: (a+b)/2, using Welford's method

## `IsCloseScalers#absA(a) => number`

Returns: `number` - Relative error scaling: abs(a)

## `IsCloseScalers#absB(a, b) => number`

Returns: `number` - Relative error scaling: abs(b)

## Installation

```sh npm install is-close --save ```

## Example Usage

```javascript var isClose = require("is-close"); // Examples of checking relative difference between first two parameters console.log(isClose.isClose(1e-7, 1e-8)); console.log(isClose.isClose(1e10, 1.0000000001e10)); console.log(isClose.isClose(1e-7, 1e10, 1e-8, 1.0000000001e10)); // Examples of relative error relative to first parameter // - note how result is not symmetric console.log(isClose.isClose(1.0, 0.9, 0.1, undefined, undefined, isClose.IsCloseScalers.absA)); console.log(isClose.isClose(0.9, 1.0, 0.1, undefined, undefined, isClose.IsCloseScalers.absA)); ``` Output should be ``` false true false, true true false ```

### TypeScript

```typescript import { isClose, IsCloseScalers } from 'is-close'; // Examples of checking relative difference between first two parameters console.log(isClose(1.0, 1.000000001)); console.log(isClose(1.0, 1.0000000001)); console.log(isClose(1.0, 1.0, 1.000000001, 1.0000000001)); // Examples of relative error relative to first parameter // - note how result is not symmetric console.log(isClose(1.0, 0.9, 0.1, undefined, undefined, IsCloseScalers.absA)); console.log(isClose(0.9, 1.0, 0.1, undefined, undefined, IsCloseScalers.absA)); ``` Output should be ``` false true false, true true false ```