Skip to content

Commit b8286e1

Browse files
authored
bench: updates random value generation and docs
PR-URL: #6658 Reviewed-by: Athan Reines <[email protected]>
1 parent 323d3ba commit b8286e1

File tree

12 files changed

+90
-72
lines changed

12 files changed

+90
-72
lines changed

Diff for: lib/node_modules/@stdlib/math/base/special/fast/hypot/README.md

+8-12
Original file line numberDiff line numberDiff line change
@@ -86,21 +86,17 @@ var h = hypot( -5.0, 12.0 );
8686
<!-- eslint no-undef: "error" -->
8787

8888
```javascript
89-
var randu = require( '@stdlib/random/base/randu' );
90-
var round = require( '@stdlib/math/base/special/round' );
89+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
90+
var logEachMap = require( '@stdlib/console/log-each-map' );
9191
var hypot = require( '@stdlib/math/base/special/fast/hypot' );
9292
93-
var x;
94-
var y;
95-
var h;
96-
var i;
93+
var opts = {
94+
'dtype': 'float64'
95+
};
96+
var x = discreteUniform( 100, -50, 50, opts );
97+
var y = discreteUniform( 100, -50, 50, opts );
9798
98-
for ( i = 0; i < 100; i++ ) {
99-
x = round( randu()*100.0 ) - 50.0;
100-
y = round( randu()*100.0 ) - 50.0;
101-
h = hypot( x, y );
102-
console.log( 'hypot(%d,%d) = %d', x, y, h );
103-
}
99+
logEachMap( 'hypot(%d,%d) = %0.4f', x, y, hypot );
104100
```
105101

106102
</section>

Diff for: lib/node_modules/@stdlib/math/base/special/fast/hypot/benchmark/benchmark.js

+9-4
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 uniform = require( '@stdlib/random/array/uniform' );
2525
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2626
var pkg = require( './../package.json' ).name;
2727
var hypot = require( './../lib' );
@@ -30,16 +30,21 @@ var hypot = require( './../lib' );
3030
// MAIN //
3131

3232
bench( pkg, function benchmark( b ) {
33+
var opts;
3334
var x;
3435
var y;
3536
var h;
3637
var i;
3738

39+
opts = {
40+
'dtype': 'float64'
41+
};
42+
x = uniform( 100, -50.0, 50.0, opts );
43+
y = uniform( 100, -50.0, 50.0, opts );
44+
3845
b.tic();
3946
for ( i = 0; i < b.iterations; i++ ) {
40-
x = ( randu()*100.0 ) - 50.0;
41-
y = ( randu()*100.0 ) - 50.0;
42-
h = hypot( x, y );
47+
h = hypot( x[ i%x.length ], y[ i%y.length ] );
4348
if ( isnan( h ) ) {
4449
b.fail( 'should not return NaN' );
4550
}

Diff for: lib/node_modules/@stdlib/math/base/special/fast/hypot/benchmark/benchmark.native.js

+9-4
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 uniform = 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;
@@ -39,16 +39,21 @@ var opts = {
3939
// MAIN //
4040

4141
bench( pkg+'::native', opts, function benchmark( b ) {
42+
var opts;
4243
var x;
4344
var y;
4445
var h;
4546
var i;
4647

48+
opts = {
49+
'dtype': 'float64'
50+
};
51+
x = uniform( 100, -50.0, 50.0, opts );
52+
y = uniform( 100, -50.0, 50.0, opts );
53+
4754
b.tic();
4855
for ( i = 0; i < b.iterations; i++ ) {
49-
x = ( randu() * 100.0 ) - 50.0;
50-
y = ( randu() * 100.0 ) - 50.0;
51-
h = hypot( x, y );
56+
h = hypot( x[ i%x.length ], y[ i%y.length ] );
5257
if ( isnan( h ) ) {
5358
b.fail( 'should not return NaN' );
5459
}

Diff for: lib/node_modules/@stdlib/math/base/special/fast/hypot/benchmark/c/native/benchmark.c

+8-5
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,20 @@ static double rand_double( void ) {
9191
*/
9292
static double benchmark( void ) {
9393
double elapsed;
94-
double x;
95-
double y;
94+
double x[ 100 ];
95+
double y[ 100 ];
9696
double z;
9797
double t;
9898
int i;
9999

100+
for ( i = 0; i < 100; i++ ) {
101+
x[ i ] = ( 100.0 * rand_double() ) - 50.0;
102+
y[ i ] = ( 100.0 * rand_double() ) - 50.0;
103+
}
104+
100105
t = tic();
101106
for ( i = 0; i < ITERATIONS; i++ ) {
102-
x = ( 100.0 * rand_double() ) - 50.0;
103-
y = ( 100.0 * rand_double() ) - 50.0;
104-
z = stdlib_base_fast_hypot( x, y );
107+
z = stdlib_base_fast_hypot( x[ i%100 ], y[ i%100 ] );
105108
if ( z != z ) {
106109
printf( "should not return NaN\n" );
107110
break;

Diff for: lib/node_modules/@stdlib/math/base/special/fast/hypot/examples/index.js

+8-12
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,14 @@
1818

1919
'use strict';
2020

21-
var randu = require( '@stdlib/random/base/randu' );
22-
var round = require( '@stdlib/math/base/special/round' );
21+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
22+
var logEachMap = require( '@stdlib/console/log-each-map' );
2323
var hypot = require( './../lib' );
2424

25-
var x;
26-
var y;
27-
var h;
28-
var i;
25+
var opts = {
26+
'dtype': 'float64'
27+
};
28+
var x = discreteUniform( 100, -50, 50, opts );
29+
var y = discreteUniform( 100, -50, 50, opts );
2930

30-
for ( i = 0; i < 100; i++ ) {
31-
x = round( randu()*100.0 ) - 50.0;
32-
y = round( randu()*100.0 ) - 50.0;
33-
h = hypot( x, y );
34-
console.log( 'hypot(%d,%d) = %d', x, y, h );
35-
}
31+
logEachMap( 'hypot(%d,%d) = %0.4f', x, y, hypot );

Diff for: lib/node_modules/@stdlib/math/base/special/fast/hypot/test/test.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -70,25 +70,25 @@ tape( 'the function computes the hypotenuse (canonical inputs)', function test(
7070
var h;
7171

7272
h = hypot( 3.0, 4.0 );
73-
t.strictEqual( h, 5.0, 'returns 5.0' );
73+
t.strictEqual( h, 5.0, 'returns expected value' );
7474

7575
h = hypot( 6.0, 8.0 );
76-
t.strictEqual( h, 10.0, 'returns 10.0' );
76+
t.strictEqual( h, 10.0, 'returns expected value' );
7777

7878
h = hypot( 5.0, 12.0 );
79-
t.strictEqual( h, 13.0, 'returns 13.0' );
79+
t.strictEqual( h, 13.0, 'returns expected value' );
8080

8181
t.end();
8282
});
8383

8484
tape( 'the function can overflow', function test( t ) {
8585
var h = hypot( 1.0e308, 1.0e308 );
86-
t.strictEqual( h, PINF, 'overflows' );
86+
t.strictEqual( h, PINF, 'returns expected value' );
8787
t.end();
8888
});
8989

9090
tape( 'the function can underflow', function test( t ) {
9191
var h = hypot( 1.0e-200, 1.0e-200 );
92-
t.strictEqual( h, 0.0, 'underflows' );
92+
t.strictEqual( h, 0.0, 'returns expected value' );
9393
t.end();
9494
});

Diff for: lib/node_modules/@stdlib/math/base/special/fast/hypot/test/test.native.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -79,25 +79,25 @@ tape( 'the function computes the hypotenuse (canonical inputs)', opts, function
7979
var h;
8080

8181
h = hypot( 3.0, 4.0 );
82-
t.strictEqual( h, 5.0, 'returns 5.0' );
82+
t.strictEqual( h, 5.0, 'returns expected value' );
8383

8484
h = hypot( 6.0, 8.0 );
85-
t.strictEqual( h, 10.0, 'returns 10.0' );
85+
t.strictEqual( h, 10.0, 'returns expected value' );
8686

8787
h = hypot( 5.0, 12.0 );
88-
t.strictEqual( h, 13.0, 'returns 13.0' );
88+
t.strictEqual( h, 13.0, 'returns expected value' );
8989

9090
t.end();
9191
});
9292

9393
tape( 'the function can overflow', opts, function test( t ) {
9494
var h = hypot( 1.0e308, 1.0e308 );
95-
t.strictEqual( h, PINF, 'overflows' );
95+
t.strictEqual( h, PINF, 'returns expected value' );
9696
t.end();
9797
});
9898

9999
tape( 'the function can underflow', opts, function test( t ) {
100100
var h = hypot( 1.0e-200, 1.0e-200 );
101-
t.strictEqual( h, 0.0, 'underflows' );
101+
t.strictEqual( h, 0.0, 'returns expected value' );
102102
t.end();
103103
});

Diff for: lib/node_modules/@stdlib/math/base/special/fast/pow-int/benchmark/benchmark.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,31 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var randu = require( '@stdlib/random/base/randu' );
24+
var uniform = require( '@stdlib/random/array/uniform' );
25+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
2526
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26-
var floor = require( '@stdlib/math/base/special/floor' );
2727
var pkg = require( './../package.json' ).name;
2828
var pow = require( './../lib' );
2929

3030

3131
// MAIN //
3232

3333
bench( pkg, function benchmark( b ) {
34+
var opts;
3435
var x;
3536
var y;
3637
var z;
3738
var i;
3839

40+
opts = {
41+
'dtype': 'float64'
42+
};
43+
x = uniform( 100, -50.0, 50.0, opts );
44+
y = discreteUniform( 100, -50.0, 50.0, opts );
45+
3946
b.tic();
4047
for ( i = 0; i < b.iterations; i++ ) {
41-
x = ( randu()*100.0 ) - 50.0;
42-
y = floor( randu()*100.0 ) - 50;
43-
z = pow( x, y );
48+
z = pow( x[ i%x.length ], y[ i%y.length ] );
4449
if ( isnan( z ) ) {
4550
b.fail( 'should not return NaN' );
4651
}

Diff for: lib/node_modules/@stdlib/math/base/special/fast/pow-int/benchmark/benchmark.native.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222

2323
var resolve = require( 'path' ).resolve;
2424
var bench = require( '@stdlib/bench' );
25-
var randu = require( '@stdlib/random/base/randu' );
25+
var uniform = require( '@stdlib/random/array/uniform' );
26+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
2627
var isnan = require( '@stdlib/math/base/assert/is-nan' );
27-
var floor = require( '@stdlib/math/base/special/floor' );
2828
var tryRequire = require( '@stdlib/utils/try-require' );
2929
var pkg = require( './../package.json' ).name;
3030

@@ -40,16 +40,21 @@ var opts = {
4040
// MAIN //
4141

4242
bench( pkg+'::native', opts, function benchmark( b ) {
43+
var opts;
4344
var x;
4445
var y;
4546
var z;
4647
var i;
4748

49+
opts = {
50+
'dtype': 'float64'
51+
};
52+
x = uniform( 100, -50.0, 50.0, opts );
53+
y = discreteUniform( 100, -50.0, 50.0, opts );
54+
4855
b.tic();
4956
for ( i = 0; i < b.iterations; i++ ) {
50-
x = ( randu() * 100.0 ) - 50.0;
51-
y = floor( randu() * 100.0 ) - 50;
52-
z = pow( x, y );
57+
z = pow( x[ i%x.length ], y[ i%y.length ] );
5358
if ( isnan( z ) ) {
5459
b.fail( 'should not return NaN' );
5560
}

Diff for: lib/node_modules/@stdlib/math/base/special/fast/pow-int/benchmark/c/native/benchmark.c

+8-5
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,20 @@ static double rand_double( void ) {
9191
*/
9292
static double benchmark( void ) {
9393
double elapsed;
94-
int32_t x;
94+
int32_t x[ 100 ];
95+
double y[ 100 ];
9596
double t;
96-
double y;
9797
double z;
9898
int i;
9999

100+
for ( i = 0; i < 100; i++ ) {
101+
x[ i ] = ( 100.0 * rand_double() ) - 50.0;
102+
y[ i ] = floor( 100.0 * rand_double() ) - 50.0;
103+
}
104+
100105
t = tic();
101106
for ( i = 0; i < ITERATIONS; i++ ) {
102-
x = ( 100.0 * rand_double() ) - 50.00;
103-
y = floor( 100.0 * rand_double() ) - 50.00;
104-
z = stdlib_base_fast_pow( x, y );
107+
z = stdlib_base_fast_pow( x[ i%100 ], y[ i%100 ] );
105108
if ( z != z ) {
106109
printf( "should not return NaN\n" );
107110
break;

Diff for: lib/node_modules/@stdlib/math/base/special/fast/pow-int/test/test.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,13 @@ tape( 'the function returns `NaN` if provided `NaN` for the base', function test
137137
var v;
138138

139139
v = pow( NaN, 5 );
140-
t.strictEqual( isnan( v ), true, 'returns NaN' );
140+
t.strictEqual( isnan( v ), true, 'returns expected value' );
141141

142142
v = pow( NaN, 1 );
143-
t.strictEqual( isnan( v ), true, 'returns NaN' );
143+
t.strictEqual( isnan( v ), true, 'returns expected value' );
144144

145145
v = pow( NaN, 0 );
146-
t.strictEqual( isnan( v ), true, 'returns NaN' );
146+
t.strictEqual( isnan( v ), true, 'returns expected value' );
147147

148148
t.end();
149149
});
@@ -295,7 +295,7 @@ tape( 'the function returns `0` if `+infinity` is raised to any negative finite
295295
for ( i = 0; i < 100; i++ ) {
296296
y = -round( randu()*1.0e5 ) - 1;
297297
v = pow( PINF, y );
298-
t.strictEqual( isPositiveZero( v ), true, 'returns 0' );
298+
t.strictEqual( isPositiveZero( v ), true, 'returns expected value' );
299299
}
300300
t.end();
301301
});
@@ -307,7 +307,7 @@ tape( 'the function returns `+infinity` if `+infinity` is raised to any positive
307307
for ( i = 0; i < 100; i++ ) {
308308
y = round( randu()*1.0e5 ) + 1;
309309
v = pow( PINF, y );
310-
t.strictEqual( v, PINF, 'returns +infinity' );
310+
t.strictEqual( v, PINF, 'returns expected value' );
311311
}
312312
t.end();
313313
});

Diff for: lib/node_modules/@stdlib/math/base/special/fast/pow-int/test/test.native.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,13 @@ tape( 'the function returns `NaN` if provided `NaN` for the base', opts, functio
146146
var v;
147147

148148
v = pow( NaN, 5 );
149-
t.strictEqual( isnan( v ), true, 'returns NaN' );
149+
t.strictEqual( isnan( v ), true, 'returns expected value' );
150150

151151
v = pow( NaN, 1 );
152-
t.strictEqual( isnan( v ), true, 'returns NaN' );
152+
t.strictEqual( isnan( v ), true, 'returns expected value' );
153153

154154
v = pow( NaN, 0 );
155-
t.strictEqual( isnan( v ), true, 'returns NaN' );
155+
t.strictEqual( isnan( v ), true, 'returns expected value' );
156156

157157
t.end();
158158
});
@@ -304,7 +304,7 @@ tape( 'the function returns `0` if `+infinity` is raised to any negative finite
304304
for ( i = 0; i < 100; i++ ) {
305305
y = -round( randu() * 1.0e5 ) - 1;
306306
v = pow( PINF, y );
307-
t.strictEqual( isPositiveZero( v ), true, 'returns 0' );
307+
t.strictEqual( isPositiveZero( v ), true, 'returns expected value' );
308308
}
309309
t.end();
310310
});
@@ -316,7 +316,7 @@ tape( 'the function returns `+infinity` if `+infinity` is raised to any positive
316316
for ( i = 0; i < 100; i++ ) {
317317
y = round( randu() * 1.0e5 ) + 1;
318318
v = pow( PINF, y );
319-
t.strictEqual( v, PINF, 'returns +infinity' );
319+
t.strictEqual( v, PINF, 'returns expected value' );
320320
}
321321
t.end();
322322
});

0 commit comments

Comments
 (0)