Skip to content

Commit

Permalink
Auto-generated commit
Browse files Browse the repository at this point in the history
  • Loading branch information
stdlib-bot committed May 22, 2024
1 parent 9884d2f commit c7012fa
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 25 deletions.
6 changes: 3 additions & 3 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions dist/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 1 addition & 19 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,6 @@

'use strict';

// MODULES //

var isnan = require( '@stdlib/math-base-assert-is-nan' );
var floor = require( '@stdlib/math-base-special-floor' );
var isNegativeZero = require( '@stdlib/math-base-assert-is-negative-zero' );


// MAIN //

/**
Expand Down Expand Up @@ -77,18 +70,7 @@ var isNegativeZero = require( '@stdlib/math-base-assert-is-negative-zero' );
* var v = round( NaN );
* // returns NaN
*/
function round( x ) {
if ( isnan( x ) ) {
return NaN;
}
if ( isNegativeZero( x ) || ( x >= -0.5 && x < 0.0 ) ) {
return -0.0;
}
if ( x > 0.0 && x < 0.5 ) {
return 0.0;
}
return floor( x + 0.5 );
}
var round = Math.round; // eslint-disable-line stdlib/no-builtin-math


// EXPORTS //
Expand Down
4 changes: 4 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,9 @@ double stdlib_base_round( const double x ) {
if ( x > 0.0 && x < 0.5 ) {
return 0.0; // 0
}
// 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...
if ( x >= 4503599627370496.0 || x <= -4503599627370496.0 ) {
return x;
}
return stdlib_base_floor( x + 0.5 );
}
22 changes: 22 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,25 @@ tape( 'the function returns `-infinity` if provided a `-infinity`', function tes
t.strictEqual( v, NINF, 'returns -infinity' );
t.end();
});

tape( 'the function returns the correct result for large positive non-decimal values', function test( t ) {
var start = 4503599627370496;
var end = 4503599627375000;
var i;

for ( i = start; i < end; i++ ) {
t.strictEqual( round( i ), i, 'returns '+i );
}
t.end();
});

tape( 'the function returns the correct result for large negative non-decimal values', function test( t ) {
var start = -4503599627375000;
var end = -4503599627370496;
var i;

for ( i = start; i < end; i++ ) {
t.strictEqual( round( i ), i, 'returns '+i );
}
t.end();
});
22 changes: 22 additions & 0 deletions test/test.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,25 @@ tape( 'the function returns `-infinity` if provided a `-infinity`', opts, functi
t.strictEqual( v, NINF, 'returns -infinity' );
t.end();
});

tape( 'the function returns the correct result for large positive non-decimal values', function test( t ) {
var start = 4503599627370496;
var end = 4503599627375000;
var i;

for ( i = start; i < end; i++ ) {
t.strictEqual( round( i ), i, 'returns '+i );
}
t.end();
});

tape( 'the function returns the correct result for large negative non-decimal values', function test( t ) {
var start = -4503599627375000;
var end = -4503599627370496;
var i;

for ( i = start; i < end; i++ ) {
t.strictEqual( round( i ), i, 'returns '+i );
}
t.end();
});

0 comments on commit c7012fa

Please sign in to comment.