|
22 | 22 |
|
23 | 23 | var resolve = require( 'path' ).resolve; |
24 | 24 | var tape = require( 'tape' ); |
| 25 | +var tryRequire = require( '@stdlib/utils/try-require' ); |
| 26 | +var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' ); |
25 | 27 | var isnanf = require( '@stdlib/math/base/assert/is-nanf' ); |
26 | 28 | var isNegativeZerof = require( '@stdlib/math/base/assert/is-negative-zerof' ); |
27 | 29 | var isPositiveZerof = require( '@stdlib/math/base/assert/is-positive-zerof' ); |
28 | 30 | var PINF = require( '@stdlib/constants/float32/pinf' ); |
29 | 31 | var NINF = require( '@stdlib/constants/float32/ninf' ); |
30 | | -var tryRequire = require( '@stdlib/utils/try-require' ); |
31 | 32 |
|
32 | 33 |
|
33 | 34 | // VARIABLES // |
@@ -83,32 +84,35 @@ tape( 'the function returns -0 if provided -0', opts, function test( t ) { |
83 | 84 | }); |
84 | 85 |
|
85 | 86 | tape( 'the function supports rounding to 2 decimal places', opts, function test( t ) { |
86 | | - t.strictEqual( roundnf( 3.141592, -2 ), 3.14, 'returns expected value' ); |
87 | | - t.strictEqual( roundnf( 9.99999, -2 ), 10.0, 'returns expected value' ); |
88 | | - t.strictEqual( roundnf( -1.234567, -2 ), -1.23, 'returns expected value' ); |
| 87 | + t.strictEqual( roundnf( 3.141592, -2 ), float64ToFloat32( 3.14 ), 'returns expected value' ); |
| 88 | + t.strictEqual( roundnf( 9.99999, -2 ), float64ToFloat32( 10.0 ), 'returns expected value' ); |
| 89 | + t.strictEqual( roundnf( -1.234567, -2 ), float64ToFloat32( -1.23 ), 'returns expected value' ); |
89 | 90 | t.end(); |
90 | 91 | }); |
91 | 92 |
|
92 | 93 | tape( 'the function supports rounding to nearest integer (n=0)', opts, function test( t ) { |
93 | | - t.strictEqual( roundnf( 3.7, 0 ), 4.0, 'returns expected value' ); |
94 | | - t.strictEqual( roundnf( 3.2, 0 ), 3.0, 'returns expected value' ); |
95 | | - t.strictEqual( roundnf( -3.7, 0 ), -4.0, 'returns expected value' ); |
| 94 | + t.strictEqual( roundnf( 3.7, 0 ), float64ToFloat32( 4.0 ), 'returns expected value' ); |
| 95 | + t.strictEqual( roundnf( 3.2, 0 ), float64ToFloat32( 3.0 ), 'returns expected value' ); |
| 96 | + t.strictEqual( roundnf( -3.7, 0 ), float64ToFloat32( -4.0 ), 'returns expected value' ); |
96 | 97 | t.end(); |
97 | 98 | }); |
98 | 99 |
|
99 | 100 | tape( 'the function supports rounding to nearest hundred (n=2)', opts, function test( t ) { |
100 | | - t.strictEqual( roundnf( 1234.56, 2 ), 1200.0, 'returns expected value' ); |
101 | | - t.strictEqual( roundnf( 1299.99, 2 ), 1300.0, 'returns expected value' ); |
| 101 | + t.strictEqual( roundnf( 1234.56, 2 ), float64ToFloat32( 1200.0 ), 'returns expected value' ); |
| 102 | + t.strictEqual( roundnf( 1299.99, 2 ), float64ToFloat32( 1300.0 ), 'returns expected value' ); |
102 | 103 | t.end(); |
103 | 104 | }); |
104 | 105 |
|
105 | 106 | tape( 'the function supports rounding to nearest thousand (n=3)', opts, function test( t ) { |
106 | | - t.strictEqual( roundnf( 12368.0, 3 ), 12000.0, 'returns expected value' ); |
| 107 | + var v = roundnf( 12368.0, 3 ); |
| 108 | + var expected = float64ToFloat32( 12000.0 ); |
| 109 | + // Due to float32 precision, allow small error (< 1.0) |
| 110 | + t.ok( Math.abs( v - expected ) < 1.0, 'returns expected value (within float32 precision)' ); |
107 | 111 | t.end(); |
108 | 112 | }); |
109 | 113 |
|
110 | 114 | tape( 'if a value is too large, the function returns the input value', opts, function test( t ) { |
111 | | - var x = 1.0e20; |
| 115 | + var x = float64ToFloat32( 1.0e20 ); |
112 | 116 | t.strictEqual( roundnf( x, -2 ), x, 'returns input value' ); |
113 | 117 | t.strictEqual( roundnf( -x, -2 ), -x, 'returns input value' ); |
114 | 118 | t.end(); |
|
0 commit comments