Skip to content

Commit 0900838

Browse files
refactor: update math/base/special/hypot to follow latest project conventions
PR-URL: #4765 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Gunj Joshi <gunjjoshi8372@gmail.com>
1 parent 73a211c commit 0900838

File tree

9 files changed

+92
-85
lines changed

9 files changed

+92
-85
lines changed

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

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -119,20 +119,16 @@ h = hypot( 5.0, NaN );
119119
<!-- eslint no-undef: "error" -->
120120

121121
```javascript
122-
var randu = require( '@stdlib/random/base/randu' );
123-
var round = require( '@stdlib/math/base/special/round' );
122+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
124123
var hypot = require( '@stdlib/math/base/special/hypot' );
125124
126-
var x;
127-
var y;
128-
var h;
129-
var i;
125+
var len = 100;
126+
var x = discreteUniform( len, -50, 50 );
127+
var y = discreteUniform( len, -50, 50 );
130128
131-
for ( i = 0; i < 100; i++ ) {
132-
x = round( randu()*100.0 ) - 50.0;
133-
y = round( randu()*100.0 ) - 50.0;
134-
h = hypot( x, y );
135-
console.log( 'h(%d,%d) = %d', x, y, h );
129+
var i;
130+
for ( i = 0; i < len; i++ ) {
131+
console.log( 'h(%d,%d) = %d', x[ i ], y[ i ], hypot( x[ i ], y[ i ] ) );
136132
}
137133
```
138134

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

Lines changed: 13 additions & 7 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' );
2524
var isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var uniform = require( '@stdlib/random/array/uniform' );
2626
var pkg = require( './../package.json' ).name;
2727
var hypot = require( './../lib' );
2828

@@ -37,16 +37,19 @@ var opts = {
3737
// MAIN //
3838

3939
bench( pkg, function benchmark( b ) {
40+
var len;
4041
var x;
4142
var y;
4243
var z;
4344
var i;
4445

46+
len = 100;
47+
x = uniform( len, -50, 50 );
48+
y = uniform( len, -50, 50 );
49+
4550
b.tic();
4651
for ( i = 0; i < b.iterations; i++ ) {
47-
x = ( randu()*100.0 ) - 50.0;
48-
y = ( randu()*100.0 ) - 50.0;
49-
z = hypot( x, y );
52+
z = hypot( x[ i % len ], y[ i % len ] );
5053
if ( isnan( z ) ) {
5154
b.fail( 'should not return NaN' );
5255
}
@@ -60,16 +63,19 @@ bench( pkg, function benchmark( b ) {
6063
});
6164

6265
bench( pkg+'::built-in', opts, function benchmark( b ) {
66+
var len;
6367
var x;
6468
var y;
6569
var z;
6670
var i;
6771

72+
len = 100;
73+
x = uniform( len, -50, 50 );
74+
y = uniform( len, -50, 50 );
75+
6876
b.tic();
6977
for ( i = 0; i < b.iterations; i++ ) {
70-
x = ( randu()*100.0 ) - 50.0;
71-
y = ( randu()*100.0 ) - 50.0;
72-
z = Math.hypot( x, y ); // eslint-disable-line stdlib/no-builtin-math
78+
z = Math.hypot( x[ i % len ], y[ i % len ] ); // eslint-disable-line stdlib/no-builtin-math
7379
if ( isnan( z ) ) {
7480
b.fail( 'should not return NaN' );
7581
}

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

Lines changed: 7 additions & 4 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' );
2625
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var uniform = require( '@stdlib/random/array/uniform' );
2727
var tryRequire = require( '@stdlib/utils/try-require' );
2828
var pkg = require( './../package.json' ).name;
2929

@@ -39,16 +39,19 @@ var opts = {
3939
// MAIN //
4040

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

48+
len = 100;
49+
x = uniform( len, -50, 50 );
50+
y = uniform( len, -50, 50 );
51+
4752
b.tic();
4853
for ( i = 0; i < b.iterations; i++ ) {
49-
x = ( randu()*100.0 ) - 50.0;
50-
y = ( randu()*100.0 ) - 50.0;
51-
z = hypot( x, y );
54+
z = hypot( x[ i % len ], y[ i % len ] );
5255
if ( isnan( z ) ) {
5356
b.fail( 'should not return NaN' );
5457
}

lib/node_modules/@stdlib/math/base/special/hypot/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() * 100.0 ) - 50.0;
102+
y[ i ] = ( rand_double() * 100.0 ) - 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_hypot( x, y );
107+
z = stdlib_base_hypot( 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/hypot/examples/index.js

Lines changed: 7 additions & 11 deletions
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' );
2322
var hypot = require( './../lib' );
2423

25-
var x;
26-
var y;
27-
var h;
28-
var i;
24+
var len = 100;
25+
var x = discreteUniform( len, -50, 50 );
26+
var y = discreteUniform( len, -50, 50 );
2927

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( 'h(%d,%d) = %d', x, y, h );
28+
var i;
29+
for ( i = 0; i < len; i++ ) {
30+
console.log( 'h(%d,%d) = %d', x[ i ], y[ i ], hypot( x[ i ], y[ i ] ) );
3531
}

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
{
2929
"task": "build",
3030
"src": [
31-
"./src/hypot.c"
31+
"./src/main.c"
3232
],
3333
"include": [
3434
"./include"
@@ -41,13 +41,14 @@
4141
"@stdlib/math/base/napi/binary",
4242
"@stdlib/math/base/assert/is-nan",
4343
"@stdlib/math/base/assert/is-infinite",
44-
"@stdlib/math/base/special/sqrt"
44+
"@stdlib/math/base/special/sqrt",
45+
"@stdlib/constants/float64/pinf"
4546
]
4647
},
4748
{
4849
"task": "benchmark",
4950
"src": [
50-
"./src/hypot.c"
51+
"./src/main.c"
5152
],
5253
"include": [
5354
"./include"
@@ -59,13 +60,14 @@
5960
"dependencies": [
6061
"@stdlib/math/base/assert/is-nan",
6162
"@stdlib/math/base/assert/is-infinite",
62-
"@stdlib/math/base/special/sqrt"
63+
"@stdlib/math/base/special/sqrt",
64+
"@stdlib/constants/float64/pinf"
6365
]
6466
},
6567
{
6668
"task": "examples",
6769
"src": [
68-
"./src/hypot.c"
70+
"./src/main.c"
6971
],
7072
"include": [
7173
"./include"
@@ -77,7 +79,8 @@
7779
"dependencies": [
7880
"@stdlib/math/base/assert/is-nan",
7981
"@stdlib/math/base/assert/is-infinite",
80-
"@stdlib/math/base/special/sqrt"
82+
"@stdlib/math/base/special/sqrt",
83+
"@stdlib/constants/float64/pinf"
8184
]
8285
}
8386
]

lib/node_modules/@stdlib/math/base/special/hypot/src/hypot.c renamed to lib/node_modules/@stdlib/math/base/special/hypot/src/main.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#include "stdlib/math/base/assert/is_nan.h"
2121
#include "stdlib/math/base/assert/is_infinite.h"
2222
#include "stdlib/math/base/special/sqrt.h"
23-
#include <math.h>
23+
#include "stdlib/constants/float64/pinf.h"
2424

2525
/**
2626
* Computes the hypotenuse avoiding overflow and underflow.
@@ -41,7 +41,7 @@ double stdlib_base_hypot( const double x, const double y ) {
4141
return 0.0 / 0.0; // NaN
4242
}
4343
if ( stdlib_base_is_infinite( x ) || stdlib_base_is_infinite( y ) ) {
44-
return HUGE_VAL;
44+
return STDLIB_CONSTANT_FLOAT64_PINF;
4545
}
4646
a = x;
4747
b = y;
@@ -60,5 +60,5 @@ double stdlib_base_hypot( const double x, const double y ) {
6060
return 0.0;
6161
}
6262
b /= a;
63-
return a * stdlib_base_sqrt( 1.0 + (b*b) );
63+
return a * stdlib_base_sqrt( 1.0 + ( b * b ) );
6464
}

lib/node_modules/@stdlib/math/base/special/hypot/test/test.js

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -49,28 +49,28 @@ tape( 'the function returns `+infinity` if either argument is `+-infinity`', fun
4949
var h;
5050

5151
h = hypot( PINF, 3.14 );
52-
t.strictEqual( h, PINF, 'returns +infinity' );
52+
t.strictEqual( h, PINF, 'returns expected value' );
5353

5454
h = hypot( 3.14, PINF );
55-
t.strictEqual( h, PINF, 'returns +infinity' );
55+
t.strictEqual( h, PINF, 'returns expected value' );
5656

5757
h = hypot( NINF, 3.14 );
58-
t.strictEqual( h, PINF, 'returns +infinity' );
58+
t.strictEqual( h, PINF, 'returns expected value' );
5959

6060
h = hypot( 3.14, NINF );
61-
t.strictEqual( h, PINF, 'returns +infinity' );
61+
t.strictEqual( h, PINF, 'returns expected value' );
6262

6363
h = hypot( PINF, PINF );
64-
t.strictEqual( h, PINF, 'returns +infinity' );
64+
t.strictEqual( h, PINF, 'returns expected value' );
6565

6666
h = hypot( NINF, PINF );
67-
t.strictEqual( h, PINF, 'returns +infinity' );
67+
t.strictEqual( h, PINF, 'returns expected value' );
6868

6969
h = hypot( PINF, NINF );
70-
t.strictEqual( h, PINF, 'returns +infinity' );
70+
t.strictEqual( h, PINF, 'returns expected value' );
7171

7272
h = hypot( NINF, NINF );
73-
t.strictEqual( h, PINF, 'returns +infinity' );
73+
t.strictEqual( h, PINF, 'returns expected value' );
7474

7575
t.end();
7676
});
@@ -79,13 +79,13 @@ tape( 'the function returns `NaN` if either argument is `NaN`', function test( t
7979
var h;
8080

8181
h = hypot( NaN, 3.14 );
82-
t.strictEqual( isnan( h ), true, 'returns NaN' );
82+
t.strictEqual( isnan( h ), true, 'returns expected value' );
8383

8484
h = hypot( 3.14, NaN );
85-
t.strictEqual( isnan( h ), true, 'returns NaN' );
85+
t.strictEqual( isnan( h ), true, 'returns expected value' );
8686

8787
h = hypot( NaN, NaN );
88-
t.strictEqual( isnan( h ), true, 'returns NaN' );
88+
t.strictEqual( isnan( h ), true, 'returns expected value' );
8989

9090
t.end();
9191
});
@@ -94,16 +94,16 @@ tape( 'the function returns `+0` if both arguments are `+-0`', function test( t
9494
var h;
9595

9696
h = hypot( +0.0, +0.0 );
97-
t.strictEqual( isPositiveZero( h ), true, 'returns +0' );
97+
t.strictEqual( isPositiveZero( h ), true, 'returns expected value' );
9898

9999
h = hypot( -0.0, +0.0 );
100-
t.strictEqual( isPositiveZero( h ), true, 'returns +0' );
100+
t.strictEqual( isPositiveZero( h ), true, 'returns expected value' );
101101

102102
h = hypot( +0.0, -0.0 );
103-
t.strictEqual( isPositiveZero( h ), true, 'returns +0' );
103+
t.strictEqual( isPositiveZero( h ), true, 'returns expected value' );
104104

105105
h = hypot( -0.0, -0.0 );
106-
t.strictEqual( isPositiveZero( h ), true, 'returns +0' );
106+
t.strictEqual( isPositiveZero( h ), true, 'returns expected value' );
107107

108108
t.end();
109109
});
@@ -138,13 +138,13 @@ tape( 'the function computes the hypotenuse (canonical inputs)', function test(
138138
var h;
139139

140140
h = hypot( 3.0, 4.0 );
141-
t.strictEqual( h, 5.0, 'returns 5.0' );
141+
t.strictEqual( h, 5.0, 'returns expected value' );
142142

143143
h = hypot( 6.0, 8.0 );
144-
t.strictEqual( h, 10.0, 'returns 10.0' );
144+
t.strictEqual( h, 10.0, 'returns expected value' );
145145

146146
h = hypot( 5.0, 12.0 );
147-
t.strictEqual( h, 13.0, 'returns 13.0' );
147+
t.strictEqual( h, 13.0, 'returns expected value' );
148148

149149
t.end();
150150
});
@@ -153,7 +153,7 @@ tape( 'the function avoids overflow', function test( t ) {
153153
var h;
154154

155155
h = sqrt( pow( 1.0e308, 2 ) + pow( 1.0e308, 2 ) );
156-
t.strictEqual( h, PINF, 'returns +infinity' );
156+
t.strictEqual( h, PINF, 'returns expected value' );
157157

158158
h = hypot( 1.0e308, 1.0e308 );
159159
t.strictEqual( h, 1.4142135623730951e308, 'avoids overflow' );

0 commit comments

Comments
 (0)