Skip to content

Commit 93409cd

Browse files
authored
fix floating point precision in closeTo assertion (#1667)
1 parent 25bcaab commit 93409cd

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

lib/chai/core/assertions.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -3150,8 +3150,12 @@ function closeTo(expected, delta, msg) {
31503150

31513151
const abs = (x) => (x < 0n ? -x : x);
31523152

3153+
// Used to round floating point number precision arithmetics
3154+
// See: https://stackoverflow.com/a/3644302
3155+
const strip = (number) => parseFloat(parseFloat(number).toPrecision(12));
3156+
31533157
this.assert(
3154-
abs(obj - expected) <= delta,
3158+
strip(abs(obj - expected)) <= delta,
31553159
'expected #{this} to be close to ' + expected + ' +/- ' + delta,
31563160
'expected #{this} not to be close to ' + expected + ' +/- ' + delta
31573161
);

test/assert.js

+4
Original file line numberDiff line numberDiff line change
@@ -1894,6 +1894,8 @@ describe('assert', function () {
18941894
assert.closeTo(10, 20, 20);
18951895
assert.closeTo(-10, 20, 30);
18961896
assert.closeTo(10, 10, 0);
1897+
assert.closeTo(1682.6, 1682.7, 0.1);
1898+
assert.closeTo(1n, 2n, 1n);
18971899

18981900
err(function(){
18991901
assert.closeTo(2, 1.0, 0.5, 'blah');
@@ -1928,6 +1930,8 @@ describe('assert', function () {
19281930
assert.approximately(1.5, 1.0, 0.5);
19291931
assert.approximately(10, 20, 20);
19301932
assert.approximately(-10, 20, 30);
1933+
assert.approximately(10, 10, 0);
1934+
assert.approximately(1682.6, 1682.7, 0.1);
19311935
assert.approximately(1n, 2n, 1n);
19321936

19331937
err(function(){

test/expect.js

+6
Original file line numberDiff line numberDiff line change
@@ -3267,6 +3267,9 @@ describe('expect', function () {
32673267
expect(1.5).to.be.closeTo(1.0, 0.5);
32683268
expect(10).to.be.closeTo(20, 20);
32693269
expect(-10).to.be.closeTo(20, 30);
3270+
expect(10).to.be.closeTo(10, 0);
3271+
expect(1682.6).to.be.closeTo(1682.7, 0.1);
3272+
expect(1n).to.be.closeTo(2n, 1n);
32703273

32713274
err(function(){
32723275
expect(2).to.be.closeTo(1.0, 0.5, 'blah');
@@ -3313,6 +3316,9 @@ describe('expect', function () {
33133316
expect(1.5).to.be.approximately(1.0, 0.5);
33143317
expect(10).to.be.approximately(20, 20);
33153318
expect(-10).to.be.approximately(20, 30);
3319+
expect(10).to.be.approximately(10, 0);
3320+
expect(1682.6).to.be.approximately(1682.7, 0.1);
3321+
expect(1n).to.be.approximately(2n, 1n);
33163322

33173323
err(function(){
33183324
expect(2).to.be.approximately(1.0, 0.5, 'blah');

test/should.js

+10
Original file line numberDiff line numberDiff line change
@@ -2749,6 +2749,11 @@ describe('should', function() {
27492749

27502750
it('closeTo', function(){
27512751
(1.5).should.be.closeTo(1.0, 0.5);
2752+
(10).should.be.closeTo(20, 20);
2753+
(-10).should.be.closeTo(20, 30);
2754+
(10).should.be.closeTo(10, 0);
2755+
(1682.6).should.be.closeTo(1682.7, 0.1);
2756+
(1n).should.be.closeTo(2n, 1n);
27522757

27532758
err(function(){
27542759
(2).should.be.closeTo(1.0, 0.5, 'blah');
@@ -2773,6 +2778,11 @@ describe('should', function() {
27732778

27742779
it('approximately', function(){
27752780
(1.5).should.be.approximately(1.0, 0.5);
2781+
(10).should.be.approximately(20, 20);
2782+
(-10).should.be.approximately(20, 30);
2783+
(10).should.be.approximately(10, 0);
2784+
(1682.6).should.be.approximately(1682.7, 0.1);
2785+
(1n).should.be.approximately(2n, 1n);
27762786

27772787
err(function(){
27782788
(2).should.be.approximately(1.0, 0.5, 'blah');

0 commit comments

Comments
 (0)