Skip to content

Commit 6a54bf9

Browse files
committed
feat: add assert/is-almost-equal
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent 4d5b888 commit 6a54bf9

File tree

2 files changed

+43
-8
lines changed

2 files changed

+43
-8
lines changed

lib/node_modules/@stdlib/assert/is-almost-equal/lib/main.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ function isAlmostEqual( a, b, maxULP ) {
7171
return isAlmostEqualF64( a, b, maxULP );
7272
}
7373
if ( isComplexLike( a ) && isComplexLike( b ) ) {
74-
if ( a.BYTES_PER_ELEMENT === 8 && b.BYTES_PER_ELEMENT === 8 ) {
75-
return isAlmostEqualComplex64( a, b, maxULP );
74+
if ( a.BYTES_PER_ELEMENT === 4 && b.BYTES_PER_ELEMENT === 4 ) {
75+
return isAlmostEqualComplex64( a, b, maxULP );
7676
}
77-
return isAlmostEqualComplex128( a, b, maxULP );
77+
return isAlmostEqualComplex128( a, b, maxULP );
7878
}
7979
return false;
8080
}

lib/node_modules/@stdlib/assert/is-almost-equal/test/test.js

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
var tape = require( 'tape' );
2626
var EPS_F64 = require( '@stdlib/constants/float64/eps' );
2727
var EPS_F32 = require( '@stdlib/constants/float32/eps' );
28-
var f32 = require( '@stdlib/number/float64/base/to-float32' );
2928
var Number = require( '@stdlib/number/ctor' );
3029
var Boolean = require( '@stdlib/boolean/ctor' );
3130
var Complex128 = require( '@stdlib/complex/float64/ctor' );
@@ -66,7 +65,6 @@ tape( 'the function returns `true` if provided two arguments which are the same
6665
new Complex64( 5.0, 2.0 )
6766
];
6867
for ( i = 0; i < values.length; i++ ) {
69-
console.log( values[ i ] );
7068
t.strictEqual( isAlmostEqual( values[ i ], values[ i ], 0 ), true, 'returns expected value' );
7169
t.strictEqual( isAlmostEqual( values[ i ], values[ i ], 1 ), true, 'returns expected value' );
7270
}
@@ -87,7 +85,6 @@ tape( 'the function returns `true` if provided two arguments which are the same
8785
});
8886

8987
tape( 'the function returns `true` if provided two arguments which are approximately equal within a specified number of ULPs', function test( t ) {
90-
t.strictEqual( isAlmostEqual( f32( 1.0 ), f32( f32( 1.0 )+EPS_F32 ), 1 ), true, 'returns expected value' );
9188
t.strictEqual( isAlmostEqual( 1.0, 1.0+EPS_F64, 1 ), true, 'returns expected value' );
9289
t.strictEqual( isAlmostEqual( 1.0+EPS_F64, 1.0, 1 ), true, 'returns expected value' );
9390
t.strictEqual( isAlmostEqual( 1.0, 1.0+EPS_F64+EPS_F64, 2 ), true, 'returns expected value' );
@@ -110,6 +107,7 @@ tape( 'the function returns `false` if provided two arguments which are not appr
110107
5,
111108
3.14,
112109
-3.14,
110+
new Number( 5 ),
113111
true,
114112
false,
115113
new Boolean( true ),
@@ -130,6 +128,7 @@ tape( 'the function returns `false` if provided two arguments which are not appr
130128
-5,
131129
-3.14,
132130
3.14,
131+
new Number( 5 ),
133132
false,
134133
true,
135134
new Boolean( true ),
@@ -144,8 +143,6 @@ tape( 'the function returns `false` if provided two arguments which are not appr
144143
new Complex64( 5.0, -2.0 )
145144
];
146145
for ( i = 0; i < a.length; i++ ) {
147-
console.log( a[ i ], b[ i ] );
148-
console.log(isAlmostEqual( a[ i ], b[ i ], 1 ));
149146
t.strictEqual( isAlmostEqual( a[ i ], b[ i ], 1 ), false, 'returns expected value' );
150147
}
151148

@@ -157,3 +154,41 @@ tape( 'the function returns `false` if provided two arguments which are not appr
157154

158155
t.end();
159156
});
157+
158+
tape( 'the function returns `false` if either input value is `NaN` or in the case of complex numbers, if either the real or imaginary component is `NaN`', function test( t ) {
159+
var a;
160+
var b;
161+
var i;
162+
163+
a = [
164+
NaN,
165+
NaN,
166+
new Complex128( NaN, 3.0 ),
167+
new Complex64( 5.0, NaN ),
168+
new Complex128( NaN, NaN ),
169+
new Complex64( NaN, NaN )
170+
];
171+
b = [
172+
NaN,
173+
1.0,
174+
new Complex128( 1.0, 2.0 ),
175+
new Complex64( 3.0, 4.0 ),
176+
new Complex128( NaN, NaN ),
177+
new Complex64( NaN, NaN )
178+
];
179+
for ( i = 0; i < a.length; i++ ) {
180+
t.strictEqual( isAlmostEqual( a[ i ], b[ i ], 1 ), false, 'returns expected value' );
181+
}
182+
183+
t.end();
184+
});
185+
186+
tape( 'the function does not distinguish between `-0` and `+0`', function test( t ) {
187+
t.strictEqual( isAlmostEqual( -0.0, 0.0, 0 ), true, 'returns expected value' );
188+
t.strictEqual( isAlmostEqual( 0.0, -0.0, 0 ), true, 'returns expected value' );
189+
190+
t.strictEqual( isAlmostEqual( new Complex128( -0.0, 3.0 ), new Complex128( 0.0, 3.0 ), 1 ), true, 'returns expected value' );
191+
t.strictEqual( isAlmostEqual( new Complex64( -0.0, 3.0 ), new Complex64( 0.0, 3.0 ), 1 ), true, 'returns expected value' );
192+
193+
t.end();
194+
});

0 commit comments

Comments
 (0)