Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bench: update benchmarks and examples in math/base/special/ldexp #2781

Merged
merged 20 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
37 changes: 6 additions & 31 deletions lib/node_modules/@stdlib/math/base/special/ldexp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,44 +182,19 @@ double stdlib_base_ldexp( const double frac, const int32_t exp );

```c
#include "stdlib/math/base/special/ldexp.h"
#include "stdlib/math/base/special/frexp.h"
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <inttypes.h>
#include <math.h>

static double rand_double() {
int r = rand();
return (double)r / ( (double)RAND_MAX + 1.0 );
}

int main( void ) {
double sign;
double frac;
int32_t exp;
double x;
double v;
double y;
int i;

for ( i = 0; i < 100; i++ ) {
if ( rand_double() < 0.5 ) {
sign = -1.0;
} else {
sign = 1.0;
}
// Generate a random number:
frac = rand_double() * 10.0;
exp = (int32_t)( rand_double()*616.0 ) - 308;
x = sign * frac * pow( 10.0, exp );

// Break the number into a normalized fraction and an integer power of two:
stdlib_base_frexp( x, &frac, &exp );

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

printf( "%e = %lf * 2^%" PRId32 " = %e\n", x, frac, exp, v );
for ( i = 0; i < 5; i++ ) {
y = stdlib_base_ldexp( frac[ i ], exp[ i ] );
printf( "ldexp(%lf, %d) = %lf\n", frac[ i ], exp[ i ], y );
}
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
// MODULES //

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

x = uniform( 100, -10.0, 10.0 );
y = discreteUniform( 100, -1020.0, 1020.0 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = ( randu()*20.0 ) - 10.0;
y = round( randu()*2040.0 ) - 1020.0;
z = ldexp( x, y );
z = ldexp( x[ i % x.length ], y[ i % y.length ] );
if ( isnan( z ) ) {
b.fail( 'should not return NaN' );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@

var resolve = require( 'path' ).resolve;
var bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var round = require( '@stdlib/math/base/special/round' );
var uniform = require( '@stdlib/random/array/uniform' );
var discreteUniform = require( '@stdlib/random/array/discrete-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 @@ -45,11 +45,12 @@ bench( pkg+'::native', opts, function benchmark( b ) {
var z;
var i;

x = uniform( 100, -10.0, 10.0 );
y = discreteUniform( 100, -1020.0, 1020.0 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = ( randu()*20.0 ) - 10.0;
y = round( randu()*2040.0 ) - 1020.0;
z = ldexp( x, y );
z = ldexp( x[ i % x.length ], y[ i % y.length ] );
if ( isnan( z ) ) {
b.fail( 'should not return NaN' );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,20 @@ static double rand_double( void ) {
*/
static double benchmark( void ) {
double elapsed;
double x;
double y;
double x[ 100 ];
double y[ 100 ];
double z;
double t;
int i;

for ( i = 0; i < 100; i++ ) {
x[ i ] = ( rand_double() * 20.0 ) - 10.0;
y[ i ] = ( rand_double() * 2048.0 ) - 1024.0;
}

t = tic();
for ( i = 0; i < ITERATIONS; i++ ) {
x = ( rand_double()*20.0 ) - 10.0;
y = ( rand_double()*2048.0 ) - 1024.0;
z = ldexp( x, y );
z = ldexp( x[ i % 100 ], y[ i % 100 ] );
if ( z != z ) {
printf( "should not return NaN\n" );
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,20 @@ static double rand_double( void ) {
*/
static double benchmark( void ) {
double elapsed;
double x;
double y;
double x[ 100 ];
double y[ 100 ];
double z;
double t;
int i;

for ( i = 0; i < 100; i++ ) {
x[ i ] = ( rand_double() * 20.0 ) - 10.0;
y[ i ] = ( rand_double() * 2048.0 ) - 1024.0;
}

t = tic();
for ( i = 0; i < ITERATIONS; i++ ) {
x = ( rand_double()*20.0 ) - 10.0;
y = ( rand_double()*2048.0 ) - 1024.0;
z = ldexp( x, y );
z = ldexp( x[ i % 100 ], y[ i % 100 ] );
if ( z != z ) {
printf( "should not return NaN\n" );
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,20 @@ static double rand_double( void ) {
*/
static double benchmark( void ) {
double elapsed;
double x;
double y;
double x[ 100 ];
double y[ 100 ];
double z;
double t;
int i;

for ( i = 0; i < 100; i++ ) {
x[ i ] = ( rand_double() * 20.0 ) - 10.0;
y[ i ] = ( rand_double() * 2048.0 ) - 1024.0;
}

t = tic();
for ( i = 0; i < ITERATIONS; i++ ) {
x = ( rand_double()*20.0 ) - 10.0;
y = ( rand_double()*2048.0 ) - 1024.0;
z = stdlib_base_ldexp( x, y );
z = stdlib_base_ldexp( x[ i % 100 ], y[ i % 100 ] );
if ( z != z ) {
printf( "should not return NaN\n" );
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,20 @@ double rand_double() {
*/
double benchmark() {
double elapsed;
double x;
double y;
double x[ 100 ];
double y[ 100 ];
double z;
double t;
int i;

for ( i = 0; i < 100; i++ ) {
x[ i ] = ( rand_double() * 20.0 ) - 10.0;
y[ i ] = ( rand_double() * 2048.0 ) - 1024.0;
}

t = tic();
for ( i = 0; i < ITERATIONS; i++ ) {
x = ( rand_double()*20.0 ) - 10.0;
y = ( rand_double()*2048.0 ) - 1024.0;
z = ldexp( x, y );
z = ldexp( x[ i % 100 ], y[ i % 100 ] );
if ( z != z ) {
printf( "should not return NaN\n" );
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,47 +16,19 @@
* limitations under the License.
*/


#include "stdlib/math/base/special/ldexp.h"
#include "stdlib/math/base/special/frexp.h"
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <inttypes.h>
#include <math.h>

// TODO: replace use of `pow` with stdlib_base_pow()

static double rand_double() {
int r = rand();
return (double)r / ( (double)RAND_MAX + 1.0 );
}

int main( void ) {
double sign;
double frac;
int32_t exp;
double x;
double v;
int i;

for ( i = 0; i < 100; i++ ) {
if ( rand_double() < 0.5 ) {
sign = -1.0;
} else {
sign = 1.0;
}
// Generate a random number:
frac = rand_double() * 10.0;
exp = (int32_t)( rand_double()*616.0 ) - 308;
x = sign * frac * pow( 10.0, exp );

// Break the number into a normalized fraction and an integer power of two:
stdlib_base_frexp( x, &frac, &exp );
double y;
int i;

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

printf( "%e = %lf * 2^%" PRId32 " = %e\n", x, frac, exp, v );
}
for ( i = 0; i < 5; i++ ) {
y = stdlib_base_ldexp( frac[ i ], exp[ i ] );
printf( "ldexp(%lf, %d) = %lf\n", frac[ i ], exp[ i ], y );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@
"@stdlib/constants/float64/min-base2-exponent-subnormal",
"@stdlib/constants/float64/max-base2-exponent",
"@stdlib/constants/float64/max-base2-exponent-subnormal",
"@stdlib/constants/float64/exponent-bias",
"@stdlib/math/base/special/frexp"
"@stdlib/constants/float64/exponent-bias"
]
},
{
Expand Down
Loading