Skip to content

Commit e82178c

Browse files
committed
refactor: replace built-ins by stdlib packages
1 parent d7f5b56 commit e82178c

File tree

10 files changed

+66
-66
lines changed

10 files changed

+66
-66
lines changed

lib/node_modules/@stdlib/math/base/special/trunc/README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,14 @@ v = trunc( -Infinity );
6868
<!-- eslint no-undef: "error" -->
6969

7070
```javascript
71-
var randu = require( '@stdlib/random/base/randu' );
71+
var randu = require( '@stdlib/random/array/uniform' );
7272
var trunc = require( '@stdlib/math/base/special/trunc' );
7373

74-
var x;
7574
var i;
75+
var x = randu( 100, -50.0, 50.0 );
7676

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

lib/node_modules/@stdlib/math/base/special/trunc/benchmark/benchmark.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
// MODULES //
2222

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

44+
x = randu( 100, -500.0, 500.0 );
45+
4446
b.tic();
4547
for ( i = 0; i < b.iterations; i++ ) {
46-
x = ( randu()*1000.0 ) - 500.0;
47-
y = trunc( x );
48+
y = trunc( x[ i % x.length ] );
4849
if ( isnan( y ) ) {
4950
b.fail( 'should not return NaN' );
5051
}
@@ -62,10 +63,11 @@ bench( pkg+'::built-in', opts, function benchmark( b ) {
6263
var y;
6364
var i;
6465

66+
x = randu( 100, -500.0, 500.0 );
67+
6568
b.tic();
6669
for ( i = 0; i < b.iterations; i++ ) {
67-
x = ( randu()*1000.0 ) - 500.0;
68-
y = Math.trunc( x ); // eslint-disable-line stdlib/no-builtin-math
70+
y = Math.trunc( x[ i % x.length ] ); // eslint-disable-line stdlib/no-builtin-math
6971
if ( isnan( y ) ) {
7072
b.fail( 'should not return NaN' );
7173
}

lib/node_modules/@stdlib/math/base/special/trunc/benchmark/benchmark.native.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

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

46+
x = randu( 100, -500.0, 500.0 );
47+
4648
b.tic();
4749
for ( i = 0; i < b.iterations; i++ ) {
48-
x = ( randu()*1000.0 ) - 500.0;
49-
y = trunc( x );
50+
y = trunc( x[ i % x.length ] );
5051
if ( isnan( y ) ) {
5152
b.fail( 'should not return NaN' );
5253
}

lib/node_modules/@stdlib/math/base/special/trunc/benchmark/c/benchmark.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,19 @@ static double rand_double( void ) {
8989
* @return elapsed time in seconds
9090
*/
9191
static double benchmark( void ) {
92+
double x[ 100 ];
9293
double elapsed;
93-
double x;
9494
double y;
9595
double t;
9696
int i;
9797

98+
for ( i = 0; i < 100; i++ ) {
99+
x[ i ] = ( 1000.0 * rand_double() ) - 500.0;
100+
}
101+
98102
t = tic();
99103
for ( i = 0; i < ITERATIONS; i++ ) {
100-
x = ( 1000.0*rand_double() ) - 500.0;
101-
y = trunc( x );
104+
y = trunc( x[ i % 100 ] );
102105
if ( y != y ) {
103106
printf( "should not return NaN\n" );
104107
break;

lib/node_modules/@stdlib/math/base/special/trunc/benchmark/c/native/benchmark.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,19 @@ static double rand_double( void ) {
9090
* @return elapsed time in seconds
9191
*/
9292
static double benchmark( void ) {
93+
double x[ 100 ];
9394
double elapsed;
94-
double x;
9595
double y;
9696
double t;
9797
int i;
9898

99+
for ( i = 0; i < 100; i++ ) {
100+
x[ i ] = ( 1000.0 * rand_double() ) - 500.0;
101+
}
102+
99103
t = tic();
100104
for ( i = 0; i < ITERATIONS; i++ ) {
101-
x = ( 1000.0*rand_double() ) - 500.0;
102-
y = stdlib_base_trunc( x );
105+
y = stdlib_base_trunc( x[ i % 100 ] );
103106
if ( y != y ) {
104107
printf( "should not return NaN\n" );
105108
break;

lib/node_modules/@stdlib/math/base/special/trunc/examples/index.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,12 @@
1818

1919
'use strict';
2020

21-
var randu = require( '@stdlib/random/base/randu' );
21+
var uniform = require( '@stdlib/random/array/uniform' );
2222
var trunc = require( './../lib' );
2323

24-
var x;
2524
var i;
25+
var x = uniform( 100, -50.0, 50.0 );
2626

27-
for ( i = 0; i < 100; i++ ) {
28-
x = (randu()*100.0) - 50.0;
29-
console.log( 'trunc(%d) = %d', x, trunc( x ) );
27+
for ( i = 0; i < x.length; i++ ) {
28+
console.log( 'trunc(%d) = %d', x[ i ], trunc( x[ i ] ) );
3029
}
Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
22
"options": {
3-
"task": "build",
4-
"wasm": false
3+
"task": "build"
54
},
65
"fields": [
76
{
@@ -28,9 +27,8 @@
2827
"confs": [
2928
{
3029
"task": "build",
31-
"wasm": false,
3230
"src": [
33-
"./src/trunc.c"
31+
"./src/main.c"
3432
],
3533
"include": [
3634
"./include"
@@ -40,14 +38,15 @@
4038
],
4139
"libpath": [],
4240
"dependencies": [
43-
"@stdlib/math/base/napi/unary"
41+
"@stdlib/math/base/napi/unary",
42+
"@stdlib/math/base/special/ceil",
43+
"@stdlib/math/base/special/floor"
4444
]
4545
},
4646
{
4747
"task": "benchmark",
48-
"wasm": false,
4948
"src": [
50-
"./src/trunc.c"
49+
"./src/main.c"
5150
],
5251
"include": [
5352
"./include"
@@ -56,28 +55,15 @@
5655
"-lm"
5756
],
5857
"libpath": [],
59-
"dependencies": []
58+
"dependencies": [
59+
"@stdlib/math/base/special/ceil",
60+
"@stdlib/math/base/special/floor"
61+
]
6062
},
6163
{
6264
"task": "examples",
63-
"wasm": false,
64-
"src": [
65-
"./src/trunc.c"
66-
],
67-
"include": [
68-
"./include"
69-
],
70-
"libraries": [
71-
"-lm"
72-
],
73-
"libpath": [],
74-
"dependencies": []
75-
},
76-
{
77-
"task": "build",
78-
"wasm": true,
7965
"src": [
80-
"./src/trunc.c"
66+
"./src/main.c"
8167
],
8268
"include": [
8369
"./include"
@@ -86,7 +72,10 @@
8672
"-lm"
8773
],
8874
"libpath": [],
89-
"dependencies": []
75+
"dependencies": [
76+
"@stdlib/math/base/special/ceil",
77+
"@stdlib/math/base/special/floor"
78+
]
9079
}
9180
]
9281
}

lib/node_modules/@stdlib/math/base/special/trunc/src/trunc.c renamed to lib/node_modules/@stdlib/math/base/special/trunc/src/main.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
*/
1818

1919
#include "stdlib/math/base/special/trunc.h"
20-
#include <math.h>
20+
#include "stdlib/math/base/special/floor.h"
21+
#include "stdlib/math/base/special/ceil.h"
2122

2223
/**
2324
* Rounds a double-precision floating-point number toward zero.
@@ -30,5 +31,8 @@
3031
* // returns 3.0
3132
*/
3233
double stdlib_base_trunc( const double x ) {
33-
return trunc( x );
34+
if ( x < 0.0 ) {
35+
return stdlib_base_ceil( x );
36+
}
37+
return stdlib_base_floor( x );
3438
}

lib/node_modules/@stdlib/math/base/special/trunc/test/test.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,37 +38,37 @@ tape( 'main export is a function', function test( t ) {
3838
});
3939

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

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

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

5858
tape( 'the function returns `NaN` if provided `NaN`', function test( t ) {
5959
var v = trunc( NaN );
60-
t.strictEqual( isnan( v ), true, 'returns NaN' );
60+
t.strictEqual( isnan( v ), true, 'returns expected value' );
6161
t.end();
6262
});
6363

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

7070
tape( 'the function returns `-infinity` if provided `-infinity`', function test( t ) {
7171
var v = trunc( NINF );
72-
t.strictEqual( v, NINF, 'returns -infinity' );
72+
t.strictEqual( v, NINF, 'returns expected value' );
7373
t.end();
7474
});

lib/node_modules/@stdlib/math/base/special/trunc/test/test.native.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,37 +47,37 @@ tape( 'main export is a function', opts, function test( t ) {
4747
});
4848

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

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

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

6767
tape( 'the function returns `NaN` if provided `NaN`', opts, function test( t ) {
6868
var v = trunc( NaN );
69-
t.strictEqual( isnan( v ), true, 'returns NaN' );
69+
t.strictEqual( isnan( v ), true, 'returns expected value' );
7070
t.end();
7171
});
7272

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

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

0 commit comments

Comments
 (0)