Skip to content

fix: update math/base/special/cotd to match correct reference implementation #5813

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 4 commits into from
Mar 7, 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
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import cotd = require( './index' );

// The function returns a number...
{
cotd( 60 ); // $ExpectType number
cotd( 60.0 ); // $ExpectType number
}

// The compiler throws an error if the function is provided a value other than a number...
Expand Down
26 changes: 6 additions & 20 deletions lib/node_modules/@stdlib/math/base/special/cotd/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@

// MODULES //

var cot = require( '@stdlib/math/base/special/cot' );
var isInteger = require( '@stdlib/math/base/assert/is-integer' );
var deg2rad = require( '@stdlib/math/base/special/deg2rad' );
var isInfinite = require( '@stdlib/assert/is-infinite' );
var sind = require( '@stdlib/math/base/special/sind' );
var cosd = require( '@stdlib/math/base/special/cosd' );


// MAIN //
Expand All @@ -39,35 +37,23 @@ var isInfinite = require( '@stdlib/assert/is-infinite' );
* // returns Infinity
*
* @example
* var v = cotd( 45 );
* var v = cotd( 45.0 );
* // returns 1.0
*
* @example
* var v = cotd( 90 );
* var v = cotd( 90.0 );
* // returns 0.0
*
* @example
* var v = cotd( 60 );
* var v = cotd( 60.0 );
* // returns ~0.58
*
* @example
* var v = cotd( NaN );
* // returns NaN
*/
function cotd( x ) {
var xRad;

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

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

xRad = deg2rad( x );

return cot( xRad );
return cosd( x ) / sind( x );
}


Expand Down
18 changes: 6 additions & 12 deletions lib/node_modules/@stdlib/math/base/special/cotd/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,8 @@
"libpath": [],
"dependencies": [
"@stdlib/math/base/napi/unary",
"@stdlib/math/base/special/deg2rad",
"@stdlib/math/base/special/cot",
"@stdlib/math/base/assert/is-integer",
"@stdlib/math/base/assert/is-infinite"
"@stdlib/math/base/special/sind",
"@stdlib/math/base/special/cosd"
]
},
{
Expand All @@ -54,10 +52,8 @@
"libraries": [],
"libpath": [],
"dependencies": [
"@stdlib/math/base/special/deg2rad",
"@stdlib/math/base/special/cot",
"@stdlib/math/base/assert/is-integer",
"@stdlib/math/base/assert/is-infinite"
"@stdlib/math/base/special/sind",
"@stdlib/math/base/special/cosd"
]
},
{
Expand All @@ -71,10 +67,8 @@
"libraries": [],
"libpath": [],
"dependencies": [
"@stdlib/math/base/special/deg2rad",
"@stdlib/math/base/special/cot",
"@stdlib/math/base/assert/is-integer",
"@stdlib/math/base/assert/is-infinite"
"@stdlib/math/base/special/sind",
"@stdlib/math/base/special/cosd"
]
}
]
Expand Down
16 changes: 3 additions & 13 deletions lib/node_modules/@stdlib/math/base/special/cotd/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@
*/

#include "stdlib/math/base/special/cotd.h"
#include "stdlib/math/base/special/cot.h"
#include "stdlib/math/base/special/deg2rad.h"
#include "stdlib/math/base/assert/is_integer.h"
#include "stdlib/math/base/assert/is_infinite.h"
#include "stdlib/math/base/special/sind.h"
#include "stdlib/math/base/special/cosd.h"

/**
* Computes the cotangent of an angle measured in degrees.
Expand All @@ -33,13 +31,5 @@
* // returns Infinity
*/
double stdlib_base_cotd( const double x ) {
double xRad;
if ( stdlib_base_is_infinite( x ) ) {
return 0.0 / 0.0; // NaN
}
if ( stdlib_base_is_integer( ( ( x / 90.0 ) - 1.0 ) / 2.0 ) ) {
return 0.0;
}
xRad = stdlib_base_deg2rad( x );
return stdlib_base_cot( xRad );
return stdlib_base_cosd( x ) / stdlib_base_sind( x );
}

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/cotd/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( -1.0, stop = -10.0, length = 1000 );
x = range( -179.0, stop = 0.0, length = 1000 );
gen( x, "negative.json" );

# Generate fixture data for positive values:
x = range( 1.0, stop = 10.0, length = 1000 );
x = range( 0.0, stop = 179.0, length = 1000 );
Comment on lines +65 to +69
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is because cotd( 180.0 ) and cotd( 0.0 ) produce different results. Therefore, 180.0 and -180.0 are tested separately.

gen( x, "positive.json" );
28 changes: 21 additions & 7 deletions lib/node_modules/@stdlib/math/base/special/cotd/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,15 @@ tape( 'the function computes the cotangent of an angle measured in degrees (nega

for ( i = 0; i < x.length; i++ ) {
y = cotd( x[i] );
if ( expected[ i ] === null ) {
t.equal( y, PINF, 'x: '+x[i]+'. E: '+expected[i] );
continue;
}
if ( y === expected[ i ] ) {
t.equal( y, expected[ i ], 'x: '+x[i]+'. E: '+expected[i] );
} else {
delta = abs( y - expected[i] );
tol = 1.4 * EPS * abs( expected[i] );
tol = 2.0 * EPS * abs( expected[i] );
t.ok( delta <= tol, 'within tolerance. x: '+x[i]+'. y: '+y+'. E: '+expected[i]+'. tol: '+tol+'. Δ: '+delta+'.' );
}
}
Expand All @@ -86,31 +90,41 @@ tape( 'the function computes the cotangent of an angle measured in degrees (posi

for ( i = 0; i < x.length; i++ ) {
y = cotd( x[i] );
if ( expected[ i ] === null ) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This check is included because cotd( 0.0 ) returns PINF, whereas Julia returns null.

Copy link
Member

Choose a reason for hiding this comment

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

Julia does return +Inf as well, it's just getting transformed to null when serialized to JSON:

image

t.equal( y, PINF, 'x: '+x[i]+'. E: '+expected[i] );
continue;
}
if ( y === expected[ i ] ) {
t.equal( y, expected[ i ], 'x: '+x[i]+'. E: '+expected[i] );
} else {
delta = abs( y - expected[i] );
tol = 1.4 * EPS * abs( expected[i] );
tol = 2.0 * EPS * abs( expected[i] );
t.ok( delta <= tol, 'within tolerance. x: '+x[i]+'. y: '+y+'. E: '+expected[i]+'. tol: '+tol+'. Δ: '+delta+'.' );
}
}
t.end();
});

tape( 'if provided `+infinity`, the function returns `NaN`', function test( t ) {
tape( 'if provided `+Infinity`, the function returns `NaN`', function test( t ) {
var v = cotd( PINF );
t.equal( isnan( v ), true, 'returns NaN' );
t.end();
});

tape( 'if provided `-infinity`, the function returns `NaN`', function test( t ) {
tape( 'if provided `-Infinity`, the function returns `NaN`', function test( t ) {
var v = cotd( NINF );
t.equal( isnan( v ), true, 'returns NaN' );
t.end();
});

tape( 'if provided `90.0`, the function returns `0.0`', function test( t ) {
var v = cotd( 90.0 );
t.equal( v, 0.0, 'returns expected value' );
tape( 'if provided `180.0`, the function returns `-Infinity`', function test( t ) {
var v = cotd( 180.0 );
t.equal( v, NINF, 'returns expected value' );
t.end();
});

tape( 'if provided `-180.0`, the function returns `+Infinity`', function test( t ) {
Comment on lines +120 to +126
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Over here 😄

var v = cotd( -180.0 );
t.equal( v, PINF, 'returns expected value' );
t.end();
});
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,15 @@ tape( 'the function computes the cotangent of an angle measured in degrees (nega

for ( i = 0; i < x.length; i++ ) {
y = cotd( x[i] );
if ( expected[ i ] === null ) {
t.equal( y, PINF, 'x: '+x[i]+'. E: '+expected[i] );
continue;
}
if ( y === expected[ i ] ) {
t.equal( y, expected[ i ], 'x: '+x[i]+'. E: '+expected[i] );
} else {
delta = abs( y - expected[i] );
tol = 1.4 * EPS * abs( expected[i] );
tol = 2.0 * EPS * abs( expected[i] );
t.ok( delta <= tol, 'within tolerance. x: '+x[i]+'. y: '+y+'. E: '+expected[i]+'. tol: '+tol+'. Δ: '+delta+'.' );
}
}
Expand All @@ -95,31 +99,41 @@ tape( 'the function computes the cotangent of an angle measured in degrees (posi

for ( i = 0; i < x.length; i++ ) {
y = cotd( x[i] );
if ( expected[ i ] === null ) {
t.equal( y, PINF, 'x: '+x[i]+'. E: '+expected[i] );
continue;
}
if ( y === expected[ i ] ) {
t.equal( y, expected[ i ], 'x: '+x[i]+'. E: '+expected[i] );
} else {
delta = abs( y - expected[i] );
tol = 1.4 * EPS * abs( expected[i] );
tol = 2.0 * EPS * abs( expected[i] );
t.ok( delta <= tol, 'within tolerance. x: '+x[i]+'. y: '+y+'. E: '+expected[i]+'. tol: '+tol+'. Δ: '+delta+'.' );
}
}
t.end();
});

tape( 'if provided `+infinity`, the function returns `NaN`', opts, function test( t ) {
tape( 'if provided `+Infinity`, the function returns `NaN`', opts, function test( t ) {
var v = cotd( PINF );
t.equal( isnan( v ), true, 'returns NaN' );
t.end();
});

tape( 'if provided `-infinity`, the function returns `NaN`', opts, function test( t ) {
tape( 'if provided `-Infinity`, the function returns `NaN`', opts, function test( t ) {
var v = cotd( NINF );
t.equal( isnan( v ), true, 'returns NaN' );
t.end();
});

tape( 'if provided `90.0`, the function returns `0.0`', opts, function test( t ) {
var v = cotd( 90.0 );
t.equal( v, 0.0, 'returns expected value' );
tape( 'if provided `180.0`, the function returns `-Infinity`', opts, function test( t ) {
var v = cotd( 180.0 );
t.equal( v, NINF, 'returns expected value' );
t.end();
});

tape( 'if provided `-180.0`, the function returns `+Infinity`', opts, function test( t ) {
var v = cotd( -180.0 );
t.equal( v, PINF, 'returns expected value' );
t.end();
});