|
| 1 | +/** |
| 2 | +* @license Apache-2.0 |
| 3 | +* |
| 4 | +* Copyright (c) 2025 The Stdlib Authors. |
| 5 | +* |
| 6 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +* you may not use this file except in compliance with the License. |
| 8 | +* You may obtain a copy of the License at |
| 9 | +* |
| 10 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +* |
| 12 | +* Unless required by applicable law or agreed to in writing, software |
| 13 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +* See the License for the specific language governing permissions and |
| 16 | +* limitations under the License. |
| 17 | +*/ |
| 18 | + |
| 19 | +'use strict'; |
| 20 | + |
| 21 | +// MODULES // |
| 22 | + |
| 23 | +var resolve = require( 'path' ).resolve; |
| 24 | +var tape = require( 'tape' ); |
| 25 | +var isnan = require( '@stdlib/math/base/assert/is-nan' ); |
| 26 | +var abs = require( '@stdlib/math/base/special/abs' ); |
| 27 | +var EPS = require( '@stdlib/constants/float64/eps' ); |
| 28 | +var rempio2 = require( '@stdlib/math/base/special/rempio2' ); |
| 29 | +var tryRequire = require( '@stdlib/utils/try-require' ); |
| 30 | + |
| 31 | + |
| 32 | +// VARIABLES // |
| 33 | + |
| 34 | +var kernelSincos = tryRequire( resolve( __dirname, './../lib/native.js' ) ); |
| 35 | +var opts = { |
| 36 | + 'skip': ( kernelSincos instanceof Error ) |
| 37 | +}; |
| 38 | + |
| 39 | + |
| 40 | +// FIXTURES // |
| 41 | + |
| 42 | +var smallRange = require( './fixtures/julia/small_range.json' ); |
| 43 | +var largePositive = require( './fixtures/julia/large_positive.json' ); |
| 44 | +var largeNegative = require( './fixtures/julia/large_negative.json' ); |
| 45 | + |
| 46 | + |
| 47 | +// TESTS // |
| 48 | + |
| 49 | +tape( 'main export is a function', opts, function test( t ) { |
| 50 | + t.ok( true, __filename ); |
| 51 | + t.equal( typeof kernelSincos, 'function', 'main export is a function' ); |
| 52 | + t.end(); |
| 53 | +}); |
| 54 | + |
| 55 | +tape( 'the function returns `NaN` if provided a `NaN`', opts, function test( t ) { |
| 56 | + var v; |
| 57 | + |
| 58 | + v = kernelSincos( NaN, 0.0 ); |
| 59 | + t.equal( isnan( v[0] ), true, 'returns expected value' ); |
| 60 | + t.equal( isnan( v[1] ), true, 'returns expected value' ); |
| 61 | + t.end(); |
| 62 | +}); |
| 63 | + |
| 64 | +tape( 'the function evaluates the sine and cosine for input values on the interval `[-pi/4, pi/4]`', opts, function test( t ) { |
| 65 | + var cosine; |
| 66 | + var delta; |
| 67 | + var sine; |
| 68 | + var tol; |
| 69 | + var x; |
| 70 | + var y; |
| 71 | + var i; |
| 72 | + |
| 73 | + x = smallRange.x; |
| 74 | + sine = smallRange.sine; |
| 75 | + cosine = smallRange.cosine; |
| 76 | + |
| 77 | + for ( i = 0; i < x.length; i++ ) { |
| 78 | + y = kernelSincos( x[i], 0.0 ); |
| 79 | + if ( y[0] === sine[ i ] ) { |
| 80 | + t.equal( y[0], sine[ i ], 'returns expected value' ); |
| 81 | + } else { |
| 82 | + delta = abs( y[0] - sine[i] ); |
| 83 | + |
| 84 | + // NOTE: the tolerance here is larger than for the JavaScript implementation due to compiler optimizations which may be performed resulting in result divergence. For discussion, see https://github.com/stdlib-js/stdlib/pull/2298#discussion_r1624765205 |
| 85 | + tol = EPS * abs( sine[i] ); |
| 86 | + t.ok( delta <= tol, 'within tolerance. x: '+x[i]+'. Value: '+y[0]+'. Expected: '+sine[i]+'. tol: '+tol+'. delta: '+delta+'.' ); |
| 87 | + } |
| 88 | + t.equal( y[1], cosine[ i ], 'returns expected value' ); |
| 89 | + } |
| 90 | + t.end(); |
| 91 | +}); |
| 92 | + |
| 93 | +tape( 'the function can be used to compute the sine and cosine for input values outside of `[-pi/4, pi/4]` after argument reduction via `rempio2` (positive)', opts, function test( t ) { |
| 94 | + var cosine; |
| 95 | + var delta; |
| 96 | + var sine; |
| 97 | + var tol; |
| 98 | + var out; |
| 99 | + var x; |
| 100 | + var y; |
| 101 | + var n; |
| 102 | + var i; |
| 103 | + |
| 104 | + y = [ 0.0, 0.0 ]; |
| 105 | + x = largePositive.x; |
| 106 | + sine = largePositive.sine; |
| 107 | + cosine = largePositive.cosine; |
| 108 | + for ( i = 0; i < x.length; i++ ) { |
| 109 | + n = rempio2( x[ i ], y ); |
| 110 | + switch ( n & 3 ) { |
| 111 | + case 0: |
| 112 | + out = kernelSincos( y[ 0 ], y[ 1 ] ); |
| 113 | + if ( out[0] === sine[ i ] ) { |
| 114 | + t.equal( out[0], sine[ i ], 'returns expected value' ); |
| 115 | + } else { |
| 116 | + delta = abs( out[0] - sine[i] ); |
| 117 | + |
| 118 | + // NOTE: the tolerance here is larger than for the JavaScript implementation due to compiler optimizations which may be performed resulting in result divergence. For discussion, see https://github.com/stdlib-js/stdlib/pull/2298#discussion_r1624765205 |
| 119 | + tol = EPS * abs( sine[i] ); |
| 120 | + t.ok( delta <= tol, 'within tolerance. x: '+x[i]+'. Value: '+out[0]+'. Expected: '+sine[i]+'. tol: '+tol+'. delta: '+delta+'.' ); |
| 121 | + } |
| 122 | + t.equal( out[1], cosine[ i ], 'returns expected value' ); |
| 123 | + break; |
| 124 | + case 2: |
| 125 | + out = kernelSincos( y[ 0 ], y[ 1 ] ); |
| 126 | + out[ 0 ] = -out[ 0 ]; |
| 127 | + out[ 1 ] = -out[ 1 ]; |
| 128 | + if ( out[0] === sine[ i ] ) { |
| 129 | + t.equal( out[0], sine[ i ], 'returns expected value' ); |
| 130 | + } else { |
| 131 | + delta = abs( out[0] - sine[i] ); |
| 132 | + |
| 133 | + // NOTE: the tolerance here is larger than for the JavaScript implementation due to compiler optimizations which may be performed resulting in result divergence. For discussion, see https://github.com/stdlib-js/stdlib/pull/2298#discussion_r1624765205 |
| 134 | + tol = EPS * abs( sine[i] ); |
| 135 | + t.ok( delta <= tol, 'within tolerance. x: '+x[i]+'. Value: '+out[0]+'. Expected: '+sine[i]+'. tol: '+tol+'. delta: '+delta+'.' ); |
| 136 | + } |
| 137 | + if ( out[1] === cosine[ i ] ) { |
| 138 | + t.equal( out[1], cosine[ i ], 'returns expected value' ); |
| 139 | + } else { |
| 140 | + delta = abs( out[1] - cosine[i] ); |
| 141 | + |
| 142 | + // NOTE: the tolerance here is larger than for the JavaScript implementation due to compiler optimizations which may be performed resulting in result divergence. For discussion, see https://github.com/stdlib-js/stdlib/pull/2298#discussion_r1624765205 |
| 143 | + tol = EPS * abs( cosine[i] ); |
| 144 | + t.ok( delta <= tol, 'within tolerance. x: '+x[i]+'. Value: '+out[1]+'. Expected: '+cosine[i]+'. tol: '+tol+'. delta: '+delta+'.' ); |
| 145 | + } |
| 146 | + break; |
| 147 | + default: |
| 148 | + break; |
| 149 | + } |
| 150 | + } |
| 151 | + t.end(); |
| 152 | +}); |
| 153 | + |
| 154 | +tape( 'the function can be used to compute the sine and cosine for input values outside of `[-pi/4, pi/4]` after argument reduction via `rempio2` (negative)', opts, function test( t ) { |
| 155 | + var cosine; |
| 156 | + var delta; |
| 157 | + var sine; |
| 158 | + var tol; |
| 159 | + var out; |
| 160 | + var x; |
| 161 | + var y; |
| 162 | + var n; |
| 163 | + var i; |
| 164 | + |
| 165 | + y = [ 0.0, 0.0 ]; |
| 166 | + x = largeNegative.x; |
| 167 | + sine = largeNegative.sine; |
| 168 | + cosine = largeNegative.cosine; |
| 169 | + for ( i = 0; i < x.length; i++ ) { |
| 170 | + n = rempio2( x[ i ], y ); |
| 171 | + switch ( n & 3 ) { |
| 172 | + case 0: |
| 173 | + out = kernelSincos( y[ 0 ], y[ 1 ] ); |
| 174 | + if ( out[0] === sine[ i ] ) { |
| 175 | + t.equal( out[0], sine[ i ], 'returns expected value' ); |
| 176 | + } else { |
| 177 | + delta = abs( out[0] - sine[i] ); |
| 178 | + |
| 179 | + // NOTE: the tolerance here is larger than for the JavaScript implementation due to compiler optimizations which may be performed resulting in result divergence. For discussion, see https://github.com/stdlib-js/stdlib/pull/2298#discussion_r1624765205 |
| 180 | + tol = EPS * abs( sine[i] ); |
| 181 | + t.ok( delta <= tol, 'within tolerance. x: '+x[i]+'. Value: '+out[0]+'. Expected: '+sine[i]+'. tol: '+tol+'. delta: '+delta+'.' ); |
| 182 | + } |
| 183 | + t.equal( out[1], cosine[ i ], 'returns expected value' ); |
| 184 | + break; |
| 185 | + case 2: |
| 186 | + out = kernelSincos( y[ 0 ], y[ 1 ] ); |
| 187 | + out[ 0 ] = -out[ 0 ]; |
| 188 | + out[ 1 ] = -out[ 1 ]; |
| 189 | + if ( out[0] === sine[ i ] ) { |
| 190 | + t.equal( out[0], sine[ i ], 'returns expected value' ); |
| 191 | + } else { |
| 192 | + delta = abs( out[0] - sine[i] ); |
| 193 | + |
| 194 | + // NOTE: the tolerance here is larger than for the JavaScript implementation due to compiler optimizations which may be performed resulting in result divergence. For discussion, see https://github.com/stdlib-js/stdlib/pull/2298#discussion_r1624765205 |
| 195 | + tol = EPS * abs( sine[i] ); |
| 196 | + t.ok( delta <= tol, 'within tolerance. x: '+x[i]+'. Value: '+out[0]+'. Expected: '+sine[i]+'. tol: '+tol+'. delta: '+delta+'.' ); |
| 197 | + } |
| 198 | + if ( out[1] === cosine[ i ] ) { |
| 199 | + t.equal( out[1], cosine[ i ], 'returns expected value' ); |
| 200 | + } else { |
| 201 | + delta = abs( out[1] - cosine[i] ); |
| 202 | + |
| 203 | + // NOTE: the tolerance here is larger than for the JavaScript implementation due to compiler optimizations which may be performed resulting in result divergence. For discussion, see https://github.com/stdlib-js/stdlib/pull/2298#discussion_r1624765205 |
| 204 | + tol = EPS * abs( cosine[i] ); |
| 205 | + t.ok( delta <= tol, 'within tolerance. x: '+x[i]+'. Value: '+out[1]+'. Expected: '+cosine[i]+'. tol: '+tol+'. delta: '+delta+'.' ); |
| 206 | + } |
| 207 | + break; |
| 208 | + default: |
| 209 | + break; |
| 210 | + } |
| 211 | + } |
| 212 | + t.end(); |
| 213 | +}); |
0 commit comments