Skip to content

@technobuddha > library > Math > Comparison

Function: approximatelyEquals()

ts
function approximatelyEquals(
   a: number, 
   b: number, 
   options: ApproximatelyEqualsOptions): boolean;

Defined in: approximately-equals.ts:30

Tests whether the two values are equal to each other, within a certain tolerance, taking into account floating point errors (numbers within EPSILON).

Parameters

ParameterTypeDescription
anumberFirst number to compare.
bnumberSecond number to compare.
optionsApproximatelyEqualsOptionssee ApproximatelyEqualsOptions

Returns

boolean

true if a and b are nearly equal.

Default Value

ts
tolerance 0

Example

typescript
approximatelyEquals(0.1 + 0.2, 0.3); // true (floating point rounding)
approximatelyEquals(100, 100.0000001); // true
approximatelyEquals(100, 100.1); // false
approximatelyEquals(5, 7, { tolerance: 2 }); // true
approximatelyEquals(5, 8, { tolerance: 2 }); // false