Skip to content

Commit b0cd1b9

Browse files
feat: add math/base/special/cospif
PR-URL: #7726 Ref: #649 Co-authored-by: Philipp Burckhardt <pburckhardt@outlook.com> Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com>
1 parent c4e5266 commit b0cd1b9

File tree

32 files changed

+2482
-0
lines changed

32 files changed

+2482
-0
lines changed
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
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+
# cospif
22+
23+
> Compute the [cosine][@stdlib/math/base/special/cosf] of a number times [π][@stdlib/constants/float32/pi].
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var cospif = require( '@stdlib/math/base/special/cospif' );
31+
```
32+
33+
#### cospif( x )
34+
35+
Computes `cos(πx)` in single-precision floating-point format more accurately than `cos(pi*x)`, especially for large `x`.
36+
37+
```javascript
38+
var y = cospif( 0.0 );
39+
// returns 1.0
40+
41+
y = cospif( 0.5 );
42+
// returns 0.0
43+
44+
y = cospif( 0.1 );
45+
// returns ~0.951
46+
47+
y = cospif( NaN );
48+
// returns NaN
49+
```
50+
51+
</section>
52+
53+
<!-- /.usage -->
54+
55+
<section class="examples">
56+
57+
## Examples
58+
59+
<!-- eslint no-undef: "error" -->
60+
61+
```javascript
62+
var uniform = require( '@stdlib/random/array/uniform' );
63+
var logEachMap = require( '@stdlib/console/log-each-map' );
64+
var cospif = require( '@stdlib/math/base/special/cospif' );
65+
66+
var opts = {
67+
'dtype': 'float32'
68+
};
69+
var x = uniform( 100, -100.0, 100.0, opts );
70+
71+
logEachMap( 'cos( π * %0.4f ) = %0.4f', x, cospif );
72+
```
73+
74+
</section>
75+
76+
<!-- /.examples -->
77+
78+
<!-- C interface documentation. -->
79+
80+
* * *
81+
82+
<section class="c">
83+
84+
## C APIs
85+
86+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
87+
88+
<section class="intro">
89+
90+
</section>
91+
92+
<!-- /.intro -->
93+
94+
<!-- C usage documentation. -->
95+
96+
<section class="usage">
97+
98+
### Usage
99+
100+
```c
101+
#include "stdlib/math/base/special/cospif.h"
102+
```
103+
104+
#### stdlib_base_cospif( x )
105+
106+
Computes `cos(πx)` in single-precision floating-point format more accurately than `cos(pi*x)`, especially for large `x`.
107+
108+
```c
109+
float out = stdlib_base_cospif( 0.5f );
110+
// returns 0.0f
111+
```
112+
113+
The function accepts the following arguments:
114+
115+
- **x**: `[in] float` input value.
116+
117+
```c
118+
float stdlib_base_cospif( const float x );
119+
```
120+
121+
</section>
122+
123+
<!-- /.usage -->
124+
125+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
126+
127+
<section class="notes">
128+
129+
</section>
130+
131+
<!-- /.notes -->
132+
133+
<!-- C API usage examples. -->
134+
135+
<section class="examples">
136+
137+
### Examples
138+
139+
```c
140+
#include "stdlib/math/base/special/cospif.h"
141+
#include <stdio.h>
142+
143+
int main( void ) {
144+
const float x[] = { 0.0f, 0.523f, 0.785f, 1.047f, 3.14f };
145+
146+
float y;
147+
int i;
148+
for ( i = 0; i < 5; i++ ) {
149+
y = stdlib_base_cospif( x[ i ] );
150+
printf( "cos( π * %f ) = %f\n", x[ i ], y );
151+
}
152+
}
153+
```
154+
155+
</section>
156+
157+
<!-- /.examples -->
158+
159+
</section>
160+
161+
<!-- /.c -->
162+
163+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
164+
165+
<section class="related">
166+
167+
</section>
168+
169+
<!-- /.related -->
170+
171+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
172+
173+
<section class="links">
174+
175+
[@stdlib/constants/float32/pi]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/constants/float32/pi
176+
177+
[@stdlib/math/base/special/cosf]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/cosf
178+
179+
<!-- <related-links> -->
180+
181+
<!-- </related-links> -->
182+
183+
</section>
184+
185+
<!-- /.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 cospif = 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, -5.0e6, 5.0e6, {
38+
'dtype': 'float32'
39+
});
40+
41+
b.tic();
42+
for ( i = 0; i < b.iterations; i++ ) {
43+
y = cospif( 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 cospif = tryRequire( resolve( __dirname, './../lib/native.js' ) );
34+
var opts = {
35+
'skip': ( cospif 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, -5.0e6, 5.0e6, {
47+
'dtype': 'float32'
48+
});
49+
50+
b.tic();
51+
for ( i = 0; i < b.iterations; i++ ) {
52+
y = cospif( 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)