Skip to content

Commit cf07552

Browse files
committed
feat: add @stdlib/math/base/special/sinhf
1 parent 404e9c7 commit cf07552

23 files changed

Lines changed: 1528 additions & 0 deletions

File tree

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<!--
2+
@license Apache-2.0
3+
4+
Copyright (c) 2026 The Stdlib Authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
-->
18+
19+
# sinhf
20+
21+
> Compute the hyperbolic sine of a single-precision floating-point number.
22+
23+
<section class="usage">
24+
25+
## Usage
26+
27+
```javascript
28+
var sinhf = require( '@stdlib/math/base/special/sinhf' );
29+
```
30+
31+
#### sinhf( x )
32+
33+
Computes the hyperbolic sine of a single-precision floating-point number.
34+
35+
```javascript
36+
var v = sinhf( 0.0 );
37+
// returns 0.0
38+
39+
v = sinhf( 2.0 );
40+
// returns ~3.627
41+
42+
v = sinhf( -2.0 );
43+
// returns ~-3.627
44+
45+
v = sinhf( NaN );
46+
// returns NaN
47+
```
48+
49+
</section>
50+
51+
<!-- /.usage -->
52+
53+
<section class="examples">
54+
55+
## Examples
56+
57+
<!-- eslint no-undef: "error" -->
58+
59+
```javascript
60+
var uniform = require( '@stdlib/random/base/uniform' );
61+
var sinhf = require( '@stdlib/math/base/special/sinhf' );
62+
63+
var x;
64+
var i;
65+
66+
for ( i = 0; i < 100; i++ ) {
67+
x = uniform( -5.0, 5.0 );
68+
console.log( 'sinh(%d) = %d', x, sinhf( x ) );
69+
}
70+
```
71+
72+
</section>
73+
74+
<!-- /.examples -->
75+
76+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
77+
78+
<section class="related">
79+
80+
</section>
81+
82+
<!-- /.related -->
83+
84+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
85+
86+
<section class="links">
87+
88+
</section>
89+
90+
<!-- /.links -->
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#include "stdlib/math/base/special/sinhf.h"
20+
#include <stdlib.h>
21+
#include <stdio.h>
22+
#include <math.h>
23+
#include <time.h>
24+
#include <sys/time.h>
25+
26+
/**
27+
* Computes the elapsed time.
28+
*
29+
* @param tic starting time
30+
* @param toc ending time
31+
* @return elapsed time in seconds
32+
*/
33+
static double elapsed( struct timeval tic, struct timeval toc ) {
34+
double t;
35+
t = (double)( toc.tv_sec - tic.tv_sec );
36+
t += (double)( toc.tv_usec - tic.tv_usec ) / 1.0e6;
37+
return t;
38+
}
39+
40+
/**
41+
* Generates a random number on the interval `[a,b)`.
42+
*
43+
* @param a lower bound
44+
* @param b upper bound
45+
* @return random number
46+
*/
47+
static float rand_float( const float a, const float b ) {
48+
float r = (float)rand() / (float)RAND_MAX;
49+
return a + ( r * ( b - a ) );
50+
}
51+
52+
/**
53+
* Runs a benchmark.
54+
*
55+
* @param iterations number of iterations
56+
* @return elapsed time in seconds
57+
*/
58+
static double benchmark( const int iterations ) {
59+
struct timeval tic;
60+
struct timeval toc;
61+
float x;
62+
float y;
63+
int i;
64+
65+
gettimeofday( &tic, NULL );
66+
for ( i = 0; i < iterations; i++ ) {
67+
x = rand_float( -10.0f, 10.0f );
68+
y = stdlib_base_sinhf( x );
69+
if ( y != y ) {
70+
printf( "should not return NaN\n" );
71+
break;
72+
}
73+
}
74+
gettimeofday( &toc, NULL );
75+
76+
if ( y != y ) {
77+
printf( "should not return NaN\n" );
78+
}
79+
return elapsed( tic, toc );
80+
}
81+
82+
/**
83+
* Main execution sequence.
84+
*/
85+
int main( void ) {
86+
double t;
87+
int count;
88+
int i;
89+
90+
// Use the current time to seed the random number generator:
91+
srand( time( NULL ) );
92+
93+
print_version();
94+
count = 1e6;
95+
for ( i = 0; i < 3; i++ ) {
96+
t = benchmark( count );
97+
print_results( count, t );
98+
}
99+
return 0;
100+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var uniform = require( '@stdlib/random/base/uniform' );
25+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
26+
var pkg = require( './../package.json' ).name;
27+
var sinhf = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var x;
34+
var y;
35+
var i;
36+
37+
b.tic();
38+
for ( i = 0; i < b.iterations; i++ ) {
39+
x = uniform( -10.0, 10.0 );
40+
y = sinhf( x );
41+
if ( isnanf( y ) ) {
42+
b.fail( 'should not return NaN' );
43+
}
44+
}
45+
b.toc();
46+
if ( isnanf( y ) ) {
47+
b.fail( 'should not return NaN' );
48+
}
49+
b.pass( 'benchmark finished' );
50+
b.end();
51+
});
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var resolve = require( 'path' ).resolve;
24+
var bench = require( '@stdlib/bench' );
25+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
26+
var uniform = require( '@stdlib/random/base/uniform' );
27+
var format = require( '@stdlib/string/format' );
28+
var tryRequire = require( '@stdlib/utils/try-require' );
29+
var pkg = require( './../package.json' ).name;
30+
31+
32+
// VARIABLES //
33+
34+
var sinhf = tryRequire( resolve( __dirname, './../lib/native.js' ) );
35+
var opts = {
36+
'skip': ( sinhf instanceof Error )
37+
};
38+
39+
40+
// MAIN //
41+
42+
bench( format( '%s::native', pkg ), opts, function benchmark( b ) {
43+
var x;
44+
var y;
45+
var i;
46+
47+
b.tic();
48+
for ( i = 0; i < b.iterations; i++ ) {
49+
x = uniform( -10.0, 10.0 );
50+
y = sinhf( x );
51+
if ( isnanf( y ) ) {
52+
b.fail( 'should not return NaN' );
53+
}
54+
}
55+
b.toc();
56+
if ( isnanf( y ) ) {
57+
b.fail( 'should not return NaN' );
58+
}
59+
b.pass( 'benchmark finished' );
60+
b.end();
61+
});

0 commit comments

Comments
 (0)