OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library math_test; |
| 6 import "package:expect/expect.dart"; |
| 7 import 'dart:math'; |
| 8 |
| 9 class MathTest { |
| 10 static void testConstants() { |
| 11 // Source for mathematical constants is Wolfram Alpha. |
| 12 Expect.equals(2.7182818284590452353602874713526624977572470936999595749669, |
| 13 E); |
| 14 Expect.equals(2.3025850929940456840179914546843642076011014886287729760333, |
| 15 LN10); |
| 16 Expect.equals(0.6931471805599453094172321214581765680755001343602552541206, |
| 17 LN2); |
| 18 Expect.equals(1.4426950408889634073599246810018921374266459541529859341354, |
| 19 LOG2E); |
| 20 Expect.equals(0.4342944819032518276511289189166050822943970058036665661144, |
| 21 LOG10E); |
| 22 Expect.equals(3.1415926535897932384626433832795028841971693993751058209749, |
| 23 PI); |
| 24 Expect.equals(0.7071067811865475244008443621048490392848359376884740365883, |
| 25 SQRT1_2); |
| 26 Expect.equals(1.4142135623730950488016887242096980785696718753769480731766, |
| 27 SQRT2); |
| 28 } |
| 29 |
| 30 static checkClose(double a, double b, EPSILON) { |
| 31 Expect.equals(true, a - EPSILON <= b); |
| 32 Expect.equals(true, b <= a + EPSILON); |
| 33 } |
| 34 |
| 35 static void testSin() { |
| 36 // Given the imprecision of PI we can't expect better results than this. |
| 37 final double EPSILON = 1e-15; |
| 38 checkClose(0.0, sin(0.0), EPSILON); |
| 39 checkClose(0.0, sin(PI), EPSILON); |
| 40 checkClose(0.0, sin(2.0 * PI), EPSILON); |
| 41 checkClose(1.0, sin(PI / 2.0), EPSILON); |
| 42 checkClose(-1.0, sin(PI * (3.0 / 2.0)), EPSILON); |
| 43 } |
| 44 |
| 45 static void testCos() { |
| 46 // Given the imprecision of PI we can't expect better results than this. |
| 47 final double EPSILON = 1e-15; |
| 48 checkClose(1.0, cos(0.0), EPSILON); |
| 49 checkClose(-1.0, cos(PI), EPSILON); |
| 50 checkClose(1.0, cos(2.0 * PI), EPSILON); |
| 51 checkClose(0.0, cos(PI / 2.0), EPSILON); |
| 52 checkClose(0.0, cos(PI * (3.0 / 2.0)), EPSILON); |
| 53 } |
| 54 |
| 55 static void testTan() { |
| 56 // Given the imprecision of PI we can't expect better results than this. |
| 57 final double EPSILON = 1e-15; |
| 58 checkClose(0.0, tan(0.0), EPSILON); |
| 59 checkClose(0.0, tan(PI), EPSILON); |
| 60 checkClose(0.0, tan(2.0 * PI), EPSILON); |
| 61 checkClose(1.0, tan(PI / 4.0), EPSILON); |
| 62 } |
| 63 |
| 64 static void testAsin() { |
| 65 // Given the imprecision of PI we can't expect better results than this. |
| 66 final double EPSILON = 1e-15; |
| 67 checkClose(0.0, asin(0.0), EPSILON); |
| 68 checkClose(PI / 2.0, asin(1.0), EPSILON); |
| 69 checkClose(-PI / 2.0, asin(-1.0), EPSILON); |
| 70 } |
| 71 |
| 72 |
| 73 static void testAcos() { |
| 74 // Given the imprecision of PI we can't expect better results than this. |
| 75 final double EPSILON = 1e-15; |
| 76 checkClose(0.0, acos(1.0), EPSILON); |
| 77 checkClose(PI, acos(-1.0), EPSILON); |
| 78 checkClose(PI / 2.0, acos(0.0), EPSILON); |
| 79 } |
| 80 |
| 81 static void testAtan() { |
| 82 // Given the imprecision of PI we can't expect better results than this. |
| 83 final double EPSILON = 1e-15; |
| 84 checkClose(0.0, atan(0.0), EPSILON); |
| 85 checkClose(PI / 4.0, atan(1.0), EPSILON); |
| 86 checkClose(-PI / 4.0, atan(-1.0), EPSILON); |
| 87 } |
| 88 |
| 89 static void testAtan2() { |
| 90 // Given the imprecision of PI we can't expect better results than this. |
| 91 final double EPSILON = 1e-15; |
| 92 checkClose(0.0, atan2(0.0, 5.0), EPSILON); |
| 93 checkClose(PI / 4.0, atan2(2.0, 2.0), EPSILON); |
| 94 checkClose(3 * PI / 4.0, atan2(0.5, -0.5), EPSILON); |
| 95 checkClose(-3 * PI / 4.0, atan2(-2.5, -2.5), EPSILON); |
| 96 } |
| 97 |
| 98 static checkVeryClose(double a, double b) { |
| 99 // We find a ulp (unit in the last place) by shifting the original number |
| 100 // to the right. This only works if we are not too close to infinity or if |
| 101 // we work with denormals. |
| 102 // We special case or 0.0, but not for infinity. |
| 103 if (a == 0.0) { |
| 104 final minimalDouble = 4.9406564584124654e-324; |
| 105 Expect.equals(true, b.abs() <= minimalDouble); |
| 106 return; |
| 107 } |
| 108 if (b == 0.0) { |
| 109 // No need to look if they are close. Otherwise the check for 'a' above |
| 110 // whould have triggered. |
| 111 Expect.equals(a, b); |
| 112 } |
| 113 final double shiftRightBy52 = 2.220446049250313080847263336181640625e-16; |
| 114 final double shiftedA = (a * shiftRightBy52).abs(); |
| 115 // Compared to 'a', 'shiftedA' is now ~1-2 ulp. |
| 116 |
| 117 final double limitLow = a - shiftedA; |
| 118 final double limitHigh = a + shiftedA; |
| 119 Expect.equals(false, a == limitLow); |
| 120 Expect.equals(false, a == limitHigh); |
| 121 Expect.equals(true, limitLow <= b); |
| 122 Expect.equals(true, b <= limitHigh); |
| 123 } |
| 124 |
| 125 static void testSqrt() { |
| 126 checkVeryClose(2.0, sqrt(4.0)); |
| 127 checkVeryClose(SQRT2, sqrt(2.0)); |
| 128 checkVeryClose(SQRT1_2, sqrt(0.5)); |
| 129 checkVeryClose(1e50, sqrt(1e100)); |
| 130 checkVeryClose(1.1111111061110855443054405046358901279277111935183977e56, |
| 131 sqrt(12345678901234e99)); |
| 132 } |
| 133 |
| 134 static void testExp() { |
| 135 checkVeryClose(E, exp(1.0)); |
| 136 final EPSILON = 1e-15; |
| 137 checkClose(10.0, exp(LN10), EPSILON); |
| 138 checkClose(2.0, exp(LN2), EPSILON); |
| 139 } |
| 140 |
| 141 static void testLog() { |
| 142 // Even though E is imprecise, it is good enough to get really close to 1. |
| 143 // We still provide an epsilon. |
| 144 checkClose(1.0, log(E), 1e-16); |
| 145 checkVeryClose(LN10, log(10.0)); |
| 146 checkVeryClose(LN2, log(2.0)); |
| 147 } |
| 148 |
| 149 static bool parseIntThrowsFormatException(str) { |
| 150 try { |
| 151 int.parse(str); |
| 152 return false; |
| 153 } on FormatException catch (e) { |
| 154 return true; |
| 155 } |
| 156 } |
| 157 |
| 158 static void testParseInt() { |
| 159 Expect.equals(499, int.parse("499")); |
| 160 Expect.equals(499, int.parse("+499")); |
| 161 Expect.equals(-499, int.parse("-499")); |
| 162 Expect.equals(499, int.parse(" 499 ")); |
| 163 Expect.equals(499, int.parse(" +499 ")); |
| 164 Expect.equals(-499, int.parse(" -499 ")); |
| 165 Expect.equals(0, int.parse("0")); |
| 166 Expect.equals(0, int.parse("+0")); |
| 167 Expect.equals(0, int.parse("-0")); |
| 168 Expect.equals(0, int.parse(" 0 ")); |
| 169 Expect.equals(0, int.parse(" +0 ")); |
| 170 Expect.equals(0, int.parse(" -0 ")); |
| 171 Expect.equals(0x1234567890, int.parse("0x1234567890")); |
| 172 Expect.equals(-0x1234567890, int.parse("-0x1234567890")); |
| 173 Expect.equals(0x1234567890, int.parse(" 0x1234567890 ")); |
| 174 Expect.equals(-0x1234567890, int.parse(" -0x1234567890 ")); |
| 175 Expect.equals(256, int.parse("0x100")); |
| 176 Expect.equals(-256, int.parse("-0x100")); |
| 177 Expect.equals(256, int.parse(" 0x100 ")); |
| 178 Expect.equals(-256, int.parse(" -0x100 ")); |
| 179 Expect.equals(0xabcdef, int.parse("0xabcdef")); |
| 180 Expect.equals(0xABCDEF, int.parse("0xABCDEF")); |
| 181 Expect.equals(0xabcdef, int.parse("0xabCDEf")); |
| 182 Expect.equals(-0xabcdef, int.parse("-0xabcdef")); |
| 183 Expect.equals(-0xABCDEF, int.parse("-0xABCDEF")); |
| 184 Expect.equals(0xabcdef, int.parse(" 0xabcdef ")); |
| 185 Expect.equals(0xABCDEF, int.parse(" 0xABCDEF ")); |
| 186 Expect.equals(-0xabcdef, int.parse(" -0xabcdef ")); |
| 187 Expect.equals(-0xABCDEF, int.parse(" -0xABCDEF ")); |
| 188 Expect.equals(0xabcdef, int.parse("0x00000abcdef")); |
| 189 Expect.equals(0xABCDEF, int.parse("0x00000ABCDEF")); |
| 190 Expect.equals(-0xabcdef, int.parse("-0x00000abcdef")); |
| 191 Expect.equals(-0xABCDEF, int.parse("-0x00000ABCDEF")); |
| 192 Expect.equals(0xabcdef, int.parse(" 0x00000abcdef ")); |
| 193 Expect.equals(0xABCDEF, int.parse(" 0x00000ABCDEF ")); |
| 194 Expect.equals(-0xabcdef, int.parse(" -0x00000abcdef ")); |
| 195 Expect.equals(-0xABCDEF, int.parse(" -0x00000ABCDEF ")); |
| 196 Expect.equals(10, int.parse("010")); |
| 197 Expect.equals(-10, int.parse("-010")); |
| 198 Expect.equals(10, int.parse(" 010 ")); |
| 199 Expect.equals(-10, int.parse(" -010 ")); |
| 200 Expect.equals(9, int.parse("09")); |
| 201 Expect.equals(9, int.parse(" 09 ")); |
| 202 Expect.equals(-9, int.parse("-09")); |
| 203 Expect.equals(0x1234567890, int.parse("+0x1234567890")); |
| 204 Expect.equals(0x1234567890,int.parse(" +0x1234567890 ")); |
| 205 Expect.equals(0x100, int.parse("+0x100")); |
| 206 Expect.equals(0x100, int.parse(" +0x100 ")); |
| 207 Expect.equals(true, parseIntThrowsFormatException("1b")); |
| 208 Expect.equals(true, parseIntThrowsFormatException(" 1b ")); |
| 209 Expect.equals(true, parseIntThrowsFormatException(" 1 b ")); |
| 210 Expect.equals(true, parseIntThrowsFormatException("1e2")); |
| 211 Expect.equals(true, parseIntThrowsFormatException(" 1e2 ")); |
| 212 Expect.equals(true, parseIntThrowsFormatException("00x12")); |
| 213 Expect.equals(true, parseIntThrowsFormatException(" 00x12 ")); |
| 214 Expect.equals(true, parseIntThrowsFormatException("-1b")); |
| 215 Expect.equals(true, parseIntThrowsFormatException(" -1b ")); |
| 216 Expect.equals(true, parseIntThrowsFormatException(" -1 b ")); |
| 217 Expect.equals(true, parseIntThrowsFormatException("-1e2")); |
| 218 Expect.equals(true, parseIntThrowsFormatException(" -1e2 ")); |
| 219 Expect.equals(true, parseIntThrowsFormatException("-00x12")); |
| 220 Expect.equals(true, parseIntThrowsFormatException(" -00x12 ")); |
| 221 Expect.equals(true, parseIntThrowsFormatException(" -00x12 ")); |
| 222 Expect.equals(true, parseIntThrowsFormatException("0x0x12")); |
| 223 Expect.equals(true, parseIntThrowsFormatException("0.1")); |
| 224 Expect.equals(true, parseIntThrowsFormatException("0x3.1")); |
| 225 Expect.equals(true, parseIntThrowsFormatException("5.")); |
| 226 Expect.equals(true, parseIntThrowsFormatException("+-5")); |
| 227 Expect.equals(true, parseIntThrowsFormatException("-+5")); |
| 228 Expect.equals(true, parseIntThrowsFormatException("--5")); |
| 229 Expect.equals(true, parseIntThrowsFormatException("++5")); |
| 230 Expect.equals(true, parseIntThrowsFormatException("+ 5")); |
| 231 Expect.equals(true, parseIntThrowsFormatException("- 5")); |
| 232 Expect.equals(true, parseIntThrowsFormatException("")); |
| 233 Expect.equals(true, parseIntThrowsFormatException(" ")); |
| 234 } |
| 235 |
| 236 static testMain() { |
| 237 testConstants(); |
| 238 testSin(); |
| 239 testCos(); |
| 240 testTan(); |
| 241 testAsin(); |
| 242 testAcos(); |
| 243 testAtan(); |
| 244 testAtan2(); |
| 245 testSqrt(); |
| 246 testLog(); |
| 247 testExp(); |
| 248 testParseInt(); |
| 249 } |
| 250 } |
| 251 |
| 252 main() { |
| 253 MathTest.testMain(); |
| 254 } |
OLD | NEW |