Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ int64_t shape[] = { 4 };
int64_t strides[] = { STDLIB_NDARRAY_FLOAT32_BYTES_PER_ELEMENT };
int8_t submodes[] = { STDLIB_NDARRAY_INDEX_ERROR };

struct ndarray *x = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT32, (uint8_t *)data, 1, shape, strides, 0, STDLIB_NDARRAY_ROW_MAJOR, STDLIB_NDARRAY_INDEX_ERROR, 1, submodes );
struct ndarray *x = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT32, (void *)data, 1, shape, strides, 0, STDLIB_NDARRAY_ROW_MAJOR, STDLIB_NDARRAY_INDEX_ERROR, 1, submodes );

// Compute the range:
const struct ndarray *arrays[] = { x };
Expand Down Expand Up @@ -225,7 +225,7 @@ int main( void ) {
const int64_t nsubmodes = 1;

// Create an ndarray:
struct ndarray *x = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT32, (uint8_t *)data, ndims, shape, strides, offset, order, imode, nsubmodes, submodes );
struct ndarray *x = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT32, (void *)data, ndims, shape, strides, offset, order, imode, nsubmodes, submodes );
if ( x == NULL ) {
fprintf( stderr, "Error allocating memory.\n" );
exit( 1 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var uniform = require( '@stdlib/random/base/uniform' );
var bernoulli = require( '@stdlib/random/base/bernoulli' );
var fillBy = require( '@stdlib/ndarray/fill-by' );
var zeros = require( '@stdlib/ndarray/zeros' );
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pow = require( '@stdlib/math/base/special/pow' );
var format = require( '@stdlib/string/format' );
var tryRequire = require( '@stdlib/utils/try-require' );
Expand Down Expand Up @@ -83,12 +83,12 @@ function createBenchmark( len ) {
b.tic();
for ( i = 0; i < b.iterations; i++ ) {
v = snanrange( [ x ] );
if ( isnanf( v ) ) {
if ( isnan( v ) ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( isnanf( v ) ) {
if ( isnan( v ) ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ static void print_version( void ) {
*/
static void print_summary( int total, int passing ) {
printf( "#\n" );
printf( "1..%d\n", total ); // TAP plan
printf( "1..%d\n", total ); // TAP plan
printf( "# total %d\n", total );
printf( "# pass %d\n", passing );
printf( "#\n" );
Expand All @@ -63,8 +63,8 @@ static void print_summary( int total, int passing ) {
* @param iterations number of iterations
* @param elapsed elapsed time in seconds
*/
static void print_results( int iterations, double elapsed ) {
double rate = (double)iterations / elapsed;
static void print_results( int iterations, float elapsed ) {
float rate = (float)iterations / elapsed;
printf( " ---\n" );
printf( " iterations: %d\n", iterations );
printf( " elapsed: %0.9f\n", elapsed );
Expand All @@ -77,10 +77,10 @@ static void print_results( int iterations, double elapsed ) {
*
* @return clock time
*/
static double tic( void ) {
static float tic( void ) {
struct timeval now;
gettimeofday( &now, NULL );
return (double)now.tv_sec + (double)now.tv_usec/1.0e6;
return (float)now.tv_sec + (float)now.tv_usec/1.0e6;
}

/**
Expand All @@ -90,19 +90,7 @@ static double tic( void ) {
*/
static float rand_float( void ) {
int r = rand();
return (float)r / ( (float)RAND_MAX + 1.0f );
}

/**
* Returns a random value or NaN.
*
* @return random value or NaN
*/
static float rand_value( void ) {
if ( rand_float() < 0.8f ) {
return NAN;
}
return ( rand_float() * 20.0f ) - 10.0f;
return (float)r / ( (float)RAND_MAX + 1.0 );
}

/**
Expand All @@ -112,7 +100,7 @@ static float rand_value( void ) {
* @param len array length
* @return elapsed time in seconds
*/
static double benchmark( int iterations, int len ) {
static float benchmark( int iterations, int len ) {
enum STDLIB_NDARRAY_INDEX_MODE imode;
const struct ndarray *arrays[ 1 ];
enum STDLIB_NDARRAY_ORDER order;
Expand All @@ -122,11 +110,11 @@ static double benchmark( int iterations, int len ) {
int64_t nsubmodes;
struct ndarray *x;
int64_t offset;
double elapsed;
float elapsed;
int64_t ndims;
float *data;
float v;
double t;
float t;
int i;

ndims = 1;
Expand All @@ -140,13 +128,13 @@ static double benchmark( int iterations, int len ) {

data = (float *) malloc( len * sizeof( float ) );
for ( i = 0; i < len; i++ ) {
data[ i ] = rand_value();
data[ i ] = ( rand_float() * 20000.0 ) - 10000.0;
}
// cppcheck-suppress invalidPointerCast
x = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT32, (uint8_t *)data, ndims, shape, strides, offset, order, imode, nsubmodes, submodes );
x = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT32, (void *)data, ndims, shape, strides, offset, order, imode, nsubmodes, submodes );
arrays[ 0 ] = x;

v = 0.0f;
v = 0.0;
t = tic();
for ( i = 0; i < iterations; i++ ) {
v = stdlib_stats_snanrange( arrays );
Expand All @@ -170,7 +158,7 @@ static double benchmark( int iterations, int len ) {
* Main execution sequence.
*/
int main( void ) {
double elapsed;
float elapsed;
int count;
int iter;
int len;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>

int main( void ) {
// Create a data buffer:
const float data[] = { 1.0f, NAN, 3.0f, -4.0f, 5.0f, -6.0f, 7.0f, -8.0f };
const float data[] = { 1.0, -2.0, 3.0, -4.0, 5.0, 0.0/0.0, 7.0, -8.0 };

// Specify the number of array dimensions:
const int64_t ndims = 1;
Expand All @@ -55,7 +54,7 @@ int main( void ) {

// Create an ndarray:
// cppcheck-suppress invalidPointerCast
struct ndarray *x = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT32, (uint8_t *)data, ndims, shape, strides, offset, order, imode, nsubmodes, submodes );
struct ndarray *x = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT32, (void *)data, ndims, shape, strides, offset, order, imode, nsubmodes, submodes );
if ( x == NULL ) {
fprintf( stderr, "Error allocating memory.\n" );
exit( 1 );
Expand All @@ -68,7 +67,7 @@ int main( void ) {
float v = stdlib_stats_snanrange( arrays );

// Print the result:
printf( "range: %f\n", v );
printf( "range: %lf\n", v );

// Free allocated memory:
stdlib_ndarray_free( x );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ extern "C" {
#endif

/**
* Computes the range of a one-dimensional single-precision floating-point ndarray, ignoring NaN values.
* Computes the range of a one-dimensional single-precision floating-point ndarray, ignoring `NaN` values.
*/
float stdlib_stats_snanrange( const struct ndarray *arrays[] );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var addon = require( './../src/addon.node' );
// MAIN //

/**
* Computes the range of a one-dimensional single-precision floating-point ndarray, ignoring NaN values.
* Computes the range of a one-dimensional single-precision floating-point ndarray, ignoring `NaN` values.
*
* ## Notes
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"task": "build",
"wasm": false,
"src": [
"./src/main.c"
"./src/snanrange.c"
],
"include": [
"./include"
Expand All @@ -51,7 +51,7 @@
"task": "benchmark",
"wasm": false,
"src": [
"./src/main.c"
"./src/snanrange.c"
],
"include": [
"./include"
Expand All @@ -72,7 +72,7 @@
"task": "examples",
"wasm": false,
"src": [
"./src/main.c"
"./src/snanrange.c"
],
"include": [
"./include"
Expand All @@ -93,7 +93,7 @@
"task": "",
"wasm": true,
"src": [
"./src/main.c"
"./src/snanrange.c"
],
"include": [
"./include"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,16 @@
"test": "./test"
},
"types": "./docs/types",
"scripts": {},
"scripts": {
"benchmark": "make benchmark",
"benchmark:c": "make benchmark-c",
"benchmark:native": "make benchmark-native",
"examples:c": "make examples-c",
"test": "make test",
"test:c": "make test-c",
"test:native": "make test-native",
"test-cov": "make test-cov"
},
"homepage": "https://github.com/stdlib-js/stdlib",
"repository": {
"type": "git",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ static napi_value addon( napi_env env, napi_callback_info info ) {
arrays[ 0 ]
};
// Perform computation:
STDLIB_NAPI_CREATE_DOUBLE( env, (double)stdlib_stats_snanrange( arr ), v );
STDLIB_NAPI_CREATE_DOUBLE( env, stdlib_stats_snanrange( arr ), v );

// Free allocated memory:
stdlib_ndarray_free( arrays[ 0 ] );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include "stdlib/blas/base/shared.h"

/**
* Computes the range of a one-dimensional single-precision floating-point ndarray, ignoring NaN values.
* Computes the range of a one-dimensional single-precision floating-point ndarray, ignoring `NaN` values.
*
* @param arrays list containing an input ndarray
* @return range
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@

var resolve = require( 'path' ).resolve;
var tape = require( 'tape' );
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
var isPositiveZerof = require( '@stdlib/math/base/assert/is-positive-zerof' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' );
var Float32Array = require( '@stdlib/array/float32' );
var ndarray = require( '@stdlib/ndarray/base/ctor' );
var tryRequire = require( '@stdlib/utils/try-require' );
Expand Down Expand Up @@ -81,15 +81,15 @@ tape( 'the function calculates the range of a one-dimensional ndarray, ignoring

x = new Float32Array( [ -0.0, 0.0, NaN, -0.0 ] );
v = snanrange( [ vector( x, 4, 1, 0 ) ] );
t.strictEqual( isPositiveZerof( v ), true, 'returns expected value' );
t.strictEqual( isPositiveZero( v ), true, 'returns expected value' );

x = new Float32Array( [ NaN ] );
v = snanrange( [ vector( x, 1, 1, 0 ) ] );
t.strictEqual( isnanf( v ), true, 'returns expected value' );
t.strictEqual( isnan( v ), true, 'returns expected value' );

x = new Float32Array( [ NaN, NaN ] );
v = snanrange( [ vector( x, 2, 1, 0 ) ] );
t.strictEqual( isnanf( v ), true, 'returns expected value' );
t.strictEqual( isnan( v ), true, 'returns expected value' );

t.end();
});
Expand All @@ -101,7 +101,7 @@ tape( 'if provided an empty ndarray, the function returns `NaN`', opts, function
x = new Float32Array( [] );

v = snanrange( [ vector( x, 0, 1, 0 ) ] );
t.strictEqual( isnanf( v ), true, 'returns expected value' );
t.strictEqual( isnan( v ), true, 'returns expected value' );

t.end();
});
Expand Down