Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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 ] ) );
}
41 changes: 15 additions & 26 deletions lib/node_modules/@stdlib/math/base/special/trunc/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"options": {
"task": "build",
"wasm": false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gunjjoshi Don't delete the wasm fields or configuration. These were added intentionally in preparation for future WebAssembly builds.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, sorry for that, I've reverted the changes, thanks!

"task": "build"
},
"fields": [
{
Expand All @@ -28,9 +27,8 @@
"confs": [
{
"task": "build",
"wasm": false,
"src": [
"./src/trunc.c"
"./src/main.c"
],
"include": [
"./include"
Expand All @@ -40,14 +38,15 @@
],
"libpath": [],
"dependencies": [
"@stdlib/math/base/napi/unary"
"@stdlib/math/base/napi/unary",
"@stdlib/math/base/special/ceil",
"@stdlib/math/base/special/floor"
]
},
{
"task": "benchmark",
"wasm": false,
"src": [
"./src/trunc.c"
"./src/main.c"
],
"include": [
"./include"
Expand All @@ -56,28 +55,15 @@
"-lm"
],
"libpath": [],
"dependencies": []
"dependencies": [
"@stdlib/math/base/special/ceil",
"@stdlib/math/base/special/floor"
]
},
{
"task": "examples",
"wasm": false,
"src": [
"./src/trunc.c"
],
"include": [
"./include"
],
"libraries": [
"-lm"
],
"libpath": [],
"dependencies": []
},
{
"task": "build",
"wasm": true,
"src": [
"./src/trunc.c"
"./src/main.c"
],
"include": [
"./include"
Expand All @@ -86,7 +72,10 @@
"-lm"
],
"libpath": [],
"dependencies": []
"dependencies": [
"@stdlib/math/base/special/ceil",
"@stdlib/math/base/special/floor"
]
}
]
}
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