From c0c57984d9f7f38f90257e7ac2780894dadbd9ad Mon Sep 17 00:00:00 2001 From: Mike Dirolf Date: Mon, 22 Mar 2010 12:07:44 -0400 Subject: assert.close just uses difference now (can specify decimal place) - old implementation was brittle for small numbers --- shell/utils.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'shell') diff --git a/shell/utils.js b/shell/utils.js index 786e0009e6f..027ba0d6386 100644 --- a/shell/utils.js +++ b/shell/utils.js @@ -132,15 +132,16 @@ assert.gt = function( a , b , msg ){ doassert( a + " is not greater than " + b + " : " + msg ); } -assert.close = function( a , b , msg ){ - if (a === 0 && b === 0) { - return; +assert.close = function( a , b , msg , places ){ + if (places === undefined) { + places = 4; } - var diff = Math.abs( (a-b)/((a+b)/2) ); - if ( diff < .001 ) + if (Math.round((a - b) * Math.pow(10, places)) === 0) { return; - doassert( a + " is not close to " + b + " diff: " + diff + " : " + msg ); -} + } + doassert( a + " is not equal to " + b + " within " + places + + " places, diff: " + (a-b) + " : " + msg ); +}; Object.extend = function( dst , src , deep ){ for ( var k in src ){ -- cgit v1.2.1