Skip to content

refactor: replace built-ins by stdlib packages, update benchmarks in math/base/special/trunc #3941

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions lib/node_modules/@stdlib/math/base/special/trunc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,13 @@ v = trunc( -Infinity );
<!-- eslint no-undef: "error" -->

```javascript
var randu = require( '@stdlib/random/base/randu' );
var randu = require( '@stdlib/random/array/uniform' );
var trunc = require( '@stdlib/math/base/special/trunc' );

var x;
var x = randu( 100, -50.0, 50.0 );
var i;

for ( i = 0; i < 100; i++ ) {
x = (randu()*100.0) - 50.0;
console.log( 'trunc(%d) = %d', x, trunc( x ) );
for ( i = 0; i < x.length; i++ ) {
console.log( 'trunc(%d) = %d', x[ i ], trunc( x[ i ] ) );
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
// MODULES //

var bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var randu = require( '@stdlib/random/array/uniform' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pkg = require( './../package.json' ).name;
var trunc = require( './../lib' );
Expand All @@ -41,10 +41,11 @@ bench( pkg, function benchmark( b ) {
var y;
var i;

x = randu( 100, -500.0, 500.0 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = ( randu()*1000.0 ) - 500.0;
y = trunc( x );
y = trunc( x[ i % x.length ] );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
Expand All @@ -62,10 +63,11 @@ bench( pkg+'::built-in', opts, function benchmark( b ) {
var y;
var i;

x = randu( 100, -500.0, 500.0 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = ( randu()*1000.0 ) - 500.0;
y = Math.trunc( x ); // eslint-disable-line stdlib/no-builtin-math
y = Math.trunc( x[ i % x.length ] ); // eslint-disable-line stdlib/no-builtin-math
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

var resolve = require( 'path' ).resolve;
var bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var randu = require( '@stdlib/random/array/uniform' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var tryRequire = require( '@stdlib/utils/try-require' );
var pkg = require( './../package.json' ).name;
Expand All @@ -43,10 +43,11 @@ bench( pkg+'::native', opts, function benchmark( b ) {
var y;
var i;

x = randu( 100, -500.0, 500.0 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = ( randu()*1000.0 ) - 500.0;
y = trunc( x );
y = trunc( x[ i % x.length ] );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,19 @@ static double rand_double( void ) {
* @return elapsed time in seconds
*/
static double benchmark( void ) {
double x[ 100 ];
double elapsed;
double x;
double y;
double t;
int i;

for ( i = 0; i < 100; i++ ) {
x[ i ] = ( 1000.0 * rand_double() ) - 500.0;
}

t = tic();
for ( i = 0; i < ITERATIONS; i++ ) {
x = ( 1000.0*rand_double() ) - 500.0;
y = trunc( x );
y = trunc( x[ i % 100 ] );
if ( y != y ) {
printf( "should not return NaN\n" );
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,19 @@ static double rand_double( void ) {
* @return elapsed time in seconds
*/
static double benchmark( void ) {
double x[ 100 ];
double elapsed;
double x;
double y;
double t;
int i;

for ( i = 0; i < 100; i++ ) {
x[ i ] = ( 1000.0 * rand_double() ) - 500.0;
}

t = tic();
for ( i = 0; i < ITERATIONS; i++ ) {
x = ( 1000.0*rand_double() ) - 500.0;
y = stdlib_base_trunc( x );
y = stdlib_base_trunc( x[ i % 100 ] );
if ( y != y ) {
printf( "should not return NaN\n" );
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@

'use strict';

var randu = require( '@stdlib/random/base/randu' );
var randu = require( '@stdlib/random/array/uniform' );
var trunc = require( './../lib' );

var x;
var x = randu( 100, -50.0, 50.0 );
var i;

for ( i = 0; i < 100; i++ ) {
x = (randu()*100.0) - 50.0;
console.log( 'trunc(%d) = %d', x, trunc( x ) );
for ( i = 0; i < x.length; i++ ) {
console.log( 'trunc(%d) = %d', x[ i ], trunc( x[ i ] ) );
}
27 changes: 19 additions & 8 deletions lib/node_modules/@stdlib/math/base/special/trunc/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"task": "build",
"wasm": false,
"src": [
"./src/trunc.c"
"./src/main.c"
],
"include": [
"./include"
Expand All @@ -40,14 +40,16 @@
],
"libpath": [],
"dependencies": [
"@stdlib/math/base/napi/unary"
"@stdlib/math/base/napi/unary",
"@stdlib/math/base/special/floor",
"@stdlib/math/base/special/ceil"
]
},
{
"task": "benchmark",
"wasm": false,
"src": [
"./src/trunc.c"
"./src/main.c"
],
"include": [
"./include"
Expand All @@ -56,13 +58,16 @@
"-lm"
],
"libpath": [],
"dependencies": []
"dependencies": [
"@stdlib/math/base/special/floor",
"@stdlib/math/base/special/ceil"
]
},
{
"task": "examples",
"wasm": false,
"src": [
"./src/trunc.c"
"./src/main.c"
],
"include": [
"./include"
Expand All @@ -71,13 +76,16 @@
"-lm"
],
"libpath": [],
"dependencies": []
"dependencies": [
"@stdlib/math/base/special/floor",
"@stdlib/math/base/special/ceil"
]
},
{
"task": "build",
"wasm": true,
"src": [
"./src/trunc.c"
"./src/main.c"
],
"include": [
"./include"
Expand All @@ -86,7 +94,10 @@
"-lm"
],
"libpath": [],
"dependencies": []
"dependencies": [
"@stdlib/math/base/special/floor",
"@stdlib/math/base/special/ceil"
]
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
*/

#include "stdlib/math/base/special/trunc.h"
#include <math.h>
#include "stdlib/math/base/special/floor.h"
#include "stdlib/math/base/special/ceil.h"

/**
* Rounds a double-precision floating-point number toward zero.
Expand All @@ -30,5 +31,8 @@
* // returns 3.0
*/
double stdlib_base_trunc( const double x ) {
return trunc( x );
if ( x < 0.0 ) {
return stdlib_base_ceil( x );
}
return stdlib_base_floor( x );
}
14 changes: 7 additions & 7 deletions lib/node_modules/@stdlib/math/base/special/trunc/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,37 +38,37 @@ tape( 'main export is a function', function test( t ) {
});

tape( 'the function rounds a numeric value toward 0', function test( t ) {
t.strictEqual( trunc( -4.2 ), -4.0, 'equals -4' );
t.strictEqual( trunc( 9.99999 ), 9.0, 'equals 9' );
t.strictEqual( trunc( -4.2 ), -4.0, 'returns expected value' );
t.strictEqual( trunc( 9.99999 ), 9.0, 'returns expected value' );
t.end();
});

tape( 'if provided `+0`, the function returns `+0`', function test( t ) {
var v = trunc( 0.0 );
t.strictEqual( isPositiveZero( v ), true, 'equals +0' );
t.strictEqual( isPositiveZero( v ), true, 'returns expected value' );
t.end();
});

tape( 'if provided `-0`, the function returns `-0`', function test( t ) {
var v = trunc( -0.0 );
t.strictEqual( isNegativeZero( v ), true, 'returns -0' );
t.strictEqual( isNegativeZero( v ), true, 'returns expected value' );
t.end();
});

tape( 'the function returns `NaN` if provided `NaN`', function test( t ) {
var v = trunc( NaN );
t.strictEqual( isnan( v ), true, 'returns NaN' );
t.strictEqual( isnan( v ), true, 'returns expected value' );
t.end();
});

tape( 'the function returns `+infinity` if provided `+infinity`', function test( t ) {
var v = trunc( PINF );
t.strictEqual( v, PINF, 'returns +infinity' );
t.strictEqual( v, PINF, 'returns expected value' );
t.end();
});

tape( 'the function returns `-infinity` if provided `-infinity`', function test( t ) {
var v = trunc( NINF );
t.strictEqual( v, NINF, 'returns -infinity' );
t.strictEqual( v, NINF, 'returns expected value' );
t.end();
});
Original file line number Diff line number Diff line change
Expand Up @@ -47,37 +47,37 @@ tape( 'main export is a function', opts, function test( t ) {
});

tape( 'the function rounds a numeric value toward 0', opts, function test( t ) {
t.strictEqual( trunc( -4.2 ), -4.0, 'equals -4' );
t.strictEqual( trunc( 9.99999 ), 9.0, 'equals 9' );
t.strictEqual( trunc( -4.2 ), -4.0, 'returns expected value' );
t.strictEqual( trunc( 9.99999 ), 9.0, 'returns expected value' );
t.end();
});

tape( 'if provided `+0`, the function returns `+0`', opts, function test( t ) {
var v = trunc( 0.0 );
t.strictEqual( isPositiveZero( v ), true, 'equals +0' );
t.strictEqual( isPositiveZero( v ), true, 'returns expected value' );
t.end();
});

tape( 'if provided `-0`, the function returns `-0`', opts, function test( t ) {
var v = trunc( -0.0 );
t.strictEqual( isNegativeZero( v ), true, 'returns -0' );
t.strictEqual( isNegativeZero( v ), true, 'returns expected value' );
t.end();
});

tape( 'the function returns `NaN` if provided `NaN`', opts, function test( t ) {
var v = trunc( NaN );
t.strictEqual( isnan( v ), true, 'returns NaN' );
t.strictEqual( isnan( v ), true, 'returns expected value' );
t.end();
});

tape( 'the function returns `+infinity` if provided `+infinity`', opts, function test( t ) {
var v = trunc( PINF );
t.strictEqual( v, PINF, 'returns +infinity' );
t.strictEqual( v, PINF, 'returns expected value' );
t.end();
});

tape( 'the function returns `-infinity` if provided `-infinity`', opts, function test( t ) {
var v = trunc( NINF );
t.strictEqual( v, NINF, 'returns -infinity' );
t.strictEqual( v, NINF, 'returns expected value' );
t.end();
});
Loading