From 8785e54ec82782d7d01912988596c9d7d2bf06d0 Mon Sep 17 00:00:00 2001 From: Gunj Joshi Date: Wed, 18 Dec 2024 20:01:20 +0530 Subject: [PATCH] refactor: replace built-ins by stdlib packages, update benchmarks in `math/base/special/trunc` PR-URL: https://github.com/stdlib-js/stdlib/pull/3941 Co-authored-by: Athan Reines Reviewed-by: Athan Reines Reviewed-by: Philipp Burckhardt Signed-off-by: Athan Reines Signed-off-by: Gunj Joshi --- .../@stdlib/math/base/special/trunc/README.md | 10 +++---- .../base/special/trunc/benchmark/benchmark.js | 12 +++++---- .../trunc/benchmark/benchmark.native.js | 7 ++--- .../special/trunc/benchmark/c/benchmark.c | 9 ++++--- .../trunc/benchmark/c/native/benchmark.c | 9 ++++--- .../math/base/special/trunc/examples/index.js | 10 +++---- .../math/base/special/trunc/manifest.json | 27 +++++++++++++------ .../special/trunc/src/{trunc.c => main.c} | 8 ++++-- .../math/base/special/trunc/test/test.js | 14 +++++----- .../base/special/trunc/test/test.native.js | 14 +++++----- 10 files changed, 70 insertions(+), 50 deletions(-) rename lib/node_modules/@stdlib/math/base/special/trunc/src/{trunc.c => main.c} (84%) diff --git a/lib/node_modules/@stdlib/math/base/special/trunc/README.md b/lib/node_modules/@stdlib/math/base/special/trunc/README.md index 4f3a42bdbd38..55241b739c8a 100644 --- a/lib/node_modules/@stdlib/math/base/special/trunc/README.md +++ b/lib/node_modules/@stdlib/math/base/special/trunc/README.md @@ -68,15 +68,13 @@ v = trunc( -Infinity ); ```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 ] ) ); } ``` diff --git a/lib/node_modules/@stdlib/math/base/special/trunc/benchmark/benchmark.js b/lib/node_modules/@stdlib/math/base/special/trunc/benchmark/benchmark.js index cfa72155db68..4a1edba65093 100644 --- a/lib/node_modules/@stdlib/math/base/special/trunc/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/math/base/special/trunc/benchmark/benchmark.js @@ -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' ); @@ -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' ); } @@ -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' ); } diff --git a/lib/node_modules/@stdlib/math/base/special/trunc/benchmark/benchmark.native.js b/lib/node_modules/@stdlib/math/base/special/trunc/benchmark/benchmark.native.js index 92558869a3b6..2666eb0540b8 100644 --- a/lib/node_modules/@stdlib/math/base/special/trunc/benchmark/benchmark.native.js +++ b/lib/node_modules/@stdlib/math/base/special/trunc/benchmark/benchmark.native.js @@ -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; @@ -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' ); } diff --git a/lib/node_modules/@stdlib/math/base/special/trunc/benchmark/c/benchmark.c b/lib/node_modules/@stdlib/math/base/special/trunc/benchmark/c/benchmark.c index d03ea36eae8b..944504b98141 100644 --- a/lib/node_modules/@stdlib/math/base/special/trunc/benchmark/c/benchmark.c +++ b/lib/node_modules/@stdlib/math/base/special/trunc/benchmark/c/benchmark.c @@ -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; diff --git a/lib/node_modules/@stdlib/math/base/special/trunc/benchmark/c/native/benchmark.c b/lib/node_modules/@stdlib/math/base/special/trunc/benchmark/c/native/benchmark.c index 1ef246f8df81..34539c26d9ff 100644 --- a/lib/node_modules/@stdlib/math/base/special/trunc/benchmark/c/native/benchmark.c +++ b/lib/node_modules/@stdlib/math/base/special/trunc/benchmark/c/native/benchmark.c @@ -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; diff --git a/lib/node_modules/@stdlib/math/base/special/trunc/examples/index.js b/lib/node_modules/@stdlib/math/base/special/trunc/examples/index.js index e9e99458f288..20893cb690f2 100644 --- a/lib/node_modules/@stdlib/math/base/special/trunc/examples/index.js +++ b/lib/node_modules/@stdlib/math/base/special/trunc/examples/index.js @@ -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 ] ) ); } diff --git a/lib/node_modules/@stdlib/math/base/special/trunc/manifest.json b/lib/node_modules/@stdlib/math/base/special/trunc/manifest.json index a0cd7b7a2c1a..fd6633bb95b7 100644 --- a/lib/node_modules/@stdlib/math/base/special/trunc/manifest.json +++ b/lib/node_modules/@stdlib/math/base/special/trunc/manifest.json @@ -30,7 +30,7 @@ "task": "build", "wasm": false, "src": [ - "./src/trunc.c" + "./src/main.c" ], "include": [ "./include" @@ -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" @@ -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" @@ -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" @@ -86,7 +94,10 @@ "-lm" ], "libpath": [], - "dependencies": [] + "dependencies": [ + "@stdlib/math/base/special/floor", + "@stdlib/math/base/special/ceil" + ] } ] } diff --git a/lib/node_modules/@stdlib/math/base/special/trunc/src/trunc.c b/lib/node_modules/@stdlib/math/base/special/trunc/src/main.c similarity index 84% rename from lib/node_modules/@stdlib/math/base/special/trunc/src/trunc.c rename to lib/node_modules/@stdlib/math/base/special/trunc/src/main.c index 4bf98676b940..cd8f400154e5 100644 --- a/lib/node_modules/@stdlib/math/base/special/trunc/src/trunc.c +++ b/lib/node_modules/@stdlib/math/base/special/trunc/src/main.c @@ -17,7 +17,8 @@ */ #include "stdlib/math/base/special/trunc.h" -#include +#include "stdlib/math/base/special/floor.h" +#include "stdlib/math/base/special/ceil.h" /** * Rounds a double-precision floating-point number toward zero. @@ -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 ); } diff --git a/lib/node_modules/@stdlib/math/base/special/trunc/test/test.js b/lib/node_modules/@stdlib/math/base/special/trunc/test/test.js index f0d3814e8dd6..2c7df5ea6e82 100644 --- a/lib/node_modules/@stdlib/math/base/special/trunc/test/test.js +++ b/lib/node_modules/@stdlib/math/base/special/trunc/test/test.js @@ -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(); }); diff --git a/lib/node_modules/@stdlib/math/base/special/trunc/test/test.native.js b/lib/node_modules/@stdlib/math/base/special/trunc/test/test.native.js index a8d6d07a02a7..3794b9c182dd 100644 --- a/lib/node_modules/@stdlib/math/base/special/trunc/test/test.native.js +++ b/lib/node_modules/@stdlib/math/base/special/trunc/test/test.native.js @@ -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(); });