Skip to content

Commit cbf9861

Browse files
refactor: update math/base/assert/is-probability to follow latest project conventions
PR-URL: #4700 Reviewed-by: Philipp Burckhardt <[email protected]>
1 parent 2693663 commit cbf9861

File tree

9 files changed

+98
-98
lines changed

9 files changed

+98
-98
lines changed

lib/node_modules/@stdlib/math/base/assert/is-probability/README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,20 @@ bool = isProbability( NaN );
5656
<!-- eslint no-undef: "error" -->
5757

5858
```javascript
59-
var randu = require( '@stdlib/random/base/randu' );
59+
var uniform = require( '@stdlib/random/array/uniform' );
6060
var isProbability = require( '@stdlib/math/base/assert/is-probability' );
6161

6262
var bool;
63+
var len;
6364
var x;
6465
var i;
6566

67+
len = 100;
68+
x = uniform( len, -1.0, 1.0 );
69+
6670
for ( i = 0; i < 100; i++ ) {
67-
x = ( randu()*2.0 ) - 1.0;
68-
bool = isProbability( x );
69-
console.log( '%d is %s', x, ( bool ) ? 'a probability' : 'not a probability' );
71+
bool = isProbability( x[ i ] );
72+
console.log( '%d is %s', x[ i ], ( bool ) ? 'a probability' : 'not a probability' );
7073
}
7174
```
7275

lib/node_modules/@stdlib/math/base/assert/is-probability/benchmark/benchmark.js

Lines changed: 6 additions & 3 deletions
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 isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
2626
var pkg = require( './../package.json' ).name;
2727
var isProbability = require( './../lib' );
@@ -30,14 +30,17 @@ var isProbability = require( './../lib' );
3030
// MAIN //
3131

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

38+
len = 100;
39+
x = uniform( len, 1.0, -1.0 );
40+
3741
b.tic();
3842
for ( i = 0; i < b.iterations; i++ ) {
39-
x = ( randu()*2.0 ) - 0.0;
40-
y = isProbability( x );
43+
y = isProbability( x[ i % len ] );
4144
if ( typeof y !== 'boolean' ) {
4245
b.fail( 'should return a boolean' );
4346
}

lib/node_modules/@stdlib/math/base/assert/is-probability/benchmark/benchmark.native.js

Lines changed: 6 additions & 3 deletions
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 isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
2727
var tryRequire = require( '@stdlib/utils/try-require' );
2828
var pkg = require( './../package.json' ).name;
@@ -39,14 +39,17 @@ var opts = {
3939
// MAIN //
4040

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

47+
len = 100;
48+
x = uniform( len, -1.0, 1.0 );
49+
4650
b.tic();
4751
for ( i = 0; i < b.iterations; i++ ) {
48-
x = ( randu()*2.0 ) - 0.0;
49-
y = isProbability( x );
52+
y = isProbability( x[ i % len ] );
5053
if ( typeof y !== 'boolean' ) {
5154
b.fail( 'should return a boolean' );
5255
}

lib/node_modules/@stdlib/math/base/assert/is-probability/benchmark/c/native/benchmark.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,18 @@ static double rand_double( void ) {
9292
*/
9393
static double benchmark( void ) {
9494
double elapsed;
95-
double x;
95+
double x[ 100 ];
9696
double t;
9797
bool b;
9898
int i;
9999

100+
for ( i = 0; i < 100; i++ ) {
101+
x[ i ] = ( rand_double() * 2.0 ) - 1.0;;
102+
}
103+
100104
t = tic();
101105
for ( i = 0; i < ITERATIONS; i++ ) {
102-
x = ( rand_double() * 2.0 ) - 0.0;
103-
b = stdlib_base_is_probability( x );
106+
b = stdlib_base_is_probability( x[ i % 100 ] );
104107
if ( b != true && b != false ) {
105108
printf( "should return either true or false\n" );
106109
break;

lib/node_modules/@stdlib/math/base/assert/is-probability/examples/index.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,18 @@
1818

1919
'use strict';
2020

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

2424
var bool;
25+
var len;
2526
var x;
2627
var i;
2728

29+
len = 100;
30+
x = uniform( len, -1.0, 1.0 );
31+
2832
for ( i = 0; i < 100; i++ ) {
29-
x = ( randu()*2.0 ) - 1.0;
30-
bool = isProbability( x );
31-
console.log( '%d is %s', x, ( bool ) ? 'a probability' : 'not a probability' );
33+
bool = isProbability( x[ i ] );
34+
console.log( '%d is %s', x[ i ], ( bool ) ? 'a probability' : 'not a probability' );
3235
}

lib/node_modules/@stdlib/math/base/assert/is-probability/manifest.json

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
{
2-
"options": {},
2+
"options": {
3+
"task": "build"
4+
},
35
"fields": [
46
{
57
"field": "src",
@@ -24,6 +26,36 @@
2426
],
2527
"confs": [
2628
{
29+
"task": "build",
30+
"src": [
31+
"./src/main.c"
32+
],
33+
"include": [
34+
"./include"
35+
],
36+
"libraries": [],
37+
"libpath": [],
38+
"dependencies": [
39+
"@stdlib/napi/argv",
40+
"@stdlib/napi/argv-double",
41+
"@stdlib/napi/create-int32",
42+
"@stdlib/napi/export"
43+
]
44+
},
45+
{
46+
"task": "benchmark",
47+
"src": [
48+
"./src/main.c"
49+
],
50+
"include": [
51+
"./include"
52+
],
53+
"libraries": [],
54+
"libpath": [],
55+
"dependencies": []
56+
},
57+
{
58+
"task": "examples",
2759
"src": [
2860
"./src/main.c"
2961
],

lib/node_modules/@stdlib/math/base/assert/is-probability/src/addon.c

Lines changed: 9 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@
1717
*/
1818

1919
#include "stdlib/math/base/assert/is_probability.h"
20+
#include "stdlib/napi/argv.h"
21+
#include "stdlib/napi/argv_double.h"
22+
#include "stdlib/napi/create_int32.h"
23+
#include "stdlib/napi/export.h"
2024
#include <node_api.h>
2125
#include <stdint.h>
22-
#include <assert.h>
2326

2427
/**
2528
* Receives JavaScript callback invocation data.
@@ -29,60 +32,10 @@
2932
* @return Node-API value
3033
*/
3134
static napi_value addon( napi_env env, napi_callback_info info ) {
32-
napi_status status;
33-
34-
// Get callback arguments:
35-
size_t argc = 1;
36-
napi_value argv[ 1 ];
37-
status = napi_get_cb_info( env, info, &argc, argv, NULL, NULL );
38-
assert( status == napi_ok );
39-
40-
// Check whether we were provided the correct number of arguments:
41-
if ( argc < 1 ) {
42-
status = napi_throw_error( env, NULL, "invalid invocation. Insufficient arguments." );
43-
assert( status == napi_ok );
44-
return NULL;
45-
}
46-
if ( argc > 1 ) {
47-
status = napi_throw_error( env, NULL, "invalid invocation. Too many arguments." );
48-
assert( status == napi_ok );
49-
return NULL;
50-
}
51-
52-
napi_valuetype vtype0;
53-
status = napi_typeof( env, argv[ 0 ], &vtype0 );
54-
assert( status == napi_ok );
55-
if ( vtype0 != napi_number ) {
56-
status = napi_throw_type_error( env, NULL, "invalid argument. First argument must be a number." );
57-
assert( status == napi_ok );
58-
return NULL;
59-
}
60-
61-
double x;
62-
status = napi_get_value_double( env, argv[ 0 ], &x );
63-
assert( status == napi_ok );
64-
65-
bool result = stdlib_base_is_probability( x );
66-
67-
napi_value v;
68-
status = napi_create_int32( env, (int32_t)result, &v );
69-
assert( status == napi_ok );
70-
71-
return v;
72-
}
73-
74-
/**
75-
* Initializes a Node-API module.
76-
*
77-
* @param env environment under which the function is invoked
78-
* @param exports exports object
79-
* @return main export
80-
*/
81-
static napi_value init( napi_env env, napi_value exports ) {
82-
napi_value fcn;
83-
napi_status status = napi_create_function( env, "exports", NAPI_AUTO_LENGTH, addon, NULL, &fcn );
84-
assert( status == napi_ok );
85-
return fcn;
35+
STDLIB_NAPI_ARGV( env, info, argv, argc, 1 );
36+
STDLIB_NAPI_ARGV_DOUBLE( env, x, argv, 0 );
37+
STDLIB_NAPI_CREATE_INT32( env, (int32_t)stdlib_base_is_probability( x ), out );
38+
return out;
8639
}
8740

88-
NAPI_MODULE( NODE_GYP_MODULE_NAME, init )
41+
STDLIB_NAPI_MODULE_EXPORT_FCN( addon )

lib/node_modules/@stdlib/math/base/assert/is-probability/test/test.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ tape( 'the function returns `false` if provided a number less than `0.0`', funct
4040
var x;
4141
var i;
4242
for ( i = 0; i < 1000; i++ ) {
43-
x = -1.0 - ( randu()*1000.0 );
43+
x = -1.0 - ( randu() * 1000.0 );
4444
bool = isProbability( x );
45-
t.equal( bool, false, 'returns false when provided '+x );
45+
t.equal( bool, false, 'returns expected value when provided '+x );
4646
}
4747
t.end();
4848
});
@@ -52,9 +52,9 @@ tape( 'the function returns `false` if provided a number greater than `1.0`', fu
5252
var x;
5353
var i;
5454
for ( i = 0; i < 1000; i++ ) {
55-
x = ( randu()*1000.0 ) + 2.0;
55+
x = ( randu() * 1000.0 ) + 2.0;
5656
bool = isProbability( x );
57-
t.equal( bool, false, 'returns false when provided '+x );
57+
t.equal( bool, false, 'returns expected value when provided '+x );
5858
}
5959
t.end();
6060
});
@@ -66,34 +66,34 @@ tape( 'the function returns `true` if provided a probability', function test( t
6666
for ( i = 0; i < 1000; i++ ) {
6767
x = randu();
6868
bool = isProbability( x );
69-
t.equal( bool, true, 'returns true when provided '+x );
69+
t.equal( bool, true, 'returns expected value when provided '+x );
7070
}
7171
t.end();
7272
});
7373

7474
tape( 'the function returns `true` if provided `+-0`', function test( t ) {
75-
t.equal( isProbability( 0.0 ), true, 'returns true' );
76-
t.equal( isProbability( -0.0 ), true, 'returns true' );
75+
t.equal( isProbability( 0.0 ), true, 'returns expected value' );
76+
t.equal( isProbability( -0.0 ), true, 'returns expected value' );
7777
t.end();
7878
});
7979

8080
tape( 'the function returns `true` if provided `1`', function test( t ) {
81-
t.equal( isProbability( 1.0 ), true, 'returns true' );
81+
t.equal( isProbability( 1.0 ), true, 'returns expected value' );
8282
t.end();
8383
});
8484

8585
tape( 'the function returns `false` if provided `+infinity`', function test( t ) {
86-
t.equal( isProbability( PINF ), false, 'returns false' );
86+
t.equal( isProbability( PINF ), false, 'returns expected value' );
8787
t.end();
8888
});
8989

9090
tape( 'the function returns `false` if provided `-infinity`', function test( t ) {
91-
t.equal( isProbability( NINF ), false, 'returns false' );
91+
t.equal( isProbability( NINF ), false, 'returns expected value' );
9292
t.end();
9393
});
9494

9595
tape( 'the function returns `false` if provided `NaN`', function test( t ) {
96-
t.equal( isProbability( NaN ), false, 'returns false' );
97-
t.equal( isProbability( 0.0/0.0 ), false, 'returns false' );
96+
t.equal( isProbability( NaN ), false, 'returns expected value' );
97+
t.equal( isProbability( 0.0 / 0.0 ), false, 'returns expected value' );
9898
t.end();
9999
});

lib/node_modules/@stdlib/math/base/assert/is-probability/test/test.native.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ tape( 'the function returns `false` if provided a number less than `0.0`', opts,
4949
var x;
5050
var i;
5151
for ( i = 0; i < 1000; i++ ) {
52-
x = -1.0 - ( randu()*1000.0 );
52+
x = -1.0 - ( randu() * 1000.0 );
5353
bool = isProbability( x );
54-
t.equal( bool, false, 'returns false when provided '+x );
54+
t.equal( bool, false, 'returns expected value when provided '+x );
5555
}
5656
t.end();
5757
});
@@ -61,9 +61,9 @@ tape( 'the function returns `false` if provided a number greater than `1.0`', op
6161
var x;
6262
var i;
6363
for ( i = 0; i < 1000; i++ ) {
64-
x = ( randu()*1000.0 ) + 2.0;
64+
x = ( randu() * 1000.0 ) + 2.0;
6565
bool = isProbability( x );
66-
t.equal( bool, false, 'returns false when provided '+x );
66+
t.equal( bool, false, 'returns expected value when provided '+x );
6767
}
6868
t.end();
6969
});
@@ -75,34 +75,34 @@ tape( 'the function returns `true` if provided a probability', opts, function te
7575
for ( i = 0; i < 1000; i++ ) {
7676
x = randu();
7777
bool = isProbability( x );
78-
t.equal( bool, true, 'returns true when provided '+x );
78+
t.equal( bool, true, 'returns expected value when provided '+x );
7979
}
8080
t.end();
8181
});
8282

8383
tape( 'the function returns `true` if provided `+-0`', opts, function test( t ) {
84-
t.equal( isProbability( 0.0 ), true, 'returns true' );
85-
t.equal( isProbability( -0.0 ), true, 'returns true' );
84+
t.equal( isProbability( 0.0 ), true, 'returns expected value' );
85+
t.equal( isProbability( -0.0 ), true, 'returns expected value' );
8686
t.end();
8787
});
8888

8989
tape( 'the function returns `true` if provided `1`', opts, function test( t ) {
90-
t.equal( isProbability( 1.0 ), true, 'returns true' );
90+
t.equal( isProbability( 1.0 ), true, 'returns expected value' );
9191
t.end();
9292
});
9393

9494
tape( 'the function returns `false` if provided `+infinity`', opts, function test( t ) {
95-
t.equal( isProbability( PINF ), false, 'returns false' );
95+
t.equal( isProbability( PINF ), false, 'returns expected value' );
9696
t.end();
9797
});
9898

9999
tape( 'the function returns `false` if provided `-infinity`', opts, function test( t ) {
100-
t.equal( isProbability( NINF ), false, 'returns false' );
100+
t.equal( isProbability( NINF ), false, 'returns expected value' );
101101
t.end();
102102
});
103103

104104
tape( 'the function returns `false` if provided `NaN`', opts, function test( t ) {
105-
t.equal( isProbability( NaN ), false, 'returns false' );
106-
t.equal( isProbability( 0.0/0.0 ), false, 'returns false' );
105+
t.equal( isProbability( NaN ), false, 'returns expected value' );
106+
t.equal( isProbability( 0.0 / 0.0 ), false, 'returns expected value' );
107107
t.end();
108108
});

0 commit comments

Comments
 (0)