@tempots/std
nearEquals() function
number numbers can sometime introduce tiny errors even for simple operations. nearEquals
compares two floats using a tiny tollerance (last optional argument). By default it is defined as EPSILON
.
Signature:
nearEquals: (a: number, b: number, tollerance?: number) => boolean
Parameters
Parameter |
Type |
Description |
---|---|---|
a |
number |
The first number to compare. |
b |
number |
The second number to compare. |
tollerance |
number |
(Optional) The tollerance value. Default is |
Returns: boolean
true
if the numbers are very close, false
otherwise.
Example
nearEquals(5, 5.000000000000001) // returns true
nearEquals(5, 5.000000000001) // returns false
nearEquals(5, 5.000000000001, 1e-9) // returns true