-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
28 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { approxEq } from './approx-eq'; | ||
|
||
describe('approxEq', () => { | ||
it('returns true for numbers within the default threshold', () => { | ||
expect(approxEq(1.0000001, 1.0000002)).toBe(true); | ||
}); | ||
|
||
it('returns false for numbers outside the default threshold', () => { | ||
expect(approxEq(1.000001, 1.00001)).toBe(false); | ||
}); | ||
|
||
it('returns true for numbers within a custom threshold', () => { | ||
expect(approxEq(1.0001, 1.0002, 0.001)).toBe(true); | ||
}); | ||
|
||
it('returns false for numbers outside a custom threshold', () => { | ||
expect(approxEq(1.0001, 1.0002, 0.00001)).toBe(false); | ||
}); | ||
|
||
it('returns true for identical numbers', () => { | ||
expect(approxEq(1, 1)).toBe(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/** Float equals check with threshold */ | ||
export function approxEq(A: number, B: number, threshold = 1e-6): boolean { | ||
return Math.abs(A - B) < threshold; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters