Skip to content

fix: update math/base/special/cosd to match correct reference implementation #5473

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 25 additions & 11 deletions lib/node_modules/@stdlib/math/base/special/cosd/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@

// MODULES //

var cos = require( '@stdlib/math/base/special/cos' );
var deg2rad = require( '@stdlib/math/base/special/deg2rad' );
var isInteger = require( '@stdlib/math/base/assert/is-integer' );
var isInfinite = require( '@stdlib/assert/is-infinite' );
var kernelSin = require( '@stdlib/math/base/special/kernel-sin' );
var kernelCos = require( '@stdlib/math/base/special/kernel-cos' );
var fmod = require( '@stdlib/math/base/special/fmod' );
var abs = require( '@stdlib/math/base/special/abs' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var isInfinite = require( '@stdlib/math/base/assert/is-infinite' );


// MAIN //
Expand Down Expand Up @@ -51,19 +54,30 @@ var isInfinite = require( '@stdlib/assert/is-infinite' );
* // returns NaN
*/
function cosd( x ) {
var xRad;
var rx;

if ( isInfinite( x ) ) {
if (
isInfinite( x ) ||
isnan( x )
) {
return NaN;
}

if ( isInteger( ( ( x / 90.0 ) - 1.0 ) / 2.0 ) ) {
return 0.0;
}

xRad = deg2rad( x );
rx = abs( fmod( x, 360.0 ) );

return cos( xRad );
if ( rx <= 45.0 ) {
return kernelCos( deg2rad( rx ), 0.0 );
}
if ( rx < 135.0 ) {
return kernelSin( deg2rad( 90.0-rx ), 0.0 );
}
if ( rx <= 225.0 ) {
return -kernelCos( deg2rad( 180.0-rx ), 0.0 );
}
if ( rx < 315.0 ) {
return kernelSin( deg2rad( rx-270.0 ), 0.0 );
}
return kernelCos( deg2rad( 360.0-rx ), 0.0 );
}


Expand Down
21 changes: 15 additions & 6 deletions lib/node_modules/@stdlib/math/base/special/cosd/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,13 @@
"libpath": [],
"dependencies": [
"@stdlib/math/base/napi/unary",
"@stdlib/math/base/assert/is-integer",
"@stdlib/math/base/assert/is-nan",
"@stdlib/math/base/assert/is-infinite",
"@stdlib/math/base/special/deg2rad",
"@stdlib/math/base/special/cos"
"@stdlib/math/base/special/kernel-sin",
"@stdlib/math/base/special/kernel-cos",
"@stdlib/math/base/special/abs",
"@stdlib/math/base/special/fmod"
]
},
{
Expand All @@ -54,10 +57,13 @@
"libraries": [],
"libpath": [],
"dependencies": [
"@stdlib/math/base/assert/is-integer",
"@stdlib/math/base/assert/is-nan",
"@stdlib/math/base/assert/is-infinite",
"@stdlib/math/base/special/deg2rad",
"@stdlib/math/base/special/cos"
"@stdlib/math/base/special/kernel-sin",
"@stdlib/math/base/special/kernel-cos",
"@stdlib/math/base/special/abs",
"@stdlib/math/base/special/fmod"
]
},
{
Expand All @@ -71,10 +77,13 @@
"libraries": [],
"libpath": [],
"dependencies": [
"@stdlib/math/base/assert/is-integer",
"@stdlib/math/base/assert/is-nan",
"@stdlib/math/base/assert/is-infinite",
"@stdlib/math/base/special/deg2rad",
"@stdlib/math/base/special/cos"
"@stdlib/math/base/special/kernel-sin",
"@stdlib/math/base/special/kernel-cos",
"@stdlib/math/base/special/abs",
"@stdlib/math/base/special/fmod"
]
}
]
Expand Down
31 changes: 23 additions & 8 deletions lib/node_modules/@stdlib/math/base/special/cosd/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@
*/

#include "stdlib/math/base/special/cosd.h"
#include "stdlib/math/base/special/cos.h"
#include "stdlib/math/base/special/kernel_sin.h"
#include "stdlib/math/base/special/kernel_cos.h"
#include "stdlib/math/base/special/deg2rad.h"
#include "stdlib/math/base/assert/is_integer.h"
#include "stdlib/math/base/special/abs.h"
#include "stdlib/math/base/special/fmod.h"
#include "stdlib/math/base/assert/is_nan.h"
#include "stdlib/math/base/assert/is_infinite.h"

/**
Expand All @@ -33,13 +36,25 @@
* // returns 1.0
*/
double stdlib_base_cosd( const double x ) {
double xRad;
if ( stdlib_base_is_infinite( x ) ) {
double rx;

if ( stdlib_base_is_infinite( x ) || stdlib_base_is_nan( x ) ) {
return 0.0 / 0.0; // NaN
}
if ( stdlib_base_is_integer( ( ( x / 90.0 ) - 1.0 ) / 2.0 ) ) {
return 0.0;

rx = stdlib_base_abs( stdlib_base_fmod( x, 360.0 ) );

if ( rx <= 45.0 ) {
return stdlib_base_kernel_cos( stdlib_base_deg2rad( rx ), 0.0 );
}
if ( rx < 135.0 ) {
return stdlib_base_kernel_sin( stdlib_base_deg2rad( 90.0-rx ), 0.0 );
}
if ( rx <= 225.0 ) {
return -stdlib_base_kernel_cos( stdlib_base_deg2rad( 180.0-rx ), 0.0 );
}
if ( rx < 315.0 ) {
return stdlib_base_kernel_sin( stdlib_base_deg2rad( rx-270.0 ), 0.0 );
}
xRad = stdlib_base_deg2rad( x );
return stdlib_base_cos( xRad );
return stdlib_base_kernel_cos( stdlib_base_deg2rad( 360.0-rx ), 0.0 );
}

Large diffs are not rendered by default.

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions lib/node_modules/@stdlib/math/base/special/cosd/test/fixtures/julia/runner.jl
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import JSON

"""
gen( domain, name )
gen( domain, name )

Generate fixture data and write to file.

Expand Down Expand Up @@ -62,9 +62,9 @@ file = @__FILE__;
dir = dirname( file );

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

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