| 1 | /** |
| 2 | * Tests for consistent and correct math results |
| 3 | */ |
| 4 | |
| 5 | // +0 is different than -0, but standard equality won't test that |
| 6 | function isNegativeZero(z) { return z === 0 && 1/z === -Infinity; } |
| 7 | function isPositiveZero(z) { return z === 0 && 1/z === Infinity; } |
| 8 | |
| 9 | |
| 10 | // rounding |
| 11 | TS_ASSERT_EQUALS(0.1+0.2, 0.30000000000000004); |
| 12 | TS_ASSERT_EQUALS(0.1+0.7+0.3, 1.0999999999999999); |
| 13 | |
| 14 | // cos |
| 15 | TS_ASSERT_EQUALS(Math.cos(Math.PI/2), 0); |
| 16 | TS_ASSERT_UNEVAL_EQUALS(Math.cos(NaN), NaN); |
| 17 | TS_ASSERT_EQUALS(Math.cos(0), 1); |
| 18 | TS_ASSERT_EQUALS(Math.cos(-0), 1); |
| 19 | TS_ASSERT_UNEVAL_EQUALS(Math.cos(Infinity), NaN); |
| 20 | TS_ASSERT_UNEVAL_EQUALS(Math.cos(-Infinity), NaN); |
| 21 | |
| 22 | // sin |
| 23 | TS_ASSERT_EQUALS(Math.sin(Math.PI), 0); |
| 24 | TS_ASSERT_UNEVAL_EQUALS(Math.sin(NaN), NaN); |
| 25 | TS_ASSERT(isPositiveZero(Math.sin(0))); |
| 26 | // TS_ASSERT(isNegativeZero(Math.sin(-0))); TODO: doesn't match spec |
| 27 | TS_ASSERT_UNEVAL_EQUALS(Math.sin(Infinity), NaN); |
| 28 | TS_ASSERT_UNEVAL_EQUALS(Math.sin(-Infinity), NaN); |
| 29 | TS_ASSERT_EQUALS(Math.sin(1e-15), 7.771561172376096e-16); |
| 30 | |
| 31 | // atan |
| 32 | TS_ASSERT_UNEVAL_EQUALS(Math.atan(NaN), NaN); |
| 33 | TS_ASSERT(isPositiveZero(Math.atan(0))); |
| 34 | //TS_ASSERT(isNegativeZero(Math.atan(-0))); TODO: doesn't match spec |
| 35 | TS_ASSERT_EQUALS(Math.atan(Infinity), Math.PI/2); |
| 36 | TS_ASSERT_EQUALS(Math.atan(-Infinity), -Math.PI/2); |
| 37 | TS_ASSERT_EQUALS(Math.atan(1e-310), 1.00000000003903e-310); |
| 38 | TS_ASSERT_EQUALS(Math.atan(100), 1.5607966601078411); |
| 39 | |
| 40 | // atan2 |
| 41 | TS_ASSERT_UNEVAL_EQUALS(Math.atan2(NaN, 1), NaN); |
| 42 | TS_ASSERT_UNEVAL_EQUALS(Math.atan2(1, NaN), NaN); |
| 43 | TS_ASSERT_EQUALS(Math.atan2(1, 0), Math.PI/2); |
| 44 | //TS_ASSERT_EQUALS(Math.atan2(1, -0), -Math.PI/2); TODO: doesn't match spec |
| 45 | TS_ASSERT(isPositiveZero(Math.atan2(0, 1))); |
| 46 | TS_ASSERT(isPositiveZero(Math.atan2(0, 0))); |
| 47 | TS_ASSERT_EQUALS(Math.atan2(0, -0), Math.PI); |
| 48 | TS_ASSERT_EQUALS(Math.atan2(0, -1), Math.PI); |
| 49 | TS_ASSERT(isNegativeZero(Math.atan2(-0, 1))); |
| 50 | TS_ASSERT(isNegativeZero(Math.atan2(-0, 0))); |
| 51 | TS_ASSERT_EQUALS(Math.atan2(-0, -0), -Math.PI); |
| 52 | TS_ASSERT_EQUALS(Math.atan2(-0, -1), -Math.PI); |
| 53 | TS_ASSERT_EQUALS(Math.atan2(-1, 0), -Math.PI/2); |
| 54 | TS_ASSERT_EQUALS(Math.atan2(-1, -0), -Math.PI/2); |
| 55 | TS_ASSERT(isPositiveZero(Math.atan2(1.7e308, Infinity))); |
| 56 | TS_ASSERT_EQUALS(Math.atan2(1.7e308, -Infinity), Math.PI); |
| 57 | TS_ASSERT(isNegativeZero(Math.atan2(-1.7e308, Infinity))); |
| 58 | TS_ASSERT_EQUALS(Math.atan2(-1.7e308, -Infinity), -Math.PI); |
| 59 | TS_ASSERT_EQUALS(Math.atan2(Infinity, -1.7e308), Math.PI/2); |
| 60 | TS_ASSERT_EQUALS(Math.atan2(-Infinity, 1.7e308), -Math.PI/2); |
| 61 | TS_ASSERT_EQUALS(Math.atan2(Infinity, Infinity), Math.PI/4); |
| 62 | TS_ASSERT_EQUALS(Math.atan2(Infinity, -Infinity), 3*Math.PI/4); |
| 63 | TS_ASSERT_EQUALS(Math.atan2(-Infinity, Infinity), -Math.PI/4); |
| 64 | TS_ASSERT_EQUALS(Math.atan2(-Infinity, -Infinity), -3*Math.PI/4); |
| 65 | TS_ASSERT_EQUALS(Math.atan2(1e-310, 2), 5.0000000001954e-311); |
| 66 | |
| 67 | // exp |
| 68 | TS_ASSERT_UNEVAL_EQUALS(Math.exp(NaN), NaN); |
| 69 | TS_ASSERT_EQUALS(Math.exp(0), 1); |
| 70 | TS_ASSERT_EQUALS(Math.exp(-0), 1); |
| 71 | TS_ASSERT_EQUALS(Math.exp(Infinity), Infinity); |
| 72 | TS_ASSERT(isPositiveZero(Math.exp(-Infinity))); |
| 73 | TS_ASSERT_EQUALS(Math.exp(10), 22026.465794806707); |
| 74 | |
| 75 | // log |
| 76 | TS_ASSERT_UNEVAL_EQUALS(Math.log("NaN"), NaN); |
| 77 | TS_ASSERT_UNEVAL_EQUALS(Math.log(-1), NaN); |
| 78 | TS_ASSERT_EQUALS(Math.log(0), -Infinity); |
| 79 | TS_ASSERT_EQUALS(Math.log(-0), -Infinity); |
| 80 | TS_ASSERT(isPositiveZero(Math.log(1))); |
| 81 | TS_ASSERT_EQUALS(Math.log(Infinity), Infinity); |
| 82 | TS_ASSERT_EQUALS(Math.log(Math.E), 0.9999999999999991); |
| 83 | TS_ASSERT_EQUALS(Math.log(Math.E*Math.E*Math.E), 2.999999999999999); |
| 84 | |
| 85 | // pow |
| 86 | TS_ASSERT_EQUALS(Math.pow(NaN, 0), 1); |
| 87 | TS_ASSERT_EQUALS(Math.pow(NaN, -0), 1); |
| 88 | TS_ASSERT_UNEVAL_EQUALS(Math.pow(NaN, 100), NaN); |
| 89 | TS_ASSERT_EQUALS(Math.pow(1.7e308, Infinity), Infinity); |
| 90 | TS_ASSERT_EQUALS(Math.pow(-1.7e308, Infinity), Infinity); |
| 91 | TS_ASSERT(isPositiveZero(Math.pow(1.7e308, -Infinity))); |
| 92 | TS_ASSERT(isPositiveZero(Math.pow(-1.7e308, -Infinity))); |
| 93 | TS_ASSERT_UNEVAL_EQUALS(Math.pow(1, Infinity), NaN); |
| 94 | TS_ASSERT_UNEVAL_EQUALS(Math.pow(-1, Infinity), NaN); |
| 95 | TS_ASSERT_UNEVAL_EQUALS(Math.pow(1, -Infinity), NaN); |
| 96 | TS_ASSERT_UNEVAL_EQUALS(Math.pow(-1, -Infinity), NaN); |
| 97 | TS_ASSERT(isPositiveZero(Math.pow(1e-310, Infinity))); |
| 98 | TS_ASSERT(isPositiveZero(Math.pow(-1e-310, Infinity))); |
| 99 | TS_ASSERT_EQUALS(Math.pow(1e-310, -Infinity), Infinity); |
| 100 | TS_ASSERT_EQUALS(Math.pow(-1e-310, -Infinity), Infinity); |
| 101 | TS_ASSERT_EQUALS(Math.pow(Infinity, 1e-310), Infinity); |
| 102 | TS_ASSERT(isPositiveZero(Math.pow(Infinity, -1e-310))); |
| 103 | TS_ASSERT_EQUALS(Math.pow(-Infinity, 101), -Infinity); |
| 104 | TS_ASSERT_EQUALS(Math.pow(-Infinity, 1.7e308), Infinity); |
| 105 | TS_ASSERT(isNegativeZero(Math.pow(-Infinity, -101))); |
| 106 | TS_ASSERT(isPositiveZero(Math.pow(-Infinity, -1.7e308))); |
| 107 | TS_ASSERT(isPositiveZero(Math.pow(0, 1e-310))); |
| 108 | TS_ASSERT_EQUALS(Math.pow(0, -1e-310), Infinity); |
| 109 | TS_ASSERT(isNegativeZero(Math.pow(-0, 101))); |
| 110 | TS_ASSERT(isPositiveZero(Math.pow(-0, 1e-310))); |
| 111 | TS_ASSERT_EQUALS(Math.pow(-0, -101), -Infinity); |
| 112 | TS_ASSERT_EQUALS(Math.pow(-0, -1e-310), Infinity); |
| 113 | TS_ASSERT_UNEVAL_EQUALS(Math.pow(-1.7e308, 1e-310), NaN); |
| 114 | TS_ASSERT_EQUALS(Math.pow(Math.PI, -100), 1.9275814160560185e-50); |
| 115 | |
| 116 | // sqrt |
| 117 | TS_ASSERT_UNEVAL_EQUALS(Math.sqrt(NaN), NaN); |
| 118 | TS_ASSERT_UNEVAL_EQUALS(Math.sqrt(-1e-323), NaN); |
| 119 | TS_ASSERT(isPositiveZero(Math.sqrt(0))); |
| 120 | TS_ASSERT(isNegativeZero(Math.sqrt(-0))); |
| 121 | TS_ASSERT_EQUALS(Math.sqrt(Infinity), Infinity); |
| 122 | TS_ASSERT_EQUALS(Math.sqrt(1e-323), 3.1434555694052576e-162); |
| 123 | |
| 124 | |