Skip to content

Commit d774fe6

Browse files
gunjjoshikgryte
andauthored
feat: add math/base/special/rad2degf
PR-URL: #2127 Closes: #2124 Reviewed-by: Athan Reines <[email protected]> Signed-off-by: GUNJ JOSHI <[email protected]> Signed-off-by: Athan Reines <[email protected]> Co-authored-by: Athan Reines <[email protected]>
1 parent 20f26c2 commit d774fe6

File tree

27 files changed

+2020
-0
lines changed

27 files changed

+2020
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
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+
# rad2degf
22+
23+
> Convert an angle from radians to degrees (single-precision).
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var rad2degf = require( '@stdlib/math/base/special/rad2degf' );
31+
```
32+
33+
#### rad2degf( x )
34+
35+
Converts an angle from radians to degrees (single-precision).
36+
37+
```javascript
38+
var d = rad2degf( 3.141592653589793 / 2.0 );
39+
// returns 90.0
40+
41+
d = rad2degf( -3.141592653589793 / 4.0 );
42+
// returns -45.0
43+
44+
d = rad2degf( NaN );
45+
// returns NaN
46+
```
47+
48+
</section>
49+
50+
<!-- /.usage -->
51+
52+
<section class="notes">
53+
54+
## Notes
55+
56+
- Due to finite precision, canonical values may not be returned. For example,
57+
58+
```javascript
59+
var d = rad2degf( 3.141592653589793 / 6.0 );
60+
// returns 30.000001907348633
61+
```
62+
63+
</section>
64+
65+
<!-- /.notes -->
66+
67+
<section class="examples">
68+
69+
## Examples
70+
71+
<!-- eslint no-undef: "error" -->
72+
73+
```javascript
74+
var randu = require( '@stdlib/random/base/randu' );
75+
var TWO_PI = require( '@stdlib/constants/float32/two-pi' );
76+
var rad2degf = require( '@stdlib/math/base/special/rad2degf' );
77+
78+
var r;
79+
var d;
80+
var i;
81+
82+
for ( i = 0; i < 100; i++ ) {
83+
r = randu() * TWO_PI;
84+
d = rad2degf( r );
85+
console.log( 'radians: %d => degrees: %d', r, d );
86+
}
87+
```
88+
89+
</section>
90+
91+
<!-- /.examples -->
92+
93+
<!-- C interface documentation. -->
94+
95+
* * *
96+
97+
<section class="c">
98+
99+
## C APIs
100+
101+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
102+
103+
<section class="intro">
104+
105+
</section>
106+
107+
<!-- /.intro -->
108+
109+
<!-- C usage documentation. -->
110+
111+
<section class="usage">
112+
113+
### Usage
114+
115+
```c
116+
#include "stdlib/math/base/special/rad2degf.h"
117+
```
118+
119+
#### stdlib_base_rad2degf( x )
120+
121+
Converts an angle from radians to degrees (single-precision).
122+
123+
```c
124+
float x = 3.141592653589793f / 2.0f;
125+
float d = stdlib_base_rad2degf( x );
126+
// returns 90.0f
127+
128+
x = -3.141592653589793f / 4.0f;
129+
d = stdlib_base_rad2degf( x );
130+
// returns -45.0f
131+
```
132+
133+
The function accepts the following arguments:
134+
135+
- **x**: `[in] float` input value (in radians).
136+
137+
```c
138+
float stdlib_base_rad2degf( const float x );
139+
```
140+
141+
</section>
142+
143+
<!-- /.usage -->
144+
145+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
146+
147+
<section class="notes">
148+
149+
</section>
150+
151+
<!-- /.notes -->
152+
153+
<!-- C API usage examples. -->
154+
155+
<section class="examples">
156+
157+
### Examples
158+
159+
```c
160+
#include "stdlib/math/base/special/rad2degf.h"
161+
#include "stdlib/constants/float32/two_pi.h"
162+
#include <stdlib.h>
163+
#include <stdio.h>
164+
165+
int main( void ) {
166+
float x;
167+
float d;
168+
int i;
169+
170+
for ( i = 0; i < 100; i++ ) {
171+
x = (float)rand() / (float)RAND_MAX * STDLIB_CONSTANT_FLOAT32_TWO_PI;
172+
d = stdlib_base_rad2degf( x );
173+
printf( "radians: %f => degrees: %f\n", x, d );
174+
}
175+
}
176+
```
177+
178+
</section>
179+
180+
<!-- /.examples -->
181+
182+
</section>
183+
184+
<!-- /.c -->
185+
186+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
187+
188+
<section class="related">
189+
190+
</section>
191+
192+
<!-- /.related -->
193+
194+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
195+
196+
<section class="links">
197+
198+
</section>
199+
200+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 isnanf = require( '@stdlib/math/base/assert/is-nanf' );
26+
var TWO_PI = require( '@stdlib/constants/float32/two-pi' );
27+
var pkg = require( './../package.json' ).name;
28+
var rad2degf = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( pkg, function benchmark( b ) {
34+
var x;
35+
var y;
36+
var i;
37+
38+
b.tic();
39+
for ( i = 0; i < b.iterations; i++ ) {
40+
x = ( randu() * TWO_PI ) - 0.0;
41+
y = rad2degf( x );
42+
if ( isnanf( y ) ) {
43+
b.fail( 'should not return NaN' );
44+
}
45+
}
46+
b.toc();
47+
if ( isnanf( y ) ) {
48+
b.fail( 'should not return NaN' );
49+
}
50+
b.pass( 'benchmark finished' );
51+
b.end();
52+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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 resolve = require( 'path' ).resolve;
24+
var bench = require( '@stdlib/bench' );
25+
var randu = require( '@stdlib/random/base/randu' );
26+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
27+
var TWO_PI = require( '@stdlib/constants/float32/two-pi' );
28+
var tryRequire = require( '@stdlib/utils/try-require' );
29+
var pkg = require( './../package.json' ).name;
30+
31+
32+
// VARIABLES //
33+
34+
var rad2degf = tryRequire( resolve( __dirname, './../lib/native.js' ) );
35+
var opts = {
36+
'skip': ( rad2degf instanceof Error )
37+
};
38+
39+
40+
// MAIN //
41+
42+
bench( pkg+'::native', 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 = ( randu() * TWO_PI ) - 0.0;
50+
y = rad2degf( 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)