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 | |
5 import "package:expect/expect.dart"; | |
6 | |
7 bool TestAssociativity(Function f) { | |
8 // Example from https://en.wikipedia.org/wiki/Floating_point | |
9 // Test that (a + b) + c == a + (b + c). | |
10 double a = f(1234.567); // Chop literals. | |
11 double b = f(45.67834); | |
12 double c = f(0.0004); | |
13 double x = (a + b) + c; // Chop result of multiplication or division only. | |
14 double y = a + (b + c); | |
15 print("x: $x"); | |
16 print("y: $y"); | |
17 return x == y; | |
18 } | |
19 | |
20 bool TestDistributivity(Function f) { | |
21 // Example from https://en.wikipedia.org/wiki/Floating_point | |
22 // Test that (a + b)*c == a*c + b*c. | |
23 double a = f(1234.567); // Chop literals. | |
24 double b = f(1.234567); | |
25 double c = f(3.333333); | |
26 double x = f((a + b)*c); // Chop result of multiplication. | |
27 double y = f(a*c) + f(b*c); | |
28 print("x: $x"); | |
29 print("y: $y"); | |
30 return x == y; | |
31 } | |
32 | |
33 main() { | |
34 Expect.equals(0.0, 0.0.p); | |
35 Expect.equals(1.5, 1.5.p); | |
36 Expect.notEquals(1.1, 1.1.p); | |
37 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.
| |
38 Expect.equals((1.1 + 1/3).p, 1.1.p + (1/3).p); | |
39 | |
40 print("Without chopping fractional bits:"); | |
41 Expect.isFalse(TestAssociativity((x) => x)); | |
42 Expect.isFalse(TestDistributivity((x) => x)); | |
43 print("With chopping fractional bits:"); | |
44 Expect.isTrue(TestAssociativity((x) => x.p)); | |
45 Expect.isTrue(TestDistributivity((x) => x.p)); | |
46 | |
47 // Check that p works with NaN and Infinity. | |
48 Expect.isTrue(double.NAN.p.isNaN); | |
49 Expect.isTrue(double.INFINITY.p.isInfinite); | |
50 Expect.isFalse(double.INFINITY.p.isNegative); | |
51 Expect.isTrue(double.NEGATIVE_INFINITY.p.isInfinite); | |
52 Expect.isTrue(double.NEGATIVE_INFINITY.p.isNegative); | |
53 } | |
OLD | NEW |