Skip to content

Commit b47c0f0

Browse files
feat: add math/base/special/sincosdf
PR-URL: #9822 Ref: #649 Co-authored-by: Philipp Burckhardt <pburckhardt@outlook.com> Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com> Signed-off-by: ByteKnight28 <shsuasud@gmail.com> Signed-off-by: Philipp Burckhardt <pburckhardt@outlook.com> Signed-off-by: Shantanu Kharwar <shsuasud@gmail.com>
1 parent 0ae8a91 commit b47c0f0

37 files changed

+3210
-0
lines changed
Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 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+
# sincosdf
22+
23+
> Simultaneously compute the [sine][@stdlib/math/base/special/sindf] and [cosine][@stdlib/math/base/special/cosdf] of a single-precision floating-point number (in degrees).
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var sincosdf = require( '@stdlib/math/base/special/sincosdf' );
31+
```
32+
33+
#### sincosdf( x )
34+
35+
Simultaneously computes the [sine][@stdlib/math/base/special/sindf] and [cosine][@stdlib/math/base/special/cosdf] of a single-precision floating-point number (in degrees).
36+
37+
```javascript
38+
var v = sincosdf( 0.0 );
39+
// returns [ ~0.0, ~1.0 ]
40+
41+
v = sincosdf( 90.0 );
42+
// returns [ ~1.0, ~0.0 ]
43+
44+
v = sincosdf( -30.0 );
45+
// returns [ ~-0.5, ~0.866 ]
46+
```
47+
48+
#### sincosdf.assign( x, out, stride, offset )
49+
50+
Simultaneously computes the [sine][@stdlib/math/base/special/sindf] and [cosine][@stdlib/math/base/special/cosdf] of a single-precision floating-point number (in degrees) and assigns the results to a provided output array.
51+
52+
```javascript
53+
var Float32Array = require( '@stdlib/array/float32' );
54+
55+
var out = new Float32Array( 2 );
56+
57+
var v = sincosdf.assign( 0.0, out, 1, 0 );
58+
// returns <Float32Array>[ ~0.0, ~1.0 ]
59+
60+
var bool = ( v === out );
61+
// returns true
62+
```
63+
64+
</section>
65+
66+
<!-- /.usage -->
67+
68+
<section class="examples">
69+
70+
## Examples
71+
72+
<!-- eslint no-undef: "error" -->
73+
74+
```javascript
75+
var linspace = require( '@stdlib/array/base/linspace' );
76+
var sincosdf = require( '@stdlib/math/base/special/sincosdf' );
77+
78+
var x = linspace( 0.0, 180.0, 100 );
79+
80+
var y;
81+
var i;
82+
for ( i = 0; i < x.length; i++ ) {
83+
y = sincosdf( x[ i ] );
84+
console.log( 'sincosdf(%d) = [ %d, %d ]', x[ i ], y[ 0 ], y[ 1 ] );
85+
}
86+
```
87+
88+
</section>
89+
90+
<!-- /.examples -->
91+
92+
<!-- C interface documentation. -->
93+
94+
* * *
95+
96+
<section class="c">
97+
98+
## C APIs
99+
100+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
101+
102+
<section class="intro">
103+
104+
</section>
105+
106+
<!-- /.intro -->
107+
108+
<!-- C usage documentation. -->
109+
110+
<section class="usage">
111+
112+
### Usage
113+
114+
```c
115+
#include "stdlib/math/base/special/sincosdf.h"
116+
```
117+
118+
#### stdlib_base_sincosdf( x, &sine, &cosine )
119+
120+
Simultaneously computes the [sine][@stdlib/math/base/special/sindf] and [cosine][@stdlib/math/base/special/cosdf] of a single-precision floating-point number (in degrees).
121+
122+
```c
123+
float cosine;
124+
float sine;
125+
126+
stdlib_base_sincosdf( 4.0f, &sine, &cosine );
127+
```
128+
129+
The function accepts the following arguments:
130+
131+
- **x**: `[in] float` input value.
132+
- **sine**: `[out] float*` destination for the sine.
133+
- **cosine**: `[out] float*` destination for the cosine.
134+
135+
```c
136+
void stdlib_base_sincosdf( const float x, float *sine, float *cosine );
137+
```
138+
139+
</section>
140+
141+
<!-- /.usage -->
142+
143+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
144+
145+
<section class="notes">
146+
147+
</section>
148+
149+
<!-- /.notes -->
150+
151+
<!-- C API usage examples. -->
152+
153+
<section class="examples">
154+
155+
### Examples
156+
157+
```c
158+
#include "stdlib/math/base/special/sincosdf.h"
159+
#include <stdio.h>
160+
161+
int main( void ) {
162+
const float x[] = { 0.0f, 90.0f, 180.0f, 360.0f };
163+
164+
float cosine;
165+
float sine;
166+
int i;
167+
for ( i = 0; i < 4; i++ ) {
168+
stdlib_base_sincosdf( x[ i ], &sine, &cosine );
169+
printf( "x: %f => sine: %f, cosine: %f\n", x[ i ], sine, cosine );
170+
}
171+
}
172+
```
173+
174+
</section>
175+
176+
<!-- /.examples -->
177+
178+
</section>
179+
180+
<!-- /.c -->
181+
182+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
183+
184+
<section class="related">
185+
186+
</section>
187+
188+
<!-- /.related -->
189+
190+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
191+
192+
<section class="links">
193+
194+
[@stdlib/math/base/special/cosdf]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/cosdf
195+
196+
[@stdlib/math/base/special/sindf]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/sindf
197+
198+
<!-- <related-links> -->
199+
200+
<!-- </related-links> -->
201+
202+
</section>
203+
204+
<!-- /.links -->
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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 format = require( '@stdlib/string/format' );
25+
var uniform = require( '@stdlib/random/array/uniform' );
26+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
27+
var sindf = require( '@stdlib/math/base/special/sindf' );
28+
var cosdf = require( '@stdlib/math/base/special/cosdf' );
29+
var pkg = require( './../package.json' ).name;
30+
var sincosdf = require( './../lib' );
31+
32+
33+
// MAIN //
34+
35+
bench( pkg, function benchmark( b ) {
36+
var x;
37+
var y;
38+
var i;
39+
40+
x = uniform( 100, -10.0, 10.0, {
41+
'dtype': 'float32'
42+
});
43+
44+
b.tic();
45+
for ( i = 0; i < b.iterations; i++ ) {
46+
y = sincosdf( x[ i % x.length ] );
47+
if ( isnanf( y[ 0 ] ) || isnanf( y[ 1 ] ) ) {
48+
b.fail( 'should not return NaN' );
49+
}
50+
}
51+
b.toc();
52+
if ( isnanf( y[ 0 ] ) || isnanf( y[ 1 ] ) ) {
53+
b.fail( 'should not return NaN' );
54+
}
55+
b.pass( 'benchmark finished' );
56+
b.end();
57+
});
58+
59+
bench( format( '%s::separate-evaluation', pkg ), function benchmark( b ) {
60+
var x;
61+
var y;
62+
var i;
63+
64+
x = uniform( 100, -10.0, 10.0, {
65+
'dtype': 'float32'
66+
});
67+
y = [ 0.0, 0.0 ];
68+
69+
b.tic();
70+
for ( i = 0; i < b.iterations; i++ ) {
71+
y = [ sindf( x[ i % x.length ] ), cosdf( x[ i % x.length ] ) ];
72+
if ( isnanf( y[ 0 ] ) || isnanf( y[ 1 ] ) ) {
73+
b.fail( 'should not return NaN' );
74+
}
75+
}
76+
b.toc();
77+
if ( isnanf( y[ 0 ] ) || isnanf( y[ 1 ] ) ) {
78+
b.fail( 'should not return NaN' );
79+
}
80+
b.pass( 'benchmark finished' );
81+
b.end();
82+
} );
83+
84+
bench( format( '%s:assign', pkg ), function benchmark( b ) {
85+
var x;
86+
var y;
87+
var i;
88+
89+
x = uniform( 100, -10.0, 10.0, {
90+
'dtype': 'float32'
91+
});
92+
y = [ 0.0, 0.0 ];
93+
94+
b.tic();
95+
for ( i = 0; i < b.iterations; i++ ) {
96+
sincosdf.assign( x[ i % x.length ], y, 1, 0 );
97+
if ( isnanf( y[ 0 ] ) || isnanf( y[ 1 ] ) ) {
98+
b.fail( 'should not return NaN' );
99+
}
100+
}
101+
b.toc();
102+
if ( isnanf( y[ 0 ] ) || isnanf( y[ 1 ] ) ) {
103+
b.fail( 'should not return NaN' );
104+
}
105+
b.pass( 'benchmark finished' );
106+
b.end();
107+
} );
108+
109+
bench( format( '%s::separate-evaluation,in-place', pkg ), function benchmark( b ) {
110+
var x;
111+
var y;
112+
var i;
113+
114+
x = uniform( 100, -10.0, 10.0, {
115+
'dtype': 'float32'
116+
});
117+
y = [ 0.0, 0.0 ];
118+
119+
b.tic();
120+
for ( i = 0; i < b.iterations; i++ ) {
121+
y[ 0 ] = sindf( x[ i % x.length ] );
122+
y[ 1 ] = cosdf( x[ i % x.length ] );
123+
if ( isnanf( y[ 0 ] ) || isnanf( y[ 1 ] ) ) {
124+
b.fail( 'should not return NaN' );
125+
}
126+
}
127+
b.toc();
128+
if ( isnanf( y[ 0 ] ) || isnanf( y[ 1 ] ) ) {
129+
b.fail( 'should not return NaN' );
130+
}
131+
b.pass( 'benchmark finished' );
132+
b.end();
133+
} );

0 commit comments

Comments
 (0)