From eda178f5484dee7b2cd051204b509b0f408207f6 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Wed, 18 Dec 2024 14:35:28 +0000 Subject: [PATCH] Auto-generated commit --- CHANGELOG.md | 1 + base/special/trunc/README.md | 10 +++---- base/special/trunc/benchmark/benchmark.js | 12 +++++---- .../trunc/benchmark/benchmark.native.js | 7 ++--- base/special/trunc/benchmark/c/benchmark.c | 9 ++++--- .../trunc/benchmark/c/native/benchmark.c | 9 ++++--- base/special/trunc/examples/index.js | 10 +++---- base/special/trunc/manifest.json | 27 +++++++++++++------ base/special/trunc/src/{trunc.c => main.c} | 8 ++++-- base/special/trunc/test/test.js | 14 +++++----- base/special/trunc/test/test.native.js | 14 +++++----- 11 files changed, 71 insertions(+), 50 deletions(-) rename base/special/trunc/src/{trunc.c => main.c} (84%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 271fdf9fb..273900816 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2741,6 +2741,7 @@ A total of 5 people contributed to this release. Thank you to the following cont
+- [`8785e54`](https://github.com/stdlib-js/stdlib/commit/8785e54ec82782d7d01912988596c9d7d2bf06d0) - **refactor:** replace built-ins by stdlib packages, update benchmarks in `math/base/special/trunc` [(#3941)](https://github.com/stdlib-js/stdlib/pull/3941) _(by Gunj Joshi, Athan Reines)_ - [`db76062`](https://github.com/stdlib-js/stdlib/commit/db76062ddee3a358ddb2ac1dc725610516b2891f) - **docs:** update function descriptions and move link [(#4015)](https://github.com/stdlib-js/stdlib/pull/4015) _(by Gunj Joshi)_ - [`a1b543a`](https://github.com/stdlib-js/stdlib/commit/a1b543a2a1bdf4d1fb9438bd4a13cb971af62063) - **docs:** update related packages sections [(#4009)](https://github.com/stdlib-js/stdlib/pull/4009) _(by stdlib-bot, Philipp Burckhardt)_ - [`66b4609`](https://github.com/stdlib-js/stdlib/commit/66b4609cee9e428f80bc78da231d2be8e7b223cc) - **refactor:** use inbuilt macro instead of new variable for `MAX_SAFE_NTH_LUCAS` [(#3980)](https://github.com/stdlib-js/stdlib/pull/3980) _(by Aayush Khanna)_ diff --git a/base/special/trunc/README.md b/base/special/trunc/README.md index 7b064dba9..f04f37d25 100644 --- a/base/special/trunc/README.md +++ b/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/base/special/trunc/benchmark/benchmark.js b/base/special/trunc/benchmark/benchmark.js index 8fe5771fe..f4e1ee6f6 100644 --- a/base/special/trunc/benchmark/benchmark.js +++ b/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( './../../../../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/base/special/trunc/benchmark/benchmark.native.js b/base/special/trunc/benchmark/benchmark.native.js index e58c7216a..3c8e349f3 100644 --- a/base/special/trunc/benchmark/benchmark.native.js +++ b/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( './../../../../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/base/special/trunc/benchmark/c/benchmark.c b/base/special/trunc/benchmark/c/benchmark.c index d03ea36ea..944504b98 100644 --- a/base/special/trunc/benchmark/c/benchmark.c +++ b/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/base/special/trunc/benchmark/c/native/benchmark.c b/base/special/trunc/benchmark/c/native/benchmark.c index 1ef246f8d..34539c26d 100644 --- a/base/special/trunc/benchmark/c/native/benchmark.c +++ b/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/base/special/trunc/examples/index.js b/base/special/trunc/examples/index.js index e9e99458f..20893cb69 100644 --- a/base/special/trunc/examples/index.js +++ b/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/base/special/trunc/manifest.json b/base/special/trunc/manifest.json index a0cd7b7a2..fd6633bb9 100644 --- a/base/special/trunc/manifest.json +++ b/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/base/special/trunc/src/trunc.c b/base/special/trunc/src/main.c similarity index 84% rename from base/special/trunc/src/trunc.c rename to base/special/trunc/src/main.c index 4bf98676b..cd8f40015 100644 --- a/base/special/trunc/src/trunc.c +++ b/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/base/special/trunc/test/test.js b/base/special/trunc/test/test.js index 6d54f324b..ca9fb2fe9 100644 --- a/base/special/trunc/test/test.js +++ b/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/base/special/trunc/test/test.native.js b/base/special/trunc/test/test.native.js index 77d63f087..1a7c1dbe4 100644 --- a/base/special/trunc/test/test.native.js +++ b/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(); });