sin-cos.js 10.2 KB
Newer Older
1
// Copyright 2011 the V8 project authors. All rights reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

// Test Math.sin and Math.cos.

30
// Flags: --allow-natives-syntax --opt
31

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
assertEquals("-Infinity", String(1/Math.sin(-0)));
assertEquals(1, Math.cos(-0));
assertEquals("-Infinity", String(1/Math.tan(-0)));

// Assert that minus zero does not cause deopt.
function no_deopt_on_minus_zero(x) {
  return Math.sin(x) + Math.cos(x) + Math.tan(x);
}

no_deopt_on_minus_zero(1);
no_deopt_on_minus_zero(1);
%OptimizeFunctionOnNextCall(no_deopt_on_minus_zero);
no_deopt_on_minus_zero(-0);
assertOptimized(no_deopt_on_minus_zero);


48 49 50 51
function sinTest() {
  assertEquals(0, Math.sin(0));
  assertEquals(1, Math.sin(Math.PI / 2));
}
52

53
function cosTest() {
54
  assertEquals(1, Math.cos(0));
55 56
  assertEquals(-1, Math.cos(Math.PI));
}
57

58 59
sinTest();
cosTest();
60 61 62

// By accident, the slow case for sine and cosine were both sine at
// some point.  This is a regression test for that issue.
63
var x = Math.pow(2, 30);
64
assertTrue(Math.sin(x) != Math.cos(x));
65 66 67 68

// Ensure that sine and log are not the same.
x = 0.5;
assertTrue(Math.sin(x) != Math.log(x));
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117

// Test against approximation by series.
var factorial = [1];
var accuracy = 50;
for (var i = 1; i < accuracy; i++) {
  factorial[i] = factorial[i-1] * i;
}

// We sum up in the reverse order for higher precision, as we expect the terms
// to grow smaller for x reasonably close to 0.
function precision_sum(array) {
  var result = 0;
  while (array.length > 0) {
    result += array.pop();
  }
  return result;
}

function sin(x) {
  var sign = 1;
  var x2 = x*x;
  var terms = [];
  for (var i = 1; i < accuracy; i += 2) {
    terms.push(sign * x / factorial[i]);
    x *= x2;
    sign *= -1;
  }
  return precision_sum(terms);
}

function cos(x) {
  var sign = -1;
  var x2 = x*x;
  x = x2;
  var terms = [1];
  for (var i = 2; i < accuracy; i += 2) {
    terms.push(sign * x / factorial[i]);
    x *= x2;
    sign *= -1;
  }
  return precision_sum(terms);
}

function abs_error(fun, ref, x) {
  return Math.abs(ref(x) - fun(x));
}

var test_inputs = [];
for (var i = -10000; i < 10000; i += 177) test_inputs.push(i/1257);
118
var epsilon = 0.0000001;
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137

test_inputs.push(0);
test_inputs.push(0 + epsilon);
test_inputs.push(0 - epsilon);
test_inputs.push(Math.PI/2);
test_inputs.push(Math.PI/2 + epsilon);
test_inputs.push(Math.PI/2 - epsilon);
test_inputs.push(Math.PI);
test_inputs.push(Math.PI + epsilon);
test_inputs.push(Math.PI - epsilon);
test_inputs.push(- 2*Math.PI);
test_inputs.push(- 2*Math.PI + epsilon);
test_inputs.push(- 2*Math.PI - epsilon);

var squares = [];
for (var i = 0; i < test_inputs.length; i++) {
  var x = test_inputs[i];
  var err_sin = abs_error(Math.sin, sin, x);
  var err_cos = abs_error(Math.cos, cos, x)
138 139
  assertEqualsDelta(0, err_sin, 1E-13);
  assertEqualsDelta(0, err_cos, 1E-13);
140 141 142 143 144 145 146 147 148 149 150 151 152
  squares.push(err_sin*err_sin + err_cos*err_cos);
}

// Sum squares up by adding them pairwise, to avoid losing precision.
while (squares.length > 1) {
  var reduced = [];
  if (squares.length % 2 == 1) reduced.push(squares.pop());
  // Remaining number of elements is even.
  while(squares.length > 1) reduced.push(squares.pop() + squares.pop());
  squares = reduced;
}

var err_rms = Math.sqrt(squares[0] / test_inputs.length / 2);
153
assertEqualsDelta(0, err_rms, 1E-14);
154 155 156 157 158 159

assertEquals(-1, Math.cos({ valueOf: function() { return Math.PI; } }));
assertEquals(0, Math.sin("0x00000"));
assertEquals(1, Math.cos("0x00000"));
assertTrue(isNaN(Math.sin(Infinity)));
assertTrue(isNaN(Math.cos("-Infinity")));
160 161
assertTrue(Math.tan(Math.PI/2) > 1e16);
assertTrue(Math.tan(-Math.PI/2) < -1e16);
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
assertEquals("-Infinity", String(1/Math.sin("-0")));

// Assert that the remainder after division by pi is reasonably precise.
function assertError(expected, x, epsilon) {
  assertTrue(Math.abs(x - expected) < epsilon);
}

assertEqualsDelta(0.9367521275331447,  Math.cos(1e06),  1e-15);
assertEqualsDelta(0.8731196226768560,  Math.cos(1e10),  1e-08);
assertEqualsDelta(0.9367521275331447,  Math.cos(-1e06), 1e-15);
assertEqualsDelta(0.8731196226768560,  Math.cos(-1e10), 1e-08);
assertEqualsDelta(-0.3499935021712929, Math.sin(1e06),  1e-15);
assertEqualsDelta(-0.4875060250875106, Math.sin(1e10),  1e-08);
assertEqualsDelta(0.3499935021712929,  Math.sin(-1e06), 1e-15);
assertEqualsDelta(0.4875060250875106,  Math.sin(-1e10), 1e-08);
assertEqualsDelta(0.7796880066069787,  Math.sin(1e16),  1e-05);
assertEqualsDelta(-0.6261681981330861, Math.cos(1e16),  1e-05);

// Assert that remainder calculation terminates.
for (var i = -1024; i < 1024; i++) {
  assertFalse(isNaN(Math.sin(Math.pow(2, i))));
}

assertFalse(isNaN(Math.cos(1.57079632679489700)));
assertFalse(isNaN(Math.cos(-1e-100)));
assertFalse(isNaN(Math.cos(-1e-323)));
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229

// Tests for specific values expected from the fdlibm implementation.

var two_32 = Math.pow(2, -32);
var two_28 = Math.pow(2, -28);

// Tests for Math.sin for |x| < pi/4
assertEquals(Infinity, 1/Math.sin(+0.0));
assertEquals(-Infinity, 1/Math.sin(-0.0));
// sin(x) = x for x < 2^-27
assertEquals(two_32, Math.sin(two_32));
assertEquals(-two_32, Math.sin(-two_32));
// sin(pi/8) = sqrt(sqrt(2)-1)/2^(3/4)
assertEquals(0.3826834323650898, Math.sin(Math.PI/8));
assertEquals(-0.3826834323650898, -Math.sin(Math.PI/8));

// Tests for Math.cos for |x| < pi/4
// cos(x) = 1 for |x| < 2^-27
assertEquals(1, Math.cos(two_32));
assertEquals(1, Math.cos(-two_32));
// Test KERNELCOS for |x| < 0.3.
// cos(pi/20) = sqrt(sqrt(2)*sqrt(sqrt(5)+5)+4)/2^(3/2)
assertEquals(0.9876883405951378, Math.cos(Math.PI/20));
// Test KERNELCOS for x ~= 0.78125
assertEquals(0.7100335477927638, Math.cos(0.7812504768371582));
assertEquals(0.7100338835660797, Math.cos(0.78125));
// Test KERNELCOS for |x| > 0.3.
// cos(pi/8) = sqrt(sqrt(2)+1)/2^(3/4)
assertEquals(0.9238795325112867, Math.cos(Math.PI/8));
// Test KERNELTAN for |x| < 0.67434.
assertEquals(0.9238795325112867, Math.cos(-Math.PI/8));

// Tests for Math.tan for |x| < pi/4
assertEquals(Infinity, 1/Math.tan(0.0));
assertEquals(-Infinity, 1/Math.tan(-0.0));
// tan(x) = x for |x| < 2^-28
assertEquals(two_32, Math.tan(two_32));
assertEquals(-two_32, Math.tan(-two_32));
// Test KERNELTAN for |x| > 0.67434.
assertEquals(0.8211418015898941, Math.tan(11/16));
assertEquals(-0.8211418015898941, Math.tan(-11/16));
assertEquals(0.41421356237309503, Math.tan(Math.PI / 8));
230 231
// crbug/427468
assertEquals(0.7993357819992383, Math.tan(0.6743358));
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282

// Tests for Math.sin.
assertEquals(0.479425538604203, Math.sin(0.5));
assertEquals(-0.479425538604203, Math.sin(-0.5));
assertEquals(1, Math.sin(Math.PI/2));
assertEquals(-1, Math.sin(-Math.PI/2));
// Test that Math.sin(Math.PI) != 0 since Math.PI is not exact.
assertEquals(1.2246467991473532e-16, Math.sin(Math.PI));
assertEquals(-7.047032979958965e-14, Math.sin(2200*Math.PI));
// Test Math.sin for various phases.
assertEquals(-0.7071067811865477, Math.sin(7/4 * Math.PI));
assertEquals(0.7071067811865474, Math.sin(9/4 * Math.PI));
assertEquals(0.7071067811865483, Math.sin(11/4 * Math.PI));
assertEquals(-0.7071067811865479, Math.sin(13/4 * Math.PI));
assertEquals(-3.2103381051568376e-11, Math.sin(1048576/4 * Math.PI));

// Tests for Math.cos.
assertEquals(1, Math.cos(two_28));
// Cover different code paths in KERNELCOS.
assertEquals(0.9689124217106447, Math.cos(0.25));
assertEquals(0.8775825618903728, Math.cos(0.5));
assertEquals(0.7073882691671998, Math.cos(0.785));
// Test that Math.cos(Math.PI/2) != 0 since Math.PI is not exact.
assertEquals(6.123233995736766e-17, Math.cos(Math.PI/2));
// Test Math.cos for various phases.
assertEquals(0.7071067811865474, Math.cos(7/4 * Math.PI));
assertEquals(0.7071067811865477, Math.cos(9/4 * Math.PI));
assertEquals(-0.7071067811865467, Math.cos(11/4 * Math.PI));
assertEquals(-0.7071067811865471, Math.cos(13/4 * Math.PI));
assertEquals(0.9367521275331447, Math.cos(1000000));
assertEquals(-3.435757038074824e-12, Math.cos(1048575/2 * Math.PI));

// Tests for Math.tan.
assertEquals(two_28, Math.tan(two_28));
// Test that  Math.tan(Math.PI/2) != Infinity since Math.PI is not exact.
assertEquals(1.633123935319537e16, Math.tan(Math.PI/2));
// Cover different code paths in KERNELTAN (tangent and cotangent)
assertEquals(0.5463024898437905, Math.tan(0.5));
assertEquals(2.0000000000000027, Math.tan(1.107148717794091));
assertEquals(-1.0000000000000004, Math.tan(7/4*Math.PI));
assertEquals(0.9999999999999994, Math.tan(9/4*Math.PI));
assertEquals(-6.420676210313675e-11, Math.tan(1048576/2*Math.PI));
assertEquals(2.910566692924059e11, Math.tan(1048575/2*Math.PI));

// Test Hayne-Panek reduction.
assertEquals(0.377820109360752e0, Math.sin(Math.pow(2, 120)));
assertEquals(-0.9258790228548379e0, Math.cos(Math.pow(2, 120)));
assertEquals(-0.40806638884180424e0, Math.tan(Math.pow(2, 120)));
assertEquals(-0.377820109360752e0, Math.sin(-Math.pow(2, 120)));
assertEquals(-0.9258790228548379e0, Math.cos(-Math.pow(2, 120)));
assertEquals(0.40806638884180424e0, Math.tan(-Math.pow(2, 120)));