Skip to content

Commit 0b0e240

Browse files
feat: add math/base/special/cosd
PR-URL: #1777 Closes: #33 --------- Signed-off-by: Sai Srikar Dumpeti <[email protected]> Signed-off-by: Philipp Burckhardt <[email protected]> Co-authored-by: Philipp Burckhardt <[email protected]> Reviewed-by: Philipp Burckhardt <[email protected]>
1 parent 5b5ce15 commit 0b0e240

File tree

14 files changed

+673
-0
lines changed

14 files changed

+673
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 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+
# cosd
22+
23+
> Computes the [cosine][trigonometric-functions] of an angle measured in degrees.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<section class="usage">
30+
31+
## Usage
32+
33+
```javascript
34+
var cosd = require( '@stdlib/math/base/special/cosd' );
35+
```
36+
37+
#### cosd( x )
38+
39+
Computes the [cosine][trigonometric-functions] of `x` (in degrees).
40+
41+
```javascript
42+
var v = cosd( 0.0 );
43+
// returns 1.0
44+
45+
v = cosd( 60.0 );
46+
// returns ~0.5
47+
48+
v = cosd( 90.0 );
49+
// returns 0.0
50+
51+
v = cosd( NaN );
52+
// returns NaN
53+
```
54+
55+
</section>
56+
57+
<!-- /.usage -->
58+
59+
<section class="examples">
60+
61+
## Examples
62+
63+
<!-- eslint no-undef: "error" -->
64+
65+
```javascript
66+
var linspace = require( '@stdlib/array/base/linspace' );
67+
var cosd = require( '@stdlib/math/base/special/cosd' );
68+
69+
var x = linspace( -180, 180, 100 );
70+
71+
var i;
72+
for ( i = 0; i < x.length; i++ ) {
73+
console.log( cosd( x[ i ] ) );
74+
}
75+
```
76+
77+
</section>
78+
79+
<!-- /.examples -->
80+
81+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
82+
83+
<section class="related">
84+
85+
</section>
86+
87+
<!-- /.related -->
88+
89+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
90+
91+
<section class="links">
92+
93+
[trigonometric-functions]: https://en.wikipedia.org/wiki/Trigonometric_functions
94+
95+
<!-- <related-links> -->
96+
97+
<!-- </related-links> -->
98+
99+
</section>
100+
101+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 randu = require( '@stdlib/random/base/randu' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pkg = require( './../package.json' ).name;
27+
var cosd = 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 = ( randu()*10.0 ) - 5.0;
40+
y = cosd( x );
41+
if ( isnan( y ) ) {
42+
b.fail( 'should not return NaN' );
43+
}
44+
}
45+
b.toc();
46+
if ( isnan( y ) ) {
47+
b.fail( 'should not return NaN' );
48+
}
49+
b.pass( 'benchmark finished' );
50+
b.end();
51+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
{{alias}}( x )
3+
Computes the cosine of an angle measured in degrees.
4+
5+
Parameters
6+
----------
7+
x: number
8+
Input value (in degrees).
9+
10+
Returns
11+
-------
12+
y: number
13+
Cosine.
14+
15+
Examples
16+
--------
17+
> var y = {{alias}}( 0.0 )
18+
1.0
19+
> y = {{alias}}( 90.0 )
20+
0.0
21+
> y = {{alias}}( 60.0 )
22+
~0.5
23+
> y = {{alias}}( NaN )
24+
NaN
25+
26+
See Also
27+
--------
28+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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+
// TypeScript Version: 4.1
20+
21+
/**
22+
* Computes the cosine of an angle measured in degrees.
23+
*
24+
* @param x - input value (in degrees)
25+
* @returns cosine
26+
*
27+
* @example
28+
* var v = cosd( 0.0 );
29+
* // returns 1.0
30+
*
31+
* @example
32+
* var v = cosd( 60.0 );
33+
* // returns ~0.5
34+
*
35+
* @example
36+
* var v = cosd( 90.0);
37+
* // returns 0
38+
*
39+
* @example
40+
* var v = cosd( NaN );
41+
* // returns NaN
42+
*/
43+
declare function cosd( x: number ): number;
44+
45+
46+
// EXPORTS //
47+
48+
export = cosd;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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+
import cosd = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a number...
25+
{
26+
cosd( 60.0 ); // $ExpectType number
27+
}
28+
29+
// The compiler throws an error if the function is provided a value other than a number...
30+
{
31+
cosd( true ); // $ExpectError
32+
cosd( false ); // $ExpectError
33+
cosd( null ); // $ExpectError
34+
cosd( undefined ); // $ExpectError
35+
cosd( '5' ); // $ExpectError
36+
cosd( [] ); // $ExpectError
37+
cosd( {} ); // $ExpectError
38+
cosd( ( x: number ): number => x ); // $ExpectError
39+
}
40+
41+
// The compiler throws an error if the function is provided insufficient arguments...
42+
{
43+
cosd(); // $ExpectError
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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+
var linspace = require( '@stdlib/array/base/linspace' );
22+
var cosd = require( './../lib' );
23+
24+
var x = linspace( -180, 180, 100 );
25+
26+
var i;
27+
for ( i = 0; i < x.length; i++ ) {
28+
console.log( 'cosd(%d) = %d', x[ i ], cosd( x[ i ] ) );
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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+
/**
22+
* Compute the cosine of an angle measured in degrees.
23+
*
24+
* @module @stdlib/math/base/special/cosd
25+
*
26+
* @example
27+
* var cosd = require( '@stdlib/math/base/special/cosd' );
28+
*
29+
* var v = cosd( 0.0 );
30+
* // returns 1.0
31+
*
32+
* v = cosd( 90.0 );
33+
* // returns 0.0
34+
*
35+
* v = cosd( 60.0 );
36+
* // returns ~0.5
37+
*
38+
* v = cosd( NaN );
39+
* // returns NaN
40+
*/
41+
42+
// MODULES //
43+
44+
var main = require( './main.js' );
45+
46+
47+
// EXPORTS //
48+
49+
module.exports = main;

0 commit comments

Comments
 (0)