Skip to content

Commit

Permalink
fix floating point precision in closeTo assertion (#1667)
Browse files Browse the repository at this point in the history
  • Loading branch information
koddsson authored Feb 3, 2025
1 parent 25bcaab commit 93409cd
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/chai/core/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3150,8 +3150,12 @@ function closeTo(expected, delta, msg) {

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

// Used to round floating point number precision arithmetics
// See: https://stackoverflow.com/a/3644302
const strip = (number) => parseFloat(parseFloat(number).toPrecision(12));

this.assert(
abs(obj - expected) <= delta,
strip(abs(obj - expected)) <= delta,
'expected #{this} to be close to ' + expected + ' +/- ' + delta,
'expected #{this} not to be close to ' + expected + ' +/- ' + delta
);
Expand Down
4 changes: 4 additions & 0 deletions test/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -1894,6 +1894,8 @@ describe('assert', function () {
assert.closeTo(10, 20, 20);
assert.closeTo(-10, 20, 30);
assert.closeTo(10, 10, 0);
assert.closeTo(1682.6, 1682.7, 0.1);
assert.closeTo(1n, 2n, 1n);

err(function(){
assert.closeTo(2, 1.0, 0.5, 'blah');
Expand Down Expand Up @@ -1928,6 +1930,8 @@ describe('assert', function () {
assert.approximately(1.5, 1.0, 0.5);
assert.approximately(10, 20, 20);
assert.approximately(-10, 20, 30);
assert.approximately(10, 10, 0);
assert.approximately(1682.6, 1682.7, 0.1);
assert.approximately(1n, 2n, 1n);

err(function(){
Expand Down
6 changes: 6 additions & 0 deletions test/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -3267,6 +3267,9 @@ describe('expect', function () {
expect(1.5).to.be.closeTo(1.0, 0.5);
expect(10).to.be.closeTo(20, 20);
expect(-10).to.be.closeTo(20, 30);
expect(10).to.be.closeTo(10, 0);
expect(1682.6).to.be.closeTo(1682.7, 0.1);
expect(1n).to.be.closeTo(2n, 1n);

err(function(){
expect(2).to.be.closeTo(1.0, 0.5, 'blah');
Expand Down Expand Up @@ -3313,6 +3316,9 @@ describe('expect', function () {
expect(1.5).to.be.approximately(1.0, 0.5);
expect(10).to.be.approximately(20, 20);
expect(-10).to.be.approximately(20, 30);
expect(10).to.be.approximately(10, 0);
expect(1682.6).to.be.approximately(1682.7, 0.1);
expect(1n).to.be.approximately(2n, 1n);

err(function(){
expect(2).to.be.approximately(1.0, 0.5, 'blah');
Expand Down
10 changes: 10 additions & 0 deletions test/should.js
Original file line number Diff line number Diff line change
Expand Up @@ -2749,6 +2749,11 @@ describe('should', function() {

it('closeTo', function(){
(1.5).should.be.closeTo(1.0, 0.5);
(10).should.be.closeTo(20, 20);
(-10).should.be.closeTo(20, 30);
(10).should.be.closeTo(10, 0);
(1682.6).should.be.closeTo(1682.7, 0.1);
(1n).should.be.closeTo(2n, 1n);

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

it('approximately', function(){
(1.5).should.be.approximately(1.0, 0.5);
(10).should.be.approximately(20, 20);
(-10).should.be.approximately(20, 30);
(10).should.be.approximately(10, 0);
(1682.6).should.be.approximately(1682.7, 0.1);
(1n).should.be.approximately(2n, 1n);

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

0 comments on commit 93409cd

Please sign in to comment.