Index: tests/standalone/fixed_precision_double_test.dart |
diff --git a/tests/standalone/fixed_precision_double_test.dart b/tests/standalone/fixed_precision_double_test.dart |
new file mode 100644 |
index 0000000000000000000000000000000000000000..0903435fce51cf8d2e10b4edc6c427ceaa09f246 |
--- /dev/null |
+++ b/tests/standalone/fixed_precision_double_test.dart |
@@ -0,0 +1,53 @@ |
+// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
+// for details. All rights reserved. Use of this source code is governed by a |
+// BSD-style license that can be found in the LICENSE file. |
+ |
+import "package:expect/expect.dart"; |
+ |
+bool TestAssociativity(Function f) { |
+ // Example from https://en.wikipedia.org/wiki/Floating_point |
+ // Test that (a + b) + c == a + (b + c). |
+ double a = f(1234.567); // Chop literals. |
+ double b = f(45.67834); |
+ double c = f(0.0004); |
+ double x = (a + b) + c; // Chop result of multiplication or division only. |
+ double y = a + (b + c); |
+ print("x: $x"); |
+ print("y: $y"); |
+ return x == y; |
+} |
+ |
+bool TestDistributivity(Function f) { |
+ // Example from https://en.wikipedia.org/wiki/Floating_point |
+ // Test that (a + b)*c == a*c + b*c. |
+ double a = f(1234.567); // Chop literals. |
+ double b = f(1.234567); |
+ double c = f(3.333333); |
+ double x = f((a + b)*c); // Chop result of multiplication. |
+ double y = f(a*c) + f(b*c); |
+ print("x: $x"); |
+ print("y: $y"); |
+ return x == y; |
+} |
+ |
+main() { |
+ Expect.equals(0.0, 0.0.p); |
+ Expect.equals(1.5, 1.5.p); |
+ Expect.notEquals(1.1, 1.1.p); |
+ Expect.notEquals(1/3, (1/3).p); |
siva
2016/08/04 20:20:13
What is the significance of these tests , how does
regis
2016/08/04 22:16:12
I have added comments explaining this.
|
+ Expect.equals((1.1 + 1/3).p, 1.1.p + (1/3).p); |
+ |
+ print("Without chopping fractional bits:"); |
+ Expect.isFalse(TestAssociativity((x) => x)); |
+ Expect.isFalse(TestDistributivity((x) => x)); |
+ print("With chopping fractional bits:"); |
+ Expect.isTrue(TestAssociativity((x) => x.p)); |
+ Expect.isTrue(TestDistributivity((x) => x.p)); |
+ |
+ // Check that p works with NaN and Infinity. |
+ Expect.isTrue(double.NAN.p.isNaN); |
+ Expect.isTrue(double.INFINITY.p.isInfinite); |
+ Expect.isFalse(double.INFINITY.p.isNegative); |
+ Expect.isTrue(double.NEGATIVE_INFINITY.p.isInfinite); |
+ Expect.isTrue(double.NEGATIVE_INFINITY.p.isNegative); |
+} |