-
-
Notifications
You must be signed in to change notification settings - Fork 809
/
Copy pathtest.js
85 lines (70 loc) · 2.4 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
'use strict';
// MODULES //
var tape = require( 'tape' );
var PINF = require( '@stdlib/math/constants/float32-pinf' );
var NINF = require( '@stdlib/math/constants/float32-ninf' );
var randu = require( '@stdlib/math/base/random/randu' );
var round = require( '@stdlib/math/base/special/round' );
var pow = require( '@stdlib/math/base/special/pow' );
var toFloat32 = require( '@stdlib/math/base/utils/float64-to-float32' );
var bits = require( '@stdlib/math/base/utils/float32-to-binary-string' );
var significandf = require( './../lib' );
// TESTS //
tape( 'main export is a function', function test( t ) {
t.ok( true, __filename );
t.equal( typeof significandf, 'function', 'main export is a function' );
t.end();
});
tape( 'the function returns a number', function test( t ) {
t.equal( typeof significandf( toFloat32( 3.14e30 ) ), 'number', 'returns a number' );
t.end();
});
tape( 'the function returns an integer corresponding to the significand of a single-precision floating-point number', function test( t ) {
var expected;
var actual;
var sign;
var frac;
var exp;
var x;
var b;
var i;
for ( i = 0; i < 5000; i++ ) {
if ( randu() < 0.5 ) {
sign = -1.0;
} else {
sign = 1.0;
}
frac = randu() * 10.0;
exp = round( randu()*44.0 ) - 22;
x = sign * frac * pow( 10.0, exp );
x = toFloat32( x );
b = bits( x );
expected = parseInt( b.substring( 9 ), 2 );
actual = significandf( x );
t.equal( actual, expected, 'returns the significand for ' + x );
}
t.end();
});
tape( 'the function returns the significand for `+-0`', function test( t ) {
t.equal( significandf( 0.0 ), 0, 'returns 0' );
t.equal( significandf( -0.0 ), 0, 'returns 0' );
t.end();
});
tape( 'the function returns the significand for `+infinity`', function test( t ) {
t.equal( significandf( PINF ), 0, 'returns 0' );
t.end();
});
tape( 'the function returns the significand for `-infinity`', function test( t ) {
t.equal( significandf( NINF ), 0, 'returns 0' );
t.end();
});
tape( 'the function returns the significand for `NaN`', function test( t ) {
t.equal( significandf( NaN ), 4194304, 'returns int corresponding to bit sequence 10000000000000000000000' );
t.end();
});
tape( 'the function returns the significand for subnormals', function test( t ) {
var x = toFloat32( 3.14e-42 );
var s = parseInt( bits( x ).substring( 9 ), 2 );
t.equal( significandf( x ), s, 'returns the significand for ' + x );
t.end();
});