Skip to content

Commit 947e77f

Browse files
committed
fix: handle large numbers correctly and revert to Math.round for JS
1 parent dbe3a16 commit 947e77f

File tree

4 files changed

+49
-19
lines changed

4 files changed

+49
-19
lines changed

lib/node_modules/@stdlib/math/base/special/round/lib/main.js

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,6 @@
1818

1919
'use strict';
2020

21-
// MODULES //
22-
23-
var isnan = require( '@stdlib/math/base/assert/is-nan' );
24-
var floor = require( '@stdlib/math/base/special/floor' );
25-
var isNegativeZero = require( '@stdlib/math/base/assert/is-negative-zero' );
26-
27-
2821
// MAIN //
2922

3023
/**
@@ -77,18 +70,7 @@ var isNegativeZero = require( '@stdlib/math/base/assert/is-negative-zero' );
7770
* var v = round( NaN );
7871
* // returns NaN
7972
*/
80-
function round( x ) {
81-
if ( isnan( x ) ) {
82-
return NaN;
83-
}
84-
if ( isNegativeZero( x ) || ( x >= -0.5 && x < 0.0 ) ) {
85-
return -0.0;
86-
}
87-
if ( x > 0.0 && x < 0.5 ) {
88-
return 0.0;
89-
}
90-
return floor( x + 0.5 );
91-
}
73+
var round = Math.round; // eslint-disable-line stdlib/no-builtin-math
9274

9375

9476
// EXPORTS //

lib/node_modules/@stdlib/math/base/special/round/src/main.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,9 @@ double stdlib_base_round( const double x ) {
4141
if ( x > 0.0 && x < 0.5 ) {
4242
return 0.0; // 0
4343
}
44+
// If the magnitude is big enough, there's no place for the fraction part. If we try to add 0.5 to this number, 1.0 will be added instead...
45+
if ( x >= 4503599627370496.0 || x <= -4503599627370496.0 ) {
46+
return x;
47+
}
4448
return stdlib_base_floor( x + 0.5 );
4549
}

lib/node_modules/@stdlib/math/base/special/round/test/test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,25 @@ tape( 'the function returns `-infinity` if provided a `-infinity`', function tes
7272
t.strictEqual( v, NINF, 'returns -infinity' );
7373
t.end();
7474
});
75+
76+
tape( 'the function returns the correct result for large positive non-decimal values', function test( t ) {
77+
var start = 4503599627370496;
78+
var end = 4503599627375000;
79+
var i;
80+
81+
for ( i = start; i < end; i++ ) {
82+
t.strictEqual( round( i ), i, 'returns '+i );
83+
}
84+
t.end();
85+
});
86+
87+
tape( 'the function returns the correct result for large negative non-decimal values', function test( t ) {
88+
var start = -4503599627375000;
89+
var end = -4503599627370496;
90+
var i;
91+
92+
for ( i = start; i < end; i++ ) {
93+
t.strictEqual( round( i ), i, 'returns '+i );
94+
}
95+
t.end();
96+
});

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,25 @@ tape( 'the function returns `-infinity` if provided a `-infinity`', opts, functi
8181
t.strictEqual( v, NINF, 'returns -infinity' );
8282
t.end();
8383
});
84+
85+
tape( 'the function returns the correct result for large positive non-decimal values', function test( t ) {
86+
var start = 4503599627370496;
87+
var end = 4503599627375000;
88+
var i;
89+
90+
for ( i = start; i < end; i++ ) {
91+
t.strictEqual( round( i ), i, 'returns '+i );
92+
}
93+
t.end();
94+
});
95+
96+
tape( 'the function returns the correct result for large negative non-decimal values', function test( t ) {
97+
var start = -4503599627375000;
98+
var end = -4503599627370496;
99+
var i;
100+
101+
for ( i = start; i < end; i++ ) {
102+
t.strictEqual( round( i ), i, 'returns '+i );
103+
}
104+
t.end();
105+
});

0 commit comments

Comments
 (0)