Skip to content

Commit fe96d97

Browse files
committed
test: use ulpdiff for floating-point comparisons
--- 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: na - 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 3ab83ba commit fe96d97

File tree

2 files changed

+30
-40
lines changed

2 files changed

+30
-40
lines changed

lib/node_modules/@stdlib/math/base/special/fast/hypotf/test/test.js

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
// MODULES //
2222

2323
var tape = require( 'tape' );
24+
var f32 = require( '@stdlib/number/float64/base/to-float32' );
25+
var ulpdiff = require( '@stdlib/number/float32/base/ulp-difference' );
2426
var PINF = require( '@stdlib/constants/float32/pinf' );
25-
var EPS = require( '@stdlib/constants/float32/eps' );
26-
var absf = require( '@stdlib/math/base/special/absf' );
2727
var hypotf = require( './../lib' );
2828

2929

@@ -42,8 +42,6 @@ tape( 'main export is a function', function test( t ) {
4242

4343
tape( 'the function computes the hypotenuse', function test( t ) {
4444
var expected;
45-
var delta;
46-
var tol;
4745
var h;
4846
var x;
4947
var y;
@@ -54,41 +52,38 @@ tape( 'the function computes the hypotenuse', function test( t ) {
5452
expected = data.expected;
5553

5654
for ( i = 0; i < x.length; i++ ) {
55+
x[ i ] = f32( x[ i ] );
56+
y[ i ] = f32( y[ i ] );
57+
expected[ i ] = f32( expected[ i ] );
5758
h = hypotf( x[ i ], y[ i ] );
58-
if ( h === expected[ i ] ) {
59-
t.ok( true, 'x: '+x[ i ]+'. y: '+y[ i ]+'. h: '+h+'. Expected: '+expected[ i ]+'.' );
60-
} else {
61-
delta = absf( h - expected[ i ] );
62-
tol = 1.4 * EPS * absf( expected[ i ] );
63-
t.strictEqual( delta <= tol, true, 'within tolerance. x: '+x[ i ]+'. y: '+y[ i ]+'. h: '+h+'. Expected: '+expected[ i ]+'. Delta: '+delta+'. Tol: '+tol+'.' );
64-
}
59+
t.strictEqual( ulpdiff( h, expected[ i ] ) <= 1, true, 'returns expected value' );
6560
}
6661
t.end();
6762
});
6863

6964
tape( 'the function computes the hypotenuse (canonical inputs)', function test( t ) {
7065
var h;
7166

72-
h = hypotf( 3.0, 4.0 );
73-
t.strictEqual( h, 5.0, 'returns expected value' );
67+
h = hypotf( f32( 3.0 ), f32( 4.0 ) );
68+
t.strictEqual( h, f32( 5.0 ), 'returns expected value' );
7469

75-
h = hypotf( 6.0, 8.0 );
76-
t.strictEqual( h, 10.0, 'returns expected value' );
70+
h = hypotf( f32( 6.0 ), f32( 8.0 ) );
71+
t.strictEqual( h, f32( 10.0 ), 'returns expected value' );
7772

78-
h = hypotf( 5.0, 12.0 );
79-
t.strictEqual( h, 13.0, 'returns expected value' );
73+
h = hypotf( f32( 5.0 ), f32( 12.0 ) );
74+
t.strictEqual( h, f32( 13.0 ), 'returns expected value' );
8075

8176
t.end();
8277
});
8378

8479
tape( 'the function can overflow', function test( t ) {
85-
var h = hypotf( 1.0e38, 1.0e38 );
80+
var h = hypotf( f32( 1.0e38 ), f32( 1.0e38 ) );
8681
t.strictEqual( h, PINF, 'returns expected value' );
8782
t.end();
8883
});
8984

9085
tape( 'the function can underflow', function test( t ) {
91-
var h = hypotf( 1.0e-45, 1.0e-45 );
92-
t.strictEqual( h, 0.0, 'returns expected value' );
86+
var h = hypotf( f32( 1.0e-45 ), f32( 1.0e-45 ) );
87+
t.strictEqual( h, f32( 0.0 ), 'returns expected value' );
9388
t.end();
9489
});

lib/node_modules/@stdlib/math/base/special/fast/hypotf/test/test.native.js

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222

2323
var resolve = require( 'path' ).resolve;
2424
var tape = require( 'tape' );
25+
var f32 = require( '@stdlib/number/float64/base/to-float32' );
26+
var ulpdiff = require( '@stdlib/number/float32/base/ulp-difference' );
2527
var PINF = require( '@stdlib/constants/float32/pinf' );
26-
var EPS = require( '@stdlib/constants/float32/eps' );
2728
var tryRequire = require( '@stdlib/utils/try-require' );
28-
var absf = require( '@stdlib/math/base/special/absf' );
2929

3030

3131
// VARIABLES //
@@ -51,8 +51,6 @@ tape( 'main export is a function', opts, function test( t ) {
5151

5252
tape( 'the function computes the hypotenuse', opts, function test( t ) {
5353
var expected;
54-
var delta;
55-
var tol;
5654
var h;
5755
var x;
5856
var y;
@@ -63,41 +61,38 @@ tape( 'the function computes the hypotenuse', opts, function test( t ) {
6361
expected = data.expected;
6462

6563
for ( i = 0; i < x.length; i++ ) {
64+
x[ i ] = f32( x[ i ] );
65+
y[ i ] = f32( y[ i ] );
66+
expected[ i ] = f32( expected[ i ] );
6667
h = hypotf( x[ i ], y[ i ] );
67-
if ( h === expected[ i ] ) {
68-
t.ok( true, 'x: '+x[ i ]+'. y: '+y[ i ]+'. h: '+h+'. Expected: '+expected[ i ]+'.' );
69-
} else {
70-
delta = absf( h - expected[ i ] );
71-
tol = 1.4 * EPS * absf( expected[ i ] );
72-
t.strictEqual( delta <= tol, true, 'within tolerance. x: '+x[ i ]+'. y: '+y[ i ]+'. h: '+h+'. Expected: '+expected[ i ]+'. Delta: '+delta+'. Tol: '+tol+'.' );
73-
}
68+
t.strictEqual( ulpdiff( h, expected[ i ] ) <= 1, true, 'returns expected value' );
7469
}
7570
t.end();
7671
});
7772

7873
tape( 'the function computes the hypotenuse (canonical inputs)', opts, function test( t ) {
7974
var h;
8075

81-
h = hypotf( 3.0, 4.0 );
82-
t.strictEqual( h, 5.0, 'returns expected value' );
76+
h = hypotf( f32( 3.0 ), f32( 4.0 ) );
77+
t.strictEqual( h, f32( 5.0 ), 'returns expected value' );
8378

84-
h = hypotf( 6.0, 8.0 );
85-
t.strictEqual( h, 10.0, 'returns expected value' );
79+
h = hypotf( f32( 6.0 ), f32( 8.0 ) );
80+
t.strictEqual( h, f32( 10.0 ), 'returns expected value' );
8681

87-
h = hypotf( 5.0, 12.0 );
88-
t.strictEqual( h, 13.0, 'returns expected value' );
82+
h = hypotf( f32( 5.0 ), f32( 12.0 ) );
83+
t.strictEqual( h, f32( 13.0 ), 'returns expected value' );
8984

9085
t.end();
9186
});
9287

9388
tape( 'the function can overflow', opts, function test( t ) {
94-
var h = hypotf( 1.0e38, 1.0e38 );
89+
var h = hypotf( f32( 1.0e38 ), f32( 1.0e38 ) );
9590
t.strictEqual( h, PINF, 'returns expected value' );
9691
t.end();
9792
});
9893

9994
tape( 'the function can underflow', opts, function test( t ) {
100-
var h = hypotf( 1.0e-45, 1.0e-45 );
101-
t.strictEqual( h, 0.0, 'returns expected value' );
95+
var h = hypotf( f32( 1.0e-45 ), f32( 1.0e-45 ) );
96+
t.strictEqual( h, f32( 0.0 ), 'returns expected value' );
10297
t.end();
10398
});

0 commit comments

Comments
 (0)