OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016, 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 // VMOptions=--enable_asserts |
| 5 |
| 6 import "package:expect/expect.dart"; |
| 7 |
| 8 bool testAssociativity(Function f) { |
| 9 // Example from https://en.wikipedia.org/wiki/Floating_point |
| 10 // Test that (a + b) + c == a + (b + c). |
| 11 double a = f(1234.567); // Chop literals. |
| 12 double b = f(45.67834); |
| 13 double c = f(0.0004); |
| 14 double x = (a + b) + c; // Chop result of multiplication or division only. |
| 15 double y = a + (b + c); |
| 16 print("x: $x"); |
| 17 print("y: $y"); |
| 18 return x == y; |
| 19 } |
| 20 |
| 21 bool testDistributivity(Function f) { |
| 22 // Example from https://en.wikipedia.org/wiki/Floating_point |
| 23 // Test that (a + b)*c == a*c + b*c. |
| 24 double a = f(1234.567); // Chop literals. |
| 25 double b = f(1.234567); |
| 26 double c = f(3.333333); |
| 27 double x = f((a + b)*c); // Chop result of multiplication. |
| 28 double y = f(a*c) + f(b*c); |
| 29 print("x: $x"); |
| 30 print("y: $y"); |
| 31 return x == y; |
| 32 } |
| 33 |
| 34 // Simulate precision checking with assert. |
| 35 assertP(double d) { |
| 36 assert(d == d.p); |
| 37 } |
| 38 |
| 39 main() { |
| 40 // The getter p keeps only 20 (by default) bits after the decimal point. |
| 41 Expect.equals(0.0, 0.0.p); // 0.0 has no 1-bit after the decimal point. |
| 42 Expect.equals(1.5, 1.5.p); // 1.5 has a single 1-bit after the decimal point. |
| 43 Expect.notEquals(1.1, 1.1.p); // 1.1 has many 1-bits after the decimal point. |
| 44 Expect.notEquals(1/3, (1/3).p); // 0.33333333... ditto. |
| 45 |
| 46 Expect.equals(1.1 + 1/3, 1/3 + 1.1); // Test addition commutativity. |
| 47 Expect.equals(1.1.p + (1/3).p, (1/3).p + 1.1.p); |
| 48 Expect.equals(1.1 * 1/3, 1/3 * 1.1); // Test multiplication commutativity. |
| 49 Expect.equals(1.1.p * (1/3).p, (1/3).p * 1.1.p); |
| 50 |
| 51 print("Without chopping fractional bits:"); |
| 52 Expect.isFalse(testAssociativity((x) => x)); |
| 53 Expect.isFalse(testDistributivity((x) => x)); |
| 54 print("With chopping fractional bits:"); |
| 55 Expect.isTrue(testAssociativity((x) => x.p)); |
| 56 Expect.isTrue(testDistributivity((x) => x.p)); |
| 57 |
| 58 // Check that p works with NaN and Infinity. |
| 59 Expect.isTrue(double.NAN.p.isNaN); |
| 60 Expect.isTrue(double.INFINITY.p.isInfinite); |
| 61 Expect.isFalse(double.INFINITY.p.isNegative); |
| 62 Expect.isTrue(double.NEGATIVE_INFINITY.p.isInfinite); |
| 63 Expect.isTrue(double.NEGATIVE_INFINITY.p.isNegative); |
| 64 |
| 65 // Check use of assert to verify precision. |
| 66 assertP(1.5); |
| 67 assertP(1.1.p); |
| 68 Expect.throws(() => assertP(1.1), (e) => e is AssertionError); |
| 69 assertP(1.23456789.p); |
| 70 Expect.throws(() => assertP(1.23456789), (e) => e is AssertionError); |
| 71 } |
OLD | NEW |