22
22
23
23
var resolve = require ( 'path' ) . resolve ;
24
24
var tape = require ( 'tape' ) ;
25
+ var f32 = require ( '@stdlib/number/float64/base/to-float32' ) ;
26
+ var ulpdiff = require ( '@stdlib/number/float32/base/ulp-difference' ) ;
25
27
var PINF = require ( '@stdlib/constants/float32/pinf' ) ;
26
- var EPS = require ( '@stdlib/constants/float32/eps' ) ;
27
28
var tryRequire = require ( '@stdlib/utils/try-require' ) ;
28
- var absf = require ( '@stdlib/math/base/special/absf' ) ;
29
29
30
30
31
31
// VARIABLES //
@@ -51,8 +51,6 @@ tape( 'main export is a function', opts, function test( t ) {
51
51
52
52
tape ( 'the function computes the hypotenuse' , opts , function test ( t ) {
53
53
var expected ;
54
- var delta ;
55
- var tol ;
56
54
var h ;
57
55
var x ;
58
56
var y ;
@@ -63,41 +61,38 @@ tape( 'the function computes the hypotenuse', opts, function test( t ) {
63
61
expected = data . expected ;
64
62
65
63
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 ] ) ;
66
67
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' ) ;
74
69
}
75
70
t . end ( ) ;
76
71
} ) ;
77
72
78
73
tape ( 'the function computes the hypotenuse (canonical inputs)' , opts , function test ( t ) {
79
74
var h ;
80
75
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' ) ;
83
78
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' ) ;
86
81
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' ) ;
89
84
90
85
t . end ( ) ;
91
86
} ) ;
92
87
93
88
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 ) ) ;
95
90
t . strictEqual ( h , PINF , 'returns expected value' ) ;
96
91
t . end ( ) ;
97
92
} ) ;
98
93
99
94
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' ) ;
102
97
t . end ( ) ;
103
98
} ) ;
0 commit comments