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 Dec 18, 2024
1 parent a0431d0 commit eda178f
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 50 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2741,6 +2741,7 @@ A total of 5 people contributed to this release. Thank you to the following cont

<details>

- [`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)_
Expand Down
10 changes: 4 additions & 6 deletions 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
12 changes: 7 additions & 5 deletions base/special/trunc/benchmark/benchmark.js
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( './../../../../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
7 changes: 4 additions & 3 deletions base/special/trunc/benchmark/benchmark.native.js
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( './../../../../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
9 changes: 6 additions & 3 deletions base/special/trunc/benchmark/c/benchmark.c
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
9 changes: 6 additions & 3 deletions base/special/trunc/benchmark/c/native/benchmark.c
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
10 changes: 4 additions & 6 deletions base/special/trunc/examples/index.js
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 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 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();
});
14 changes: 7 additions & 7 deletions base/special/trunc/test/test.native.js
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();
});

0 comments on commit eda178f

Please sign in to comment.