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

Side by Side Diff: corelib/src/math.dart

Issue 10690086: Inline compareTo for numbers into min and max. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 5 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 | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 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 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. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 // Dart core library. 5 // Dart core library.
6 6
7 class Math { 7 class Math {
8 /** 8 /**
9 * Base of the natural logarithms. 9 * Base of the natural logarithms.
10 */ 10 */
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
53 static int parseInt(String str) => MathNatives.parseInt(str); 53 static int parseInt(String str) => MathNatives.parseInt(str);
54 54
55 /** 55 /**
56 * Parses a [String] representation of a [double], and returns 56 * Parses a [String] representation of a [double], and returns
57 * a [double]. Throws a [BadNumberFormatException] if [str] cannot 57 * a [double]. Throws a [BadNumberFormatException] if [str] cannot
58 * be parsed as a [double]. 58 * be parsed as a [double].
59 */ 59 */
60 static double parseDouble(String str) => MathNatives.parseDouble(str); 60 static double parseDouble(String str) => MathNatives.parseDouble(str);
61 61
62 static num min(num a, num b) { 62 static num min(num a, num b) {
63 int c = a.compareTo(b); 63 if (a is num) { // TODO(floitsch): move this into one if.
sra1 2012/07/04 19:28:23 Under what conditions? (description or issue #) Pr
floitsch 2012/07/05 17:48:49 Done.
64 if (c == 0) return a; 64 if (b is num) {
65 if (c < 0) { 65 // Special case for NaN and -0.0. If one argument is NaN return NaN.
66 if ((b is double) && b.isNaN()) return b; 66 // In the context of [min] we have furthermore -0.0 < 0.0.
sra1 2012/07/04 19:28:23 What does 'in the context of [min]' mean here, in
floitsch 2012/07/05 17:48:49 Done.
67 return a; 67 if (a > b) return b;
68 if (a == b) {
69 if (a is double && a == 0.0) {
Lasse Reichstein Nielsen 2012/07/05 09:34:41 This is a little tricky, since in dart2js 0.0 is b
floitsch 2012/07/05 17:48:49 removed with your code.
70 if (b is double) {
71 if (1.0 / a < 0.0) {
Lasse Reichstein Nielsen 2012/07/05 09:34:41 Consider checking as 1.0 / a == -double.Infinity
floitsch 2012/07/05 17:48:49 nice. done.
72 // a == -0.0.
73 return a;
74 }
75 // b == -0.0 or 0.0.
76 return b;
77 }
78 // a == -0.0 or 0.0, and b == 0.
79 return a;
80 }
81 return b;
82 }
83 if (a != a) return a;
84 return b;
85 }
68 } 86 }
69 if ((a is double) && a.isNaN()) return a; 87 return a.compareTo(b) <= 0 ? a : b;
Lasse Reichstein Nielsen 2012/07/05 09:34:41 Consider using (0 >= a.compareTo(b)) to avoid havi
floitsch 2012/07/05 17:48:49 Done.
70 return b;
71 } 88 }
72 89
73 static num max(num a, num b) { 90 static num max(num a, num b) {
74 // NaNs are handled correctly since the compareTo function always considers 91 if (a is num) { // TODO(floitsch): move this into one if.
75 // them to be bigger than any other operand. 92 if (b is num) {
76 return (a.compareTo(b) < 0) ? b : a; 93 // Special case for NaN and -0.0. If one argument is NaN return NaN.
94 // In the context of [min] we have furthermore -0.0 < 0.0.
sra1 2012/07/04 19:28:23 [max]
floitsch 2012/07/05 17:48:49 Done.
95 if (a < b) return a;
sra1 2012/07/04 19:28:23 max(1,2) ==> 1 ? make sure the test covers A x
Lasse Reichstein Nielsen 2012/07/05 09:34:41 Yes, swap either the return or the comparison, but
floitsch 2012/07/05 17:48:49 Done.
96 if (a == b) {
97 if (a is double && a == 0.0) {
Lasse Reichstein Nielsen 2012/07/05 09:34:41 if (a == 0) return a + b; // -0.0 if both are -0.
floitsch 2012/07/05 17:48:49 done.
98 if (b is double) {
99 if (1.0 / a < 0.0) {
100 // a == -0.0, and b == -0.0 or 0.0.
101 return b;
102 }
103 // a == 0.0.
104 return a;
105 }
106 // a == -0.0 or 0.0, and b == 0.
107 return b;
108 }
109 return a;
110 }
111 if (a != a) return a;
112 return b;
113 }
Lasse Reichstein Nielsen 2012/07/05 09:34:41 Here you know that 'a' is num and 'b' is not, so y
floitsch 2012/07/05 17:48:49 rewritten.
114 }
115 return a.compareTo(b) >= 0 ? a : b;
77 } 116 }
78 117
79 /** 118 /**
80 * Returns the arc tangent of [a]/[b] with sign according to quadrant. 119 * Returns the arc tangent of [a]/[b] with sign according to quadrant.
81 */ 120 */
82 static double atan2(num a, num b) => MathNatives.atan2(a, b); 121 static double atan2(num a, num b) => MathNatives.atan2(a, b);
83 122
84 /** 123 /**
85 * If the [exponent] is an integer the result is of the same type as [x]. 124 * If the [exponent] is an integer the result is of the same type as [x].
86 * Otherwise it is a [double]. 125 * Otherwise it is a [double].
87 */ 126 */
88 static num pow(num x, num exponent) => MathNatives.pow(x, exponent); 127 static num pow(num x, num exponent) => MathNatives.pow(x, exponent);
89 128
90 /** 129 /**
91 * Returns a random double greater than or equal to 0.0 and less 130 * Returns a random double greater than or equal to 0.0 and less
92 * than 1.0. 131 * than 1.0.
93 */ 132 */
94 static double random() => MathNatives.random(); 133 static double random() => MathNatives.random();
95 134
96 static double sin(num x) => MathNatives.sin(x); 135 static double sin(num x) => MathNatives.sin(x);
97 static double cos(num x) => MathNatives.cos(x); 136 static double cos(num x) => MathNatives.cos(x);
98 static double tan(num x) => MathNatives.tan(x); 137 static double tan(num x) => MathNatives.tan(x);
99 static double acos(num x) => MathNatives.acos(x); 138 static double acos(num x) => MathNatives.acos(x);
100 static double asin(num x) => MathNatives.asin(x); 139 static double asin(num x) => MathNatives.asin(x);
101 static double atan(num x) => MathNatives.atan(x); 140 static double atan(num x) => MathNatives.atan(x);
102 static double sqrt(num x) => MathNatives.sqrt(x); 141 static double sqrt(num x) => MathNatives.sqrt(x);
103 static double exp(num x) => MathNatives.exp(x); 142 static double exp(num x) => MathNatives.exp(x);
104 static double log(num x) => MathNatives.log(x); 143 static double log(num x) => MathNatives.log(x);
105 } 144 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698