Skip to content

Commit 2a10dde

Browse files
bench: update benchmarks and examples in math/base/special/ldexp
PR-URL: #2781 Reviewed-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com> Signed-off-by: Gunj Joshi <gunjjoshi8372@gmail.com> Co-authored-by: stdlib-bot <82920195+stdlib-bot@users.noreply.github.com>
1 parent 755b053 commit 2a10dde

File tree

9 files changed

+59
-99
lines changed

9 files changed

+59
-99
lines changed

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

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -182,44 +182,19 @@ double stdlib_base_ldexp( const double frac, const int32_t exp );
182182
183183
```c
184184
#include "stdlib/math/base/special/ldexp.h"
185-
#include "stdlib/math/base/special/frexp.h"
186-
#include <stdlib.h>
187185
#include <stdint.h>
188186
#include <stdio.h>
189-
#include <inttypes.h>
190-
#include <math.h>
191-
192-
static double rand_double() {
193-
int r = rand();
194-
return (double)r / ( (double)RAND_MAX + 1.0 );
195-
}
196187
197188
int main( void ) {
198-
double sign;
199-
double frac;
200-
int32_t exp;
201-
double x;
202-
double v;
189+
double y;
203190
int i;
204191
205-
for ( i = 0; i < 100; i++ ) {
206-
if ( rand_double() < 0.5 ) {
207-
sign = -1.0;
208-
} else {
209-
sign = 1.0;
210-
}
211-
// Generate a random number:
212-
frac = rand_double() * 10.0;
213-
exp = (int32_t)( rand_double()*616.0 ) - 308;
214-
x = sign * frac * pow( 10.0, exp );
215-
216-
// Break the number into a normalized fraction and an integer power of two:
217-
stdlib_base_frexp( x, &frac, &exp );
218-
219-
// Reconstitute the original number:
220-
v = stdlib_base_ldexp( frac, exp );
192+
const double frac[] = { 0.5, 5.0, 0.0, 3.5, 7.9 };
193+
const int32_t exp[] = { 3, -2, 20, 39, 14 };
221194
222-
printf( "%e = %lf * 2^%" PRId32 " = %e\n", x, frac, exp, v );
195+
for ( i = 0; i < 5; i++ ) {
196+
y = stdlib_base_ldexp( frac[ i ], exp[ i ] );
197+
printf( "ldexp(%lf, %d) = %lf\n", frac[ i ], exp[ i ], y );
223198
}
224199
}
225200
```

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

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

2323
var bench = require( '@stdlib/bench' );
24-
var randu = require( '@stdlib/random/base/randu' );
25-
var round = require( '@stdlib/math/base/special/round' );
24+
var uniform = require( '@stdlib/random/array/uniform' );
25+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
2626
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2727
var pkg = require( './../package.json' ).name;
2828
var ldexp = require( './../lib' );
@@ -36,11 +36,12 @@ bench( pkg, function benchmark( b ) {
3636
var z;
3737
var i;
3838

39+
x = uniform( 100, -10.0, 10.0 );
40+
y = discreteUniform( 100, -1020.0, 1020.0 );
41+
3942
b.tic();
4043
for ( i = 0; i < b.iterations; i++ ) {
41-
x = ( randu()*20.0 ) - 10.0;
42-
y = round( randu()*2040.0 ) - 1020.0;
43-
z = ldexp( x, y );
44+
z = ldexp( x[ i % x.length ], y[ i % y.length ] );
4445
if ( isnan( z ) ) {
4546
b.fail( 'should not return NaN' );
4647
}

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222

2323
var resolve = require( 'path' ).resolve;
2424
var bench = require( '@stdlib/bench' );
25-
var randu = require( '@stdlib/random/base/randu' );
26-
var round = require( '@stdlib/math/base/special/round' );
25+
var uniform = require( '@stdlib/random/array/uniform' );
26+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
2727
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2828
var tryRequire = require( '@stdlib/utils/try-require' );
2929
var pkg = require( './../package.json' ).name;
@@ -45,11 +45,12 @@ bench( pkg+'::native', opts, function benchmark( b ) {
4545
var z;
4646
var i;
4747

48+
x = uniform( 100, -10.0, 10.0 );
49+
y = discreteUniform( 100, -1020.0, 1020.0 );
50+
4851
b.tic();
4952
for ( i = 0; i < b.iterations; i++ ) {
50-
x = ( randu()*20.0 ) - 10.0;
51-
y = round( randu()*2040.0 ) - 1020.0;
52-
z = ldexp( x, y );
53+
z = ldexp( x[ i % x.length ], y[ i % y.length ] );
5354
if ( isnan( z ) ) {
5455
b.fail( 'should not return NaN' );
5556
}

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,20 @@ static double rand_double( void ) {
9090
*/
9191
static double benchmark( void ) {
9292
double elapsed;
93-
double x;
94-
double y;
93+
double x[ 100 ];
94+
double y[ 100 ];
9595
double z;
9696
double t;
9797
int i;
9898

99+
for ( i = 0; i < 100; i++ ) {
100+
x[ i ] = ( rand_double() * 20.0 ) - 10.0;
101+
y[ i ] = ( rand_double() * 2048.0 ) - 1024.0;
102+
}
103+
99104
t = tic();
100105
for ( i = 0; i < ITERATIONS; i++ ) {
101-
x = ( rand_double()*20.0 ) - 10.0;
102-
y = ( rand_double()*2048.0 ) - 1024.0;
103-
z = ldexp( x, y );
106+
z = ldexp( x[ i % 100 ], y[ i % 100 ] );
104107
if ( z != z ) {
105108
printf( "should not return NaN\n" );
106109
break;

lib/node_modules/@stdlib/math/base/special/ldexp/benchmark/c/cephes/benchmark.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,20 @@ static double rand_double( void ) {
9595
*/
9696
static double benchmark( void ) {
9797
double elapsed;
98-
double x;
99-
double y;
98+
double x[ 100 ];
99+
double y[ 100 ];
100100
double z;
101101
double t;
102102
int i;
103103

104+
for ( i = 0; i < 100; i++ ) {
105+
x[ i ] = ( rand_double() * 20.0 ) - 10.0;
106+
y[ i ] = ( rand_double() * 2048.0 ) - 1024.0;
107+
}
108+
104109
t = tic();
105110
for ( i = 0; i < ITERATIONS; i++ ) {
106-
x = ( rand_double()*20.0 ) - 10.0;
107-
y = ( rand_double()*2048.0 ) - 1024.0;
108-
z = ldexp( x, y );
111+
z = ldexp( x[ i % 100 ], y[ i % 100 ] );
109112
if ( z != z ) {
110113
printf( "should not return NaN\n" );
111114
break;

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

Lines changed: 8 additions & 5 deletions
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 ] = ( rand_double() * 20.0 ) - 10.0;
102+
y[ i ] = ( rand_double() * 2048.0 ) - 1024.0;
103+
}
104+
100105
t = tic();
101106
for ( i = 0; i < ITERATIONS; i++ ) {
102-
x = ( rand_double()*20.0 ) - 10.0;
103-
y = ( rand_double()*2048.0 ) - 1024.0;
104-
z = stdlib_base_ldexp( x, y );
107+
z = stdlib_base_ldexp( x[ i % 100 ], y[ i % 100 ] );
105108
if ( z != z ) {
106109
printf( "should not return NaN\n" );
107110
break;

lib/node_modules/@stdlib/math/base/special/ldexp/benchmark/cpp/benchmark.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,20 @@ double rand_double() {
9393
*/
9494
double benchmark() {
9595
double elapsed;
96-
double x;
97-
double y;
96+
double x[ 100 ];
97+
double y[ 100 ];
9898
double z;
9999
double t;
100100
int i;
101101

102+
for ( i = 0; i < 100; i++ ) {
103+
x[ i ] = ( rand_double() * 20.0 ) - 10.0;
104+
y[ i ] = ( rand_double() * 2048.0 ) - 1024.0;
105+
}
106+
102107
t = tic();
103108
for ( i = 0; i < ITERATIONS; i++ ) {
104-
x = ( rand_double()*20.0 ) - 10.0;
105-
y = ( rand_double()*2048.0 ) - 1024.0;
106-
z = ldexp( x, y );
109+
z = ldexp( x[ i % 100 ], y[ i % 100 ] );
107110
if ( z != z ) {
108111
printf( "should not return NaN\n" );
109112
break;

lib/node_modules/@stdlib/math/base/special/ldexp/examples/c/example.c

Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -16,47 +16,19 @@
1616
* limitations under the License.
1717
*/
1818

19-
2019
#include "stdlib/math/base/special/ldexp.h"
21-
#include "stdlib/math/base/special/frexp.h"
22-
#include <stdlib.h>
2320
#include <stdint.h>
2421
#include <stdio.h>
25-
#include <inttypes.h>
26-
#include <math.h>
27-
28-
// TODO: replace use of `pow` with stdlib_base_pow()
29-
30-
static double rand_double() {
31-
int r = rand();
32-
return (double)r / ( (double)RAND_MAX + 1.0 );
33-
}
3422

3523
int main( void ) {
36-
double sign;
37-
double frac;
38-
int32_t exp;
39-
double x;
40-
double v;
41-
int i;
42-
43-
for ( i = 0; i < 100; i++ ) {
44-
if ( rand_double() < 0.5 ) {
45-
sign = -1.0;
46-
} else {
47-
sign = 1.0;
48-
}
49-
// Generate a random number:
50-
frac = rand_double() * 10.0;
51-
exp = (int32_t)( rand_double()*616.0 ) - 308;
52-
x = sign * frac * pow( 10.0, exp );
53-
54-
// Break the number into a normalized fraction and an integer power of two:
55-
stdlib_base_frexp( x, &frac, &exp );
24+
double y;
25+
int i;
5626

57-
// Reconstitute the original number:
58-
v = stdlib_base_ldexp( frac, exp );
27+
const double frac[] = { 0.5, 5.0, 0.0, 3.5, 7.9 };
28+
const int32_t exp[] = { 3, -2, 20, 39, 14 };
5929

60-
printf( "%e = %lf * 2^%" PRId32 " = %e\n", x, frac, exp, v );
61-
}
30+
for ( i = 0; i < 5; i++ ) {
31+
y = stdlib_base_ldexp( frac[ i ], exp[ i ] );
32+
printf( "ldexp(%lf, %d) = %lf\n", frac[ i ], exp[ i ], y );
33+
}
6234
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@
8181
"@stdlib/constants/float64/min-base2-exponent-subnormal",
8282
"@stdlib/constants/float64/max-base2-exponent",
8383
"@stdlib/constants/float64/max-base2-exponent-subnormal",
84-
"@stdlib/constants/float64/exponent-bias",
85-
"@stdlib/math/base/special/frexp"
84+
"@stdlib/constants/float64/exponent-bias"
8685
]
8786
},
8887
{

0 commit comments

Comments
 (0)