Skip to content

Commit c60ec02

Browse files
gunjjoshiNeerajpathak07
authored andcommitted
refactor: replace built-ins by stdlib packages, update benchmarks in math/base/special/trunc
PR-URL: #3941 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Reviewed-by: Philipp Burckhardt <[email protected]> Signed-off-by: Athan Reines <[email protected]> Signed-off-by: Gunj Joshi <[email protected]> --- type: pre_push_report description: Results of running various checks prior to pushing changes. report: - task: run_javascript_examples status: na - task: run_c_examples status: na - task: run_cpp_examples status: na - task: run_javascript_readme_examples status: na - task: run_c_benchmarks status: na - task: run_cpp_benchmarks status: na - task: run_fortran_benchmarks status: na - task: run_javascript_benchmarks status: na - task: run_julia_benchmarks status: na - task: run_python_benchmarks status: na - task: run_r_benchmarks status: na - task: run_javascript_tests status: na ---
1 parent 0dc20b3 commit c60ec02

File tree

10 files changed

+70
-50
lines changed

10 files changed

+70
-50
lines changed

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

+4-6
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,13 @@ 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;
74+
var x = randu( 100, -50.0, 50.0 );
7575
var i;
76-
77-
for ( i = 0; i < 100; i++ ) {
78-
x = (randu()*100.0) - 50.0;
79-
console.log( 'trunc(%d) = %d', x, trunc( x ) );
76+
for ( i = 0; i < x.length; i++ ) {
77+
console.log( 'trunc(%d) = %d', x[ i ], trunc( x[ i ] ) );
8078
}
8179
```
8280

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

+7-5
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

+4-3
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

+6-3
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

+6-3
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

+4-6
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@
1818

1919
'use strict';
2020

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

24-
var x;
24+
var x = randu( 100, -50.0, 50.0 );
2525
var i;
26-
27-
for ( i = 0; i < 100; i++ ) {
28-
x = (randu()*100.0) - 50.0;
29-
console.log( 'trunc(%d) = %d', x, trunc( x ) );
26+
for ( i = 0; i < x.length; i++ ) {
27+
console.log( 'trunc(%d) = %d', x[ i ], trunc( x[ i ] ) );
3028
}

lib/node_modules/@stdlib/math/base/special/trunc/manifest.json

+19-8
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"task": "build",
3131
"wasm": false,
3232
"src": [
33-
"./src/trunc.c"
33+
"./src/main.c"
3434
],
3535
"include": [
3636
"./include"
@@ -40,14 +40,16 @@
4040
],
4141
"libpath": [],
4242
"dependencies": [
43-
"@stdlib/math/base/napi/unary"
43+
"@stdlib/math/base/napi/unary",
44+
"@stdlib/math/base/special/floor",
45+
"@stdlib/math/base/special/ceil"
4446
]
4547
},
4648
{
4749
"task": "benchmark",
4850
"wasm": false,
4951
"src": [
50-
"./src/trunc.c"
52+
"./src/main.c"
5153
],
5254
"include": [
5355
"./include"
@@ -56,13 +58,16 @@
5658
"-lm"
5759
],
5860
"libpath": [],
59-
"dependencies": []
61+
"dependencies": [
62+
"@stdlib/math/base/special/floor",
63+
"@stdlib/math/base/special/ceil"
64+
]
6065
},
6166
{
6267
"task": "examples",
6368
"wasm": false,
6469
"src": [
65-
"./src/trunc.c"
70+
"./src/main.c"
6671
],
6772
"include": [
6873
"./include"
@@ -71,13 +76,16 @@
7176
"-lm"
7277
],
7378
"libpath": [],
74-
"dependencies": []
79+
"dependencies": [
80+
"@stdlib/math/base/special/floor",
81+
"@stdlib/math/base/special/ceil"
82+
]
7583
},
7684
{
7785
"task": "build",
7886
"wasm": true,
7987
"src": [
80-
"./src/trunc.c"
88+
"./src/main.c"
8189
],
8290
"include": [
8391
"./include"
@@ -86,7 +94,10 @@
8694
"-lm"
8795
],
8896
"libpath": [],
89-
"dependencies": []
97+
"dependencies": [
98+
"@stdlib/math/base/special/floor",
99+
"@stdlib/math/base/special/ceil"
100+
]
90101
}
91102
]
92103
}

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

+6-2
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

+7-7
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

+7-7
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)