Skip to content

Commit 75941b9

Browse files
fix: update math/base/special/cotd to match correct reference implementation
PR-URL: #5813 Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com>
1 parent eac21cf commit 75941b9

File tree

9 files changed

+63
-65
lines changed

9 files changed

+63
-65
lines changed

lib/node_modules/@stdlib/math/base/special/cotd/docs/types/test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import cotd = require( './index' );
2323

2424
// The function returns a number...
2525
{
26-
cotd( 60 ); // $ExpectType number
26+
cotd( 60.0 ); // $ExpectType number
2727
}
2828

2929
// The compiler throws an error if the function is provided a value other than a number...

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

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@
2020

2121
// MODULES //
2222

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

2826

2927
// MAIN //
@@ -39,35 +37,23 @@ var isInfinite = require( '@stdlib/assert/is-infinite' );
3937
* // returns Infinity
4038
*
4139
* @example
42-
* var v = cotd( 45 );
40+
* var v = cotd( 45.0 );
4341
* // returns 1.0
4442
*
4543
* @example
46-
* var v = cotd( 90 );
44+
* var v = cotd( 90.0 );
4745
* // returns 0.0
4846
*
4947
* @example
50-
* var v = cotd( 60 );
48+
* var v = cotd( 60.0 );
5149
* // returns ~0.58
5250
*
5351
* @example
5452
* var v = cotd( NaN );
5553
* // returns NaN
5654
*/
5755
function cotd( x ) {
58-
var xRad;
59-
60-
if ( isInfinite( x ) ) {
61-
return NaN;
62-
}
63-
64-
if ( isInteger( ( ( x / 90.0 ) - 1.0 ) / 2.0 ) ) {
65-
return 0.0;
66-
}
67-
68-
xRad = deg2rad( x );
69-
70-
return cot( xRad );
56+
return cosd( x ) / sind( x );
7157
}
7258

7359

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

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,8 @@
3737
"libpath": [],
3838
"dependencies": [
3939
"@stdlib/math/base/napi/unary",
40-
"@stdlib/math/base/special/deg2rad",
41-
"@stdlib/math/base/special/cot",
42-
"@stdlib/math/base/assert/is-integer",
43-
"@stdlib/math/base/assert/is-infinite"
40+
"@stdlib/math/base/special/sind",
41+
"@stdlib/math/base/special/cosd"
4442
]
4543
},
4644
{
@@ -54,10 +52,8 @@
5452
"libraries": [],
5553
"libpath": [],
5654
"dependencies": [
57-
"@stdlib/math/base/special/deg2rad",
58-
"@stdlib/math/base/special/cot",
59-
"@stdlib/math/base/assert/is-integer",
60-
"@stdlib/math/base/assert/is-infinite"
55+
"@stdlib/math/base/special/sind",
56+
"@stdlib/math/base/special/cosd"
6157
]
6258
},
6359
{
@@ -71,10 +67,8 @@
7167
"libraries": [],
7268
"libpath": [],
7369
"dependencies": [
74-
"@stdlib/math/base/special/deg2rad",
75-
"@stdlib/math/base/special/cot",
76-
"@stdlib/math/base/assert/is-integer",
77-
"@stdlib/math/base/assert/is-infinite"
70+
"@stdlib/math/base/special/sind",
71+
"@stdlib/math/base/special/cosd"
7872
]
7973
}
8074
]

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

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@
1717
*/
1818

1919
#include "stdlib/math/base/special/cotd.h"
20-
#include "stdlib/math/base/special/cot.h"
21-
#include "stdlib/math/base/special/deg2rad.h"
22-
#include "stdlib/math/base/assert/is_integer.h"
23-
#include "stdlib/math/base/assert/is_infinite.h"
20+
#include "stdlib/math/base/special/sind.h"
21+
#include "stdlib/math/base/special/cosd.h"
2422

2523
/**
2624
* Computes the cotangent of an angle measured in degrees.
@@ -33,13 +31,5 @@
3331
* // returns Infinity
3432
*/
3533
double stdlib_base_cotd( const double x ) {
36-
double xRad;
37-
if ( stdlib_base_is_infinite( x ) ) {
38-
return 0.0 / 0.0; // NaN
39-
}
40-
if ( stdlib_base_is_integer( ( ( x / 90.0 ) - 1.0 ) / 2.0 ) ) {
41-
return 0.0;
42-
}
43-
xRad = stdlib_base_deg2rad( x );
44-
return stdlib_base_cot( xRad );
34+
return stdlib_base_cosd( x ) / stdlib_base_sind( x );
4535
}

lib/node_modules/@stdlib/math/base/special/cotd/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/cotd/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/cotd/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( -1.0, stop = -10.0, length = 1000 );
65+
x = range( -179.0, stop = 0.0, length = 1000 );
6666
gen( x, "negative.json" );
6767

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

lib/node_modules/@stdlib/math/base/special/cotd/test/test.js

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,15 @@ tape( 'the function computes the cotangent of an angle measured in degrees (nega
6262

6363
for ( i = 0; i < x.length; i++ ) {
6464
y = cotd( x[i] );
65+
if ( expected[ i ] === null ) {
66+
t.equal( y, PINF, 'x: '+x[i]+'. E: '+expected[i] );
67+
continue;
68+
}
6569
if ( y === expected[ i ] ) {
6670
t.equal( y, expected[ i ], 'x: '+x[i]+'. E: '+expected[i] );
6771
} else {
6872
delta = abs( y - expected[i] );
69-
tol = 1.4 * EPS * abs( expected[i] );
73+
tol = 2.0 * EPS * abs( expected[i] );
7074
t.ok( delta <= tol, 'within tolerance. x: '+x[i]+'. y: '+y+'. E: '+expected[i]+'. tol: '+tol+'. Δ: '+delta+'.' );
7175
}
7276
}
@@ -86,31 +90,41 @@ tape( 'the function computes the cotangent of an angle measured in degrees (posi
8690

8791
for ( i = 0; i < x.length; i++ ) {
8892
y = cotd( x[i] );
93+
if ( expected[ i ] === null ) {
94+
t.equal( y, PINF, 'x: '+x[i]+'. E: '+expected[i] );
95+
continue;
96+
}
8997
if ( y === expected[ i ] ) {
9098
t.equal( y, expected[ i ], 'x: '+x[i]+'. E: '+expected[i] );
9199
} else {
92100
delta = abs( y - expected[i] );
93-
tol = 1.4 * EPS * abs( expected[i] );
101+
tol = 2.0 * EPS * abs( expected[i] );
94102
t.ok( delta <= tol, 'within tolerance. x: '+x[i]+'. y: '+y+'. E: '+expected[i]+'. tol: '+tol+'. Δ: '+delta+'.' );
95103
}
96104
}
97105
t.end();
98106
});
99107

100-
tape( 'if provided `+infinity`, the function returns `NaN`', function test( t ) {
108+
tape( 'if provided `+Infinity`, the function returns `NaN`', function test( t ) {
101109
var v = cotd( PINF );
102110
t.equal( isnan( v ), true, 'returns NaN' );
103111
t.end();
104112
});
105113

106-
tape( 'if provided `-infinity`, the function returns `NaN`', function test( t ) {
114+
tape( 'if provided `-Infinity`, the function returns `NaN`', function test( t ) {
107115
var v = cotd( NINF );
108116
t.equal( isnan( v ), true, 'returns NaN' );
109117
t.end();
110118
});
111119

112-
tape( 'if provided `90.0`, the function returns `0.0`', function test( t ) {
113-
var v = cotd( 90.0 );
114-
t.equal( v, 0.0, 'returns expected value' );
120+
tape( 'if provided `180.0`, the function returns `-Infinity`', function test( t ) {
121+
var v = cotd( 180.0 );
122+
t.equal( v, NINF, 'returns expected value' );
123+
t.end();
124+
});
125+
126+
tape( 'if provided `-180.0`, the function returns `+Infinity`', function test( t ) {
127+
var v = cotd( -180.0 );
128+
t.equal( v, PINF, 'returns expected value' );
115129
t.end();
116130
});

lib/node_modules/@stdlib/math/base/special/cotd/test/test.native.js

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,15 @@ tape( 'the function computes the cotangent of an angle measured in degrees (nega
7171

7272
for ( i = 0; i < x.length; i++ ) {
7373
y = cotd( x[i] );
74+
if ( expected[ i ] === null ) {
75+
t.equal( y, PINF, 'x: '+x[i]+'. E: '+expected[i] );
76+
continue;
77+
}
7478
if ( y === expected[ i ] ) {
7579
t.equal( y, expected[ i ], 'x: '+x[i]+'. E: '+expected[i] );
7680
} else {
7781
delta = abs( y - expected[i] );
78-
tol = 1.4 * EPS * abs( expected[i] );
82+
tol = 2.0 * EPS * abs( expected[i] );
7983
t.ok( delta <= tol, 'within tolerance. x: '+x[i]+'. y: '+y+'. E: '+expected[i]+'. tol: '+tol+'. Δ: '+delta+'.' );
8084
}
8185
}
@@ -95,31 +99,41 @@ tape( 'the function computes the cotangent of an angle measured in degrees (posi
9599

96100
for ( i = 0; i < x.length; i++ ) {
97101
y = cotd( x[i] );
102+
if ( expected[ i ] === null ) {
103+
t.equal( y, PINF, 'x: '+x[i]+'. E: '+expected[i] );
104+
continue;
105+
}
98106
if ( y === expected[ i ] ) {
99107
t.equal( y, expected[ i ], 'x: '+x[i]+'. E: '+expected[i] );
100108
} else {
101109
delta = abs( y - expected[i] );
102-
tol = 1.4 * EPS * abs( expected[i] );
110+
tol = 2.0 * EPS * abs( expected[i] );
103111
t.ok( delta <= tol, 'within tolerance. x: '+x[i]+'. y: '+y+'. E: '+expected[i]+'. tol: '+tol+'. Δ: '+delta+'.' );
104112
}
105113
}
106114
t.end();
107115
});
108116

109-
tape( 'if provided `+infinity`, the function returns `NaN`', opts, function test( t ) {
117+
tape( 'if provided `+Infinity`, the function returns `NaN`', opts, function test( t ) {
110118
var v = cotd( PINF );
111119
t.equal( isnan( v ), true, 'returns NaN' );
112120
t.end();
113121
});
114122

115-
tape( 'if provided `-infinity`, the function returns `NaN`', opts, function test( t ) {
123+
tape( 'if provided `-Infinity`, the function returns `NaN`', opts, function test( t ) {
116124
var v = cotd( NINF );
117125
t.equal( isnan( v ), true, 'returns NaN' );
118126
t.end();
119127
});
120128

121-
tape( 'if provided `90.0`, the function returns `0.0`', opts, function test( t ) {
122-
var v = cotd( 90.0 );
123-
t.equal( v, 0.0, 'returns expected value' );
129+
tape( 'if provided `180.0`, the function returns `-Infinity`', opts, function test( t ) {
130+
var v = cotd( 180.0 );
131+
t.equal( v, NINF, 'returns expected value' );
132+
t.end();
133+
});
134+
135+
tape( 'if provided `-180.0`, the function returns `+Infinity`', opts, function test( t ) {
136+
var v = cotd( -180.0 );
137+
t.equal( v, PINF, 'returns expected value' );
124138
t.end();
125139
});

0 commit comments

Comments
 (0)