Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(205)

Side by Side Diff: tests/lib/math/double_pow_test.dart

Issue 22943005: Add documentation to dart:math pow function. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address comments. Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « tests/lib/lib.status ('k') | tests/lib/math/math_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 void checkVeryClose(double a, double b) {
10 // We find a ulp (unit in the last place) by shifting the original number
11 // to the right. This only works if we are not too close to infinity or if
12 // we work with denormals.
13 // We special case or 0.0, but not for infinity.
14 if (a == 0.0) {
15 final minimalDouble = 4.9406564584124654e-324;
16 Expect.equals(true, b.abs() <= minimalDouble);
17 return;
18 }
19 if (b == 0.0) {
20 // No need to look if they are close. Otherwise the check for 'a' above
21 // whould have triggered.
22 Expect.equals(a, b);
23 }
24 final double shiftRightBy52 = 2.220446049250313080847263336181640625e-16;
25 final double shiftedA = (a * shiftRightBy52).abs();
26 // Compared to 'a', 'shiftedA' is now ~1-2 ulp.
27
28 final double limitLow = a - shiftedA;
29 final double limitHigh = a + shiftedA;
30 Expect.equals(false, a == limitLow);
31 Expect.equals(false, a == limitHigh);
32 Expect.equals(true, limitLow <= b);
33 Expect.equals(true, b <= limitHigh);
34 }
35
36 const NaN = double.NAN;
37 const Infinity = double.INFINITY;
38
39 var samples = [
40 NaN,
41 -Infinity,
42 -3.0, // Odd integer
43 -2.0, // Even integer
44 -1.5, // Non-integer, magnitude > 1
45 -1.0, // Unit
46 -0.5, // Non-integer, magnitude < 1.
47 -0.0,
48 0.5, // Non-integer, magnitude < 1.
49 1.0, // Unit
50 1.5, // Non-integer, magnitude > 1
51 2.0, // Even integer
52 3.0, // Odd integer
53 Infinity
54 ];
55
56 main() {
57 // Tests of pow(x, y):
58 for (var d in samples) {
59 // if `y` is zero (0.0 or -0.0), the result is always 1.0.
60 Expect.identical(1.0, pow(d, 0.0), "$d");
61 Expect.identical(1.0, pow(d, -0.0), "$d");
62 }
63 for (var d in samples) {
64 // if `x` is 1.0, the result is always 1.0.
65 Expect.identical(1.0, pow(1.0, d), "$d");
66 }
67 for (var d in samples) {
68 // otherwise, if either `x` or `y` is NaN then the result is NaN.
69 if (d != 0.0) Expect.identical(NaN, pow(NaN, d), "$d");
70 if (d != 1.0) Expect.identical(NaN, pow(d, NaN), "$d");
71 }
72
73 for (var d in samples) {
74 // if `x` is a finite and strictly negative and `y` is a finite non-integer,
75 // the result is NaN.
76 if (d < 0 && !d.isInfinite) {
77 Expect.identical(NaN, pow(d, 0.5), "$d");
78 Expect.identical(NaN, pow(d, -0.5), "$d");
79 Expect.identical(NaN, pow(d, 1.5), "$d");
80 Expect.identical(NaN, pow(d, -1.5), "$d");
81 }
82 }
83
84 for (var d in samples) {
85 if (d < 0) {
86 // if `x` is Infinity and `y` is strictly negative, the result is 0.0.
87 Expect.identical(0.0, pow(Infinity, d), "$d");
88 }
89 if (d > 0) {
90 // if `x` is Infinity and `y` is strictly positive, the result is Infinity .
91 Expect.identical(Infinity, pow(Infinity, d), "$d");
92 }
93 }
94
95 for (var d in samples) {
96 if (d < 0) {
97 // if `x` is 0.0 and `y` is strictly negative, the result is Infinity.
98 Expect.identical(Infinity, pow(0.0, d), "$d");
99 }
100 if (d > 0) {
101 // if `x` is 0.0 and `y` is strictly positive, the result is 0.0.
102 Expect.identical(0.0, pow(0.0, d), "$d");
103 }
104 }
105
106 for (var d in samples) {
107 if (!d.isInfinite && !d.isNaN) {
108 var dint = d.toInt();
109 if (d == dint && dint.isOdd) {
110 // if `x` is -Infinity or -0.0 and `y` is an odd integer, then the
111 // result is`-pow(-x ,y)`.
112 Expect.identical(-pow(Infinity, d), pow(-Infinity, d));
113 Expect.identical(-pow(0.0, d), pow(-0.0, d));
114 continue;
115 }
116 }
117 // if `x` is -Infinity or -0.0 and `y` is not an odd integer, then the
118 // result is the same as `pow(-x , y)`.
119 Expect.identical(pow(Infinity, d), pow(-Infinity, d));
120 Expect.identical(pow(0.0, d), pow(-0.0, d));
121 }
122
123 for (var d in samples) {
124
125 if (d.abs() < 1) {
126 // if `y` is Infinity and the absolute value of `x` is less than 1, the
127 // result is 0.0.
128 Expect.identical(0.0, pow(d, Infinity));
129 } else if (d.abs() > 1) {
130 // if `y` is Infinity and the absolute value of `x` is greater than 1,
131 // the result is Infinity.
132 Expect.identical(Infinity, pow(d, Infinity));
133 } else if (d == -1) {
134 // if `y` is Infinity and `x` is -1, the result is 1.0.
135 Expect.identical(1.0, pow(d, Infinity));
136 }
137 // if `y` is -Infinity, the result is `1/pow(x, Infinity)`.
138 Expect.identical(1/pow(d, Infinity), pow(d, -Infinity));
139 }
140
141 // Some non-exceptional values.
142 checkVeryClose(16.0, pow(4.0, 2.0));
143 checkVeryClose(SQRT2, pow(2.0, 0.5));
144 checkVeryClose(SQRT1_2, pow(0.5, 0.5));
145 // Denormal result.
146 Expect.identical(5e-324, pow(2.0, -1074.0));
147 // Overflow.
148 Expect.identical(Infinity, pow(10.0, 309.0));
149 // Underflow.
150 Expect.identical(0.0, pow(10.0, -325.0));
151
152 // Conversion to double.
153
154 // The second argument is an odd integer as int, but not when converted
155 // to double.
156 Expect.identical(Infinity, pow(-0.0, -9223372036854775809));
157 }
158
OLDNEW
« no previous file with comments | « tests/lib/lib.status ('k') | tests/lib/math/math_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698