Skip to content

Commit b947812

Browse files
fix: update math/base/special/cosd to match correct reference implementation
PR-URL: #5473 Co-authored-by: Gunj Joshi <gunjjoshi8372@gmail.com> Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com> Reviewed-by: Gunj Joshi <gunjjoshi8372@gmail.com> Signed-off-by: Gunj Joshi <gunjjoshi8372@gmail.com>
1 parent e1c3032 commit b947812

File tree

6 files changed

+68
-30
lines changed

6 files changed

+68
-30
lines changed

lib/node_modules/@stdlib/math/base/special/cosd/lib/main.js

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@
2020

2121
// MODULES //
2222

23-
var cos = require( '@stdlib/math/base/special/cos' );
2423
var deg2rad = require( '@stdlib/math/base/special/deg2rad' );
25-
var isInteger = require( '@stdlib/math/base/assert/is-integer' );
26-
var isInfinite = require( '@stdlib/assert/is-infinite' );
24+
var kernelSin = require( '@stdlib/math/base/special/kernel-sin' );
25+
var kernelCos = require( '@stdlib/math/base/special/kernel-cos' );
26+
var fmod = require( '@stdlib/math/base/special/fmod' );
27+
var abs = require( '@stdlib/math/base/special/abs' );
28+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
29+
var isInfinite = require( '@stdlib/math/base/assert/is-infinite' );
2730

2831

2932
// MAIN //
@@ -51,19 +54,30 @@ var isInfinite = require( '@stdlib/assert/is-infinite' );
5154
* // returns NaN
5255
*/
5356
function cosd( x ) {
54-
var xRad;
57+
var rx;
5558

56-
if ( isInfinite( x ) ) {
59+
if (
60+
isInfinite( x ) ||
61+
isnan( x )
62+
) {
5763
return NaN;
5864
}
5965

60-
if ( isInteger( ( ( x / 90.0 ) - 1.0 ) / 2.0 ) ) {
61-
return 0.0;
62-
}
63-
64-
xRad = deg2rad( x );
66+
rx = abs( fmod( x, 360.0 ) );
6567

66-
return cos( xRad );
68+
if ( rx <= 45.0 ) {
69+
return kernelCos( deg2rad( rx ), 0.0 );
70+
}
71+
if ( rx < 135.0 ) {
72+
return kernelSin( deg2rad( 90.0-rx ), 0.0 );
73+
}
74+
if ( rx <= 225.0 ) {
75+
return -kernelCos( deg2rad( 180.0-rx ), 0.0 );
76+
}
77+
if ( rx < 315.0 ) {
78+
return kernelSin( deg2rad( rx-270.0 ), 0.0 );
79+
}
80+
return kernelCos( deg2rad( 360.0-rx ), 0.0 );
6781
}
6882

6983

lib/node_modules/@stdlib/math/base/special/cosd/manifest.json

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,13 @@
3737
"libpath": [],
3838
"dependencies": [
3939
"@stdlib/math/base/napi/unary",
40-
"@stdlib/math/base/assert/is-integer",
40+
"@stdlib/math/base/assert/is-nan",
4141
"@stdlib/math/base/assert/is-infinite",
4242
"@stdlib/math/base/special/deg2rad",
43-
"@stdlib/math/base/special/cos"
43+
"@stdlib/math/base/special/kernel-sin",
44+
"@stdlib/math/base/special/kernel-cos",
45+
"@stdlib/math/base/special/abs",
46+
"@stdlib/math/base/special/fmod"
4447
]
4548
},
4649
{
@@ -54,10 +57,13 @@
5457
"libraries": [],
5558
"libpath": [],
5659
"dependencies": [
57-
"@stdlib/math/base/assert/is-integer",
60+
"@stdlib/math/base/assert/is-nan",
5861
"@stdlib/math/base/assert/is-infinite",
5962
"@stdlib/math/base/special/deg2rad",
60-
"@stdlib/math/base/special/cos"
63+
"@stdlib/math/base/special/kernel-sin",
64+
"@stdlib/math/base/special/kernel-cos",
65+
"@stdlib/math/base/special/abs",
66+
"@stdlib/math/base/special/fmod"
6167
]
6268
},
6369
{
@@ -71,10 +77,13 @@
7177
"libraries": [],
7278
"libpath": [],
7379
"dependencies": [
74-
"@stdlib/math/base/assert/is-integer",
80+
"@stdlib/math/base/assert/is-nan",
7581
"@stdlib/math/base/assert/is-infinite",
7682
"@stdlib/math/base/special/deg2rad",
77-
"@stdlib/math/base/special/cos"
83+
"@stdlib/math/base/special/kernel-sin",
84+
"@stdlib/math/base/special/kernel-cos",
85+
"@stdlib/math/base/special/abs",
86+
"@stdlib/math/base/special/fmod"
7887
]
7988
}
8089
]

lib/node_modules/@stdlib/math/base/special/cosd/src/main.c

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@
1717
*/
1818

1919
#include "stdlib/math/base/special/cosd.h"
20-
#include "stdlib/math/base/special/cos.h"
20+
#include "stdlib/math/base/special/kernel_sin.h"
21+
#include "stdlib/math/base/special/kernel_cos.h"
2122
#include "stdlib/math/base/special/deg2rad.h"
22-
#include "stdlib/math/base/assert/is_integer.h"
23+
#include "stdlib/math/base/special/abs.h"
24+
#include "stdlib/math/base/special/fmod.h"
25+
#include "stdlib/math/base/assert/is_nan.h"
2326
#include "stdlib/math/base/assert/is_infinite.h"
2427

2528
/**
@@ -33,13 +36,25 @@
3336
* // returns 1.0
3437
*/
3538
double stdlib_base_cosd( const double x ) {
36-
double xRad;
37-
if ( stdlib_base_is_infinite( x ) ) {
39+
double rx;
40+
41+
if ( stdlib_base_is_infinite( x ) || stdlib_base_is_nan( x ) ) {
3842
return 0.0 / 0.0; // NaN
3943
}
40-
if ( stdlib_base_is_integer( ( ( x / 90.0 ) - 1.0 ) / 2.0 ) ) {
41-
return 0.0;
44+
45+
rx = stdlib_base_abs( stdlib_base_fmod( x, 360.0 ) );
46+
47+
if ( rx <= 45.0 ) {
48+
return stdlib_base_kernel_cos( stdlib_base_deg2rad( rx ), 0.0 );
49+
}
50+
if ( rx < 135.0 ) {
51+
return stdlib_base_kernel_sin( stdlib_base_deg2rad( 90.0-rx ), 0.0 );
52+
}
53+
if ( rx <= 225.0 ) {
54+
return -stdlib_base_kernel_cos( stdlib_base_deg2rad( 180.0-rx ), 0.0 );
55+
}
56+
if ( rx < 315.0 ) {
57+
return stdlib_base_kernel_sin( stdlib_base_deg2rad( rx-270.0 ), 0.0 );
4258
}
43-
xRad = stdlib_base_deg2rad( x );
44-
return stdlib_base_cos( xRad );
59+
return stdlib_base_kernel_cos( stdlib_base_deg2rad( 360.0-rx ), 0.0 );
4560
}

lib/node_modules/@stdlib/math/base/special/cosd/test/fixtures/julia/negative.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

lib/node_modules/@stdlib/math/base/special/cosd/test/fixtures/julia/positive.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

lib/node_modules/@stdlib/math/base/special/cosd/test/fixtures/julia/runner.jl

100644100755
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import JSON
2020

2121
"""
22-
gen( domain, name )
22+
gen( domain, name )
2323
2424
Generate fixture data and write to file.
2525
@@ -62,9 +62,9 @@ file = @__FILE__;
6262
dir = dirname( file );
6363

6464
# Generate fixture data for negative values:
65-
x = range( -10.0, stop = 0, length = 1000 );
65+
x = range( -360.0, stop = 0, length = 1000 );
6666
gen( x, "negative.json" );
6767

6868
# Generate fixture data for positive values:
69-
x = range( 10.0, stop = 0, length = 1000 );
69+
x = range( 0.0, stop = 360.0, length = 1000 );
7070
gen( x, "positive.json" );

0 commit comments

Comments
 (0)