# is-close

> Check if two numbers are equal within a given tolerance

Latest version **1.3.3** (published 2018-03-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install is-close
pnpm add is-close
yarn add is-close
bun add is-close
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.3.3 |
| Published | 2018-03-20 |
| First published | 2018-03-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 32 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Steve Bosman |
| Maintainers | stevebosman |
| Keywords | closeness, check, test |

## Links

- npm: https://www.npmjs.com/package/is-close
- Repository: https://github.com/stevebosman/is-close
- Homepage: https://github.com/stevebosman/is-close#readme
- Issues: https://github.com/stevebosman/is-close/issues
- npm.io page: https://npm.io/package/is-close

## Alternatives

- [duck](https://npm.io/package/duck.md) — 4.2M weekly downloads
- [ava](https://npm.io/package/ava.md) — 560.2K weekly downloads
- [storybook-addon-module-mock](https://npm.io/package/storybook-addon-module-mock.md) — 71.7K weekly downloads
- [@ethereum-waffle/mock-contract](https://npm.io/package/@ethereum-waffle/mock-contract.md) — 40.0K weekly downloads
- [aws-elasticsearch-connector](https://npm.io/package/aws-elasticsearch-connector.md) — 37.4K weekly downloads

## Recent versions

- 1.3.3 (latest) — 2018-03-20
- 1.3.2 — 2018-03-20
- 1.3.1 — 2018-03-19
- 1.3.0 — 2018-03-19
- 1.2.1 — 2018-03-15
- 1.2.0 — 2018-03-15
- 1.1.1 — 2018-03-15
- 1.1.0 — 2018-03-15
- 1.0.4 — 2018-03-15
- 1.0.3 — 2018-03-15
- 1.0.2 — 2018-03-15
- 1.0.1 — 2018-03-14
- 1.0.0 — 2018-03-14

## README

# is-close

> Check if either: 
> 1. two numbers are equal within a given tolerance
> 2. the values in two arrays of numbers are equal within a given tolerance

## `isClose(a, b, rtol, atol, equalNaN, isCloseScaler) ⇒ boolean`&#124;`Array<boolean>`
Check if `a` is approximately equal to `b`.

This check uses similar logic to [Python math.isClose](https://docs.python.org/3/library/math.html#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<br/>[default = 1e-09] |
| `atol`           | `number [optional]` | The absolute tolerance parameter<br/>[default = 0.0] |
| `equalNaN`       | `boolean [optional]` | Whether to compare NaNs as equal.<br/>If true, when `a` is NaN and `b` is NaN they will be considered equal.<br/>[default = false] |
| `isCloseScaler`  | `IsCloseScaler [optional]` | Method used to scale the absolute difference into a relative difference.<br/>[default = IsCloseScalers.maxAbsAOrB] |

### Overload
**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<br/>[default = 1e-09] |
| `atol`           | `number [optional]` | The absolute tolerance parameter<br/>[default = 0.0] |
| `equalNaN`       | `boolean [optional]` | Whether to compare NaNs as equal.<br/>[default = false] |
| `isCloseScaler`  | `IsCloseScaler [optional]` | Method used to scale the absolute difference into a relative difference.<br/>[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
```

## License

MIT © [Steve Bosman](http://github.com/stevebosman)

---
_Source: https://npm.io/package/is-close · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
