Skip to content

Commit e91af13

Browse files
refactor: update math/base/special/hypotf to follow latest project conventions
PR-URL: #4783 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent aca04f8 commit e91af13

File tree

9 files changed

+194
-166
lines changed

9 files changed

+194
-166
lines changed

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,20 +83,19 @@ h = hypotf( 5.0, NaN );
8383
<!-- eslint no-undef: "error" -->
8484

8585
```javascript
86-
var randu = require( '@stdlib/random/base/randu' );
87-
var round = require( '@stdlib/math/base/special/round' );
86+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
8887
var hypotf = require( '@stdlib/math/base/special/hypotf' );
8988

90-
var x;
91-
var y;
92-
var h;
93-
var i;
89+
var len = 100;
90+
var opts = {
91+
'dtype': 'float32'
92+
};
93+
var x = discreteUniform( len, -50, 50, opts );
94+
var y = discreteUniform( len, -50, 50, opts );
9495

95-
for ( i = 0; i < 100; i++ ) {
96-
x = round( randu()*100.0 ) - 50.0;
97-
y = round( randu()*100.0 ) - 50.0;
98-
h = hypotf( x, y );
99-
console.log( 'h(%d,%d) = %d', x, y, h );
96+
var i;
97+
for ( i = 0; i < len; i++ ) {
98+
console.log( 'h(%d,%d) = %d', x[ i ], y[ i ], hypotf( x[ i ], y[ i ] ) );
10099
}
101100
```
102101

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

Lines changed: 23 additions & 7 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 isnanf = require( '@stdlib/math/base/assert/is-nanf' );
2626
var pkg = require( './../package.json' ).name;
2727
var hypotf = require( './../lib' );
@@ -37,16 +37,24 @@ var opts = {
3737
// MAIN //
3838

3939
bench( pkg, function benchmark( b ) {
40+
var opts;
41+
var len;
4042
var x;
4143
var y;
4244
var z;
4345
var i;
4446

47+
opts = {
48+
'dtype': 'float32'
49+
};
50+
51+
len = 100;
52+
x = uniform( len, -50, 50, opts );
53+
y = uniform( len, -50, 50, opts );
54+
4555
b.tic();
4656
for ( i = 0; i < b.iterations; i++ ) {
47-
x = ( randu()*100.0 ) - 50.0;
48-
y = ( randu()*100.0 ) - 50.0;
49-
z = hypotf( x, y );
57+
z = hypotf( x[ i % len ], y[ i % len ] );
5058
if ( isnanf( z ) ) {
5159
b.fail( 'should not return NaN' );
5260
}
@@ -60,16 +68,24 @@ bench( pkg, function benchmark( b ) {
6068
});
6169

6270
bench( pkg+'::built-in', opts, function benchmark( b ) {
71+
var opts;
72+
var len;
6373
var x;
6474
var y;
6575
var z;
6676
var i;
6777

78+
opts = {
79+
'dtype': 'float32'
80+
};
81+
82+
len = 100;
83+
x = uniform( len, -50, 50, opts );
84+
y = uniform( len, -50, 50, opts );
85+
6886
b.tic();
6987
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
88+
z = Math.hypot( x[ i % len ], y[ i % len ] ); // eslint-disable-line stdlib/no-builtin-math
7389
if ( isnanf( z ) ) {
7490
b.fail( 'should not return NaN' );
7591
}

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

Lines changed: 12 additions & 4 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 isnanf = require( '@stdlib/math/base/assert/is-nanf' );
2727
var tryRequire = require( '@stdlib/utils/try-require' );
2828
var pkg = require( './../package.json' ).name;
@@ -39,16 +39,24 @@ var opts = {
3939
// MAIN //
4040

4141
bench( pkg+'::native', opts, function benchmark( b ) {
42+
var opts;
43+
var len;
4244
var x;
4345
var y;
4446
var z;
4547
var i;
4648

49+
opts = {
50+
'dtype': 'float32'
51+
};
52+
53+
len = 100;
54+
x = uniform( len, -50, 50, opts );
55+
y = uniform( len, -50, 50, opts );
56+
4757
b.tic();
4858
for ( i = 0; i < b.iterations; i++ ) {
49-
x = ( randu()*100.0 ) - 50.0;
50-
y = ( randu()*100.0 ) - 50.0;
51-
z = hypotf( x, y );
59+
z = hypotf( x[ i % len ], y[ i % len ] );
5260
if ( isnanf( z ) ) {
5361
b.fail( 'should not return NaN' );
5462
}

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,19 @@ static float rand_float( void ) {
9292
static double benchmark( void ) {
9393
double elapsed;
9494
double t;
95-
float x;
96-
float y;
95+
float x[ 100 ];
96+
float y[ 100 ];
9797
float z;
9898
int i;
9999

100+
for ( i = 0; i < 100; i++ ) {
101+
x[ i ] = ( 100.0f * rand_float() ) - 50.0f;
102+
y[ i ] = ( 100.0f * rand_float() ) - 50.0f;
103+
}
104+
100105
t = tic();
101106
for ( i = 0; i < ITERATIONS; i++ ) {
102-
x = ( 100.0f*rand_float() ) - 50.0f;
103-
y = ( 100.0f*rand_float() ) - 50.0f;
104-
z = stdlib_base_hypotf( x, y );
107+
z = stdlib_base_hypotf( 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/hypotf/examples/index.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,17 @@
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 hypotf = require( './../lib' );
2423

25-
var x;
26-
var y;
27-
var h;
28-
var i;
24+
var len = 100;
25+
var opts = {
26+
'dtype': 'float32'
27+
};
28+
var x = discreteUniform( len, -50, 50, opts );
29+
var y = discreteUniform( len, -50, 50, opts );
2930

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 = hypotf( x, y );
34-
console.log( 'h(%d,%d) = %d', x, y, h );
31+
var i;
32+
for ( i = 0; i < len; i++ ) {
33+
console.log( 'h(%d,%d) = %d', x[ i ], y[ i ], hypotf( x[ i ], y[ i ] ) );
3534
}
Lines changed: 85 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,87 @@
11
{
2-
"options": {
3-
"task": "build"
4-
},
5-
"fields": [
6-
{
7-
"field": "src",
8-
"resolve": true,
9-
"relative": true
10-
},
11-
{
12-
"field": "include",
13-
"resolve": true,
14-
"relative": true
15-
},
16-
{
17-
"field": "libraries",
18-
"resolve": false,
19-
"relative": false
20-
},
21-
{
22-
"field": "libpath",
23-
"resolve": true,
24-
"relative": false
25-
}
26-
],
27-
"confs": [
28-
{
29-
"task": "build",
30-
"src": [
31-
"./src/hypotf.c"
32-
],
33-
"include": [
34-
"./include"
35-
],
36-
"libraries": [
37-
"-lm"
38-
],
39-
"libpath": [],
40-
"dependencies": [
41-
"@stdlib/math/base/napi/binary",
42-
"@stdlib/math/base/assert/is-nanf",
43-
"@stdlib/math/base/assert/is-infinitef",
44-
"@stdlib/math/base/special/sqrtf"
45-
]
46-
},
47-
{
48-
"task": "benchmark",
49-
"src": [
50-
"./src/hypotf.c"
51-
],
52-
"include": [
53-
"./include"
54-
],
55-
"libraries": [
56-
"-lm"
57-
],
58-
"libpath": [],
59-
"dependencies": [
60-
"@stdlib/math/base/assert/is-nanf",
61-
"@stdlib/math/base/assert/is-infinitef",
62-
"@stdlib/math/base/special/sqrtf"
63-
]
64-
},
65-
{
66-
"task": "examples",
67-
"src": [
68-
"./src/hypotf.c"
69-
],
70-
"include": [
71-
"./include"
72-
],
73-
"libraries": [
74-
"-lm"
75-
],
76-
"libpath": [],
77-
"dependencies": [
78-
"@stdlib/math/base/assert/is-nanf",
79-
"@stdlib/math/base/assert/is-infinitef",
80-
"@stdlib/math/base/special/sqrtf"
81-
]
82-
}
83-
]
2+
"options": {
3+
"task": "build"
4+
},
5+
"fields": [
6+
{
7+
"field": "src",
8+
"resolve": true,
9+
"relative": true
10+
},
11+
{
12+
"field": "include",
13+
"resolve": true,
14+
"relative": true
15+
},
16+
{
17+
"field": "libraries",
18+
"resolve": false,
19+
"relative": false
20+
},
21+
{
22+
"field": "libpath",
23+
"resolve": true,
24+
"relative": false
25+
}
26+
],
27+
"confs": [
28+
{
29+
"task": "build",
30+
"src": [
31+
"./src/main.c"
32+
],
33+
"include": [
34+
"./include"
35+
],
36+
"libraries": [
37+
"-lm"
38+
],
39+
"libpath": [],
40+
"dependencies": [
41+
"@stdlib/math/base/napi/binary",
42+
"@stdlib/math/base/assert/is-nanf",
43+
"@stdlib/math/base/assert/is-infinitef",
44+
"@stdlib/math/base/special/sqrtf",
45+
"@stdlib/constants/float32/pinf"
46+
]
47+
},
48+
{
49+
"task": "benchmark",
50+
"src": [
51+
"./src/main.c"
52+
],
53+
"include": [
54+
"./include"
55+
],
56+
"libraries": [
57+
"-lm"
58+
],
59+
"libpath": [],
60+
"dependencies": [
61+
"@stdlib/math/base/assert/is-nanf",
62+
"@stdlib/math/base/assert/is-infinitef",
63+
"@stdlib/math/base/special/sqrtf",
64+
"@stdlib/constants/float32/pinf"
65+
]
66+
},
67+
{
68+
"task": "examples",
69+
"src": [
70+
"./src/main.c"
71+
],
72+
"include": [
73+
"./include"
74+
],
75+
"libraries": [
76+
"-lm"
77+
],
78+
"libpath": [],
79+
"dependencies": [
80+
"@stdlib/math/base/assert/is-nanf",
81+
"@stdlib/math/base/assert/is-infinitef",
82+
"@stdlib/math/base/special/sqrtf",
83+
"@stdlib/constants/float32/pinf"
84+
]
85+
}
86+
]
8487
}

lib/node_modules/@stdlib/math/base/special/hypotf/src/hypotf.c renamed to lib/node_modules/@stdlib/math/base/special/hypotf/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_nanf.h"
2121
#include "stdlib/math/base/assert/is_infinitef.h"
2222
#include "stdlib/math/base/special/sqrtf.h"
23-
#include <math.h>
23+
#include "stdlib/constants/float32/pinf.h"
2424

2525
/**
2626
* Computes the hypotenuse avoiding overflow and underflow (single-precision).
@@ -41,7 +41,7 @@ float stdlib_base_hypotf( const float x, const float y ) {
4141
return 0.0f / 0.0f; // NaN
4242
}
4343
if ( stdlib_base_is_infinitef( x ) || stdlib_base_is_infinitef( y ) ) {
44-
return INFINITY;
44+
return STDLIB_CONSTANT_FLOAT32_PINF;
4545
}
4646
a = x;
4747
b = y;
@@ -60,5 +60,5 @@ float stdlib_base_hypotf( const float x, const float y ) {
6060
return 0.0f;
6161
}
6262
b /= a;
63-
return a * stdlib_base_sqrtf( 1.0f + (b*b) );
63+
return a * stdlib_base_sqrtf( 1.0f + ( b * b ) );
6464
}

0 commit comments

Comments
 (0)