Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

isAlmostSameValue

Test if two arguments are approximately the same value within a specified number of ULPs (units in the last place).

Usage

var isAlmostSameValue = require( '@stdlib/assert/is-almost-same-value' );

isAlmostSameValue( a, b, maxULP )

Tests if two arguments are approximately the same value within a specified number of ULPs (units in the last place).

var EPS = require( '@stdlib/constants/float64/eps' );

var bool = isAlmostSameValue( 1.0, 1.0+EPS, 1 );
// returns true

bool = isAlmostSameValue( '', '', 0 );
// returns true

bool = isAlmostSameValue( {}, {}, 1 );
// returns false

In contrast to the strict equality operator ===, the function distinguishes between +0 and -0 and treats NaNs as the same value.

var Complex128 = require( '@stdlib/complex/float64/ctor' );

var bool = isAlmostSameValue( NaN, 1.0, 1 );
// returns false

bool = isAlmostSameValue( NaN, NaN, 1 );
// returns true

var z1 = new Complex128( NaN, 3.0 );
var z2 = new Complex128( 1.0, 3.0 );

bool = isAlmostSameValue( z1, z2, 1 );
// returns false

z1 = new Complex128( NaN, NaN );
z2 = new Complex128( NaN, NaN );

bool = isAlmostSameValue( z1, z2, 1 );
// returns true

bool = isAlmostSameValue( 0.0, -0.0, 0 );
// returns false

z1 = new Complex128( 0.0, 0.0 );
z2 = new Complex128( -0.0, -0.0 );

bool = isAlmostSameValue( z1, z2, 0 );
// returns false

Notes

Examples

var EPS = require( '@stdlib/constants/float64/eps' );
var Complex128 = require( '@stdlib/complex/float64/ctor' );
var isAlmostSameValue = require( '@stdlib/assert/is-almost-same-value' );

console.log( isAlmostSameValue( true, true, 0 ) );
// => true

console.log( isAlmostSameValue( true, false, 1 ) );
// => false

console.log( isAlmostSameValue( 'beep', 'beep', 1 ) );
// => true

console.log( isAlmostSameValue( 1.0, 1.0+EPS, 1 ) );
// => true

console.log( isAlmostSameValue( null, null, 0 ) );
// => true

console.log( isAlmostSameValue( 0.0, -0.0, 0 ) );
// => false

console.log( isAlmostSameValue( NaN, NaN, 1 ) );
// => true

var z1 = new Complex128( 1.0, 3.0+EPS );
var z2 = new Complex128( 1.0+EPS, 3.0 );
console.log( isAlmostSameValue( z1, z2, 1 ) );
// => true

console.log( isAlmostSameValue( {}, {}, 1 ) );
// => false

console.log( isAlmostSameValue( [], [], 1 ) );
// => false

console.log( isAlmostSameValue( isAlmostSameValue, isAlmostSameValue, 0 ) );
// => true