Skip to content

Commit 8c519dd

Browse files
feat: add math/base/special/coversinf
PR-URL: #7316 Ref: #649 Reviewed-by: Philipp Burckhardt <[email protected]>
1 parent c1ec14d commit 8c519dd

34 files changed

+2350
-0
lines changed
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# Coversine
22+
23+
> Compute the [coversed sine][coversed-sine] of a single-precision floating-point number (in radians).
24+
25+
<section class="intro">
26+
27+
The [coversed sine][coversed-sine] is defined as
28+
29+
<!-- <equation class="equation" label="eq:coversine" align="center" raw="\operatorname{coversin}(\theta) = 1 - \sin \theta" alt="Coversed sine."> -->
30+
31+
```math
32+
\mathop{\mathrm{coversin}}(\theta) = 1 - \sin \theta
33+
```
34+
35+
<!-- <div class="equation" align="center" data-raw-text="\operatorname{coversin}(\theta) = 1 - \sin \theta" data-equation="eq:coversine">
36+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@bb29798906e119fcb2af99e94b60407a270c9b32/lib/node_modules/@stdlib/math/base/special/coversin/docs/img/equation_coversine.svg" alt="Coversed sine.">
37+
<br>
38+
</div> -->
39+
40+
<!-- </equation> -->
41+
42+
</section>
43+
44+
<!-- /.intro -->
45+
46+
<section class="usage">
47+
48+
## Usage
49+
50+
```javascript
51+
var coversinf = require( '@stdlib/math/base/special/coversinf' );
52+
```
53+
54+
#### coversinf( x )
55+
56+
Computes the [coversed sine][coversed-sine] of a single-precision floating-point number (in radians).
57+
58+
```javascript
59+
var v = coversinf( 0.0 );
60+
// returns 1.0
61+
62+
v = coversinf( 3.141592653589793/2.0 );
63+
// returns 0.0
64+
65+
v = coversinf( -3.141592653589793/6.0 );
66+
// returns 1.5
67+
```
68+
69+
</section>
70+
71+
<!-- /.usage -->
72+
73+
<section class="examples">
74+
75+
## Examples
76+
77+
<!-- eslint no-undef: "error" -->
78+
79+
```javascript
80+
var uniform = require( '@stdlib/random/array/uniform' );
81+
var logEachMap = require( '@stdlib/console/log-each-map' );
82+
var TWO_PI = require( '@stdlib/constants/float32/two-pi' );
83+
var coversinf = require( '@stdlib/math/base/special/coversinf' );
84+
85+
var opts = {
86+
'dtype': 'float32'
87+
};
88+
var x = uniform( 100, 0.0, TWO_PI, opts );
89+
90+
logEachMap( 'coversinf(%0.4f) = %0.4f', x, coversinf );
91+
```
92+
93+
</section>
94+
95+
<!-- /.examples -->
96+
97+
<!-- C interface documentation. -->
98+
99+
* * *
100+
101+
<section class="c">
102+
103+
## C APIs
104+
105+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
106+
107+
<section class="intro">
108+
109+
</section>
110+
111+
<!-- /.intro -->
112+
113+
<!-- C usage documentation. -->
114+
115+
<section class="usage">
116+
117+
### Usage
118+
119+
```c
120+
#include "stdlib/math/base/special/coversinf.h"
121+
```
122+
123+
#### stdlib_base_coversinf( x )
124+
125+
Computes the [coversed sine][coversed-sine] of a single-precision floating-point number (in radians).
126+
127+
```c
128+
float out = stdlib_base_coversinf( 0.0f );
129+
// returns 1.0f
130+
131+
out = stdlib_base_coversinf( 3.141592653589793f / 2.0f );
132+
// returns 0.0f
133+
```
134+
135+
The function accepts the following arguments:
136+
137+
- **x**: `[in] float` input value.
138+
139+
```c
140+
float stdlib_base_coversinf( const float x );
141+
```
142+
143+
</section>
144+
145+
<!-- /.usage -->
146+
147+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
148+
149+
<section class="notes">
150+
151+
</section>
152+
153+
<!-- /.notes -->
154+
155+
<!-- C API usage examples. -->
156+
157+
<section class="examples">
158+
159+
### Examples
160+
161+
```c
162+
#include "stdlib/math/base/special/coversinf.h"
163+
#include <stdio.h>
164+
165+
int main( void ) {
166+
const float x[] = { 0.0f, 0.523f, 0.785f, 1.047f, 3.14f };
167+
168+
float y;
169+
int i;
170+
for ( i = 0; i < 5; i++ ) {
171+
y = stdlib_base_coversinf( x[ i ] );
172+
printf( "coversinf(%f) = %f\n", x[ i ], y );
173+
}
174+
}
175+
```
176+
177+
</section>
178+
179+
<!-- /.examples -->
180+
181+
</section>
182+
183+
<!-- /.c -->
184+
185+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
186+
187+
<section class="related">
188+
189+
</section>
190+
191+
<!-- /.related -->
192+
193+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
194+
195+
<section class="links">
196+
197+
[coversed-sine]: https://en.wikipedia.org/wiki/Versine
198+
199+
<!-- <related-links> -->
200+
201+
<!-- </related-links> -->
202+
203+
</section>
204+
205+
<!-- /.links -->
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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/array/uniform' );
25+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
26+
var pkg = require( './../package.json' ).name;
27+
var coversinf = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var x;
34+
var y;
35+
var i;
36+
37+
x = uniform( 100, -10.0, 10.0, {
38+
'dtype': 'float32'
39+
});
40+
41+
b.tic();
42+
for ( i = 0; i < b.iterations; i++ ) {
43+
y = coversinf( x[ i%x.length ] );
44+
if ( isnanf( y ) ) {
45+
b.fail( 'should not return NaN' );
46+
}
47+
}
48+
b.toc();
49+
if ( isnanf( y ) ) {
50+
b.fail( 'should not return NaN' );
51+
}
52+
b.pass( 'benchmark finished' );
53+
b.end();
54+
});
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 uniform = require( '@stdlib/random/array/uniform' );
26+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
27+
var tryRequire = require( '@stdlib/utils/try-require' );
28+
var pkg = require( './../package.json' ).name;
29+
30+
31+
// VARIABLES //
32+
33+
var coversinf = tryRequire( resolve( __dirname, './../lib/native.js' ) );
34+
var opts = {
35+
'skip': ( coversinf instanceof Error )
36+
};
37+
38+
39+
// MAIN //
40+
41+
bench( pkg+'::native', opts, function benchmark( b ) {
42+
var x;
43+
var y;
44+
var i;
45+
46+
x = uniform( 100, -10.0, 10.0, {
47+
'dtype': 'float32'
48+
});
49+
50+
b.tic();
51+
for ( i = 0; i < b.iterations; i++ ) {
52+
y = coversinf( x[ i%x.length ] );
53+
if ( isnanf( y ) ) {
54+
b.fail( 'should not return NaN' );
55+
}
56+
}
57+
b.toc();
58+
if ( isnanf( y ) ) {
59+
b.fail( 'should not return NaN' );
60+
}
61+
b.pass( 'benchmark finished' );
62+
b.end();
63+
});

0 commit comments

Comments
 (0)