Skip to content

Commit

Permalink
fix: update math/base/special/cosd to match correct reference imple…
Browse files Browse the repository at this point in the history
…mentation

PR-URL: #5473

Co-authored-by: Gunj Joshi <[email protected]>
Reviewed-by: Philipp Burckhardt <[email protected]>
Reviewed-by: Gunj Joshi <[email protected]>
Signed-off-by: Gunj Joshi <[email protected]>
  • Loading branch information
anandkaranubc and gunjjoshi authored Mar 4, 2025
1 parent e1c3032 commit b947812
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 30 deletions.
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" );

1 comment on commit b947812

@stdlib-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage Report

Package Statements Branches Functions Lines
math/base/special/cosd $\color{green}193/193$
$\color{green}+100.00\%$
$\color{green}16/16$
$\color{green}+100.00\%$
$\color{green}2/2$
$\color{green}+100.00\%$
$\color{green}193/193$
$\color{green}+100.00\%$

The above coverage report was generated for the changes in this push.

Please sign in to comment.