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

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: Refactored. 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 | tests/corelib/min_max_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
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 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
52 */ 52 */
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) {
Lasse Reichstein Nielsen 2012/07/10 07:35:00 Give it a doc-comment and say how it works (NaN if
floitsch 2012/07/10 17:32:32 Done.
63 int c = a.compareTo(b); 63 if (a is num) {
64 if (c == 0) return a; 64 // TODO(floitsch): merge this if into the previous one, once dart2js
65 if (c < 0) { 65 // correctly propagates types for logical ands.
66 if ((b is double) && b.isNaN()) return b; 66 if (b is num) {
67 return a; 67 if (a > b) return b;
68 if (a < b) return a;
69 if (b is double) {
70 // Special case for NaN and -0.0. If one argument is NaN return NaN.
71 // [min] must also distinguish between -0.0 and 0.0.
72 if (a is double) {
73 if (a == 0.0) {
74 // a is either 0.0 or -0.0. b is either 0.0, -0.0 or NaN.
75 // The following returns -0.0 if either a or b is -0.0, and it
76 // returns NaN if b is NaN.
77 return (a + b) * a * b;
Lasse Reichstein Nielsen 2012/07/10 07:35:00 This should work even if a or b are not doubles. A
floitsch 2012/07/10 17:32:32 I want to return a consistent order when both argu
78 }
79 }
80 // Check for NaN and b == -0.0.
Lasse Reichstein Nielsen 2012/07/10 07:35:00 If you didn't check for "a is double" above, then
floitsch 2012/07/10 17:32:32 Yes, but then we wouldn't return the left argument
81 if (a == 0 && b.isNegative() || b != b) return b;
82 return a;
83 }
84 return a;
85 }
86 throw new IllegalArgumentException(b);
68 } 87 }
69 if ((a is double) && a.isNaN()) return a; 88 throw new IllegalArgumentException(a);
70 return b;
71 } 89 }
72 90
73 static num max(num a, num b) { 91 static num max(num a, num b) {
74 // NaNs are handled correctly since the compareTo function always considers 92 if (a is num) {
75 // them to be bigger than any other operand. 93 // TODO(floitsch): merge this if into the previous one, once dart2js
76 return (a.compareTo(b) < 0) ? b : a; 94 // correctly propagates types for logical ands.
95 if (b is num) {
96 if (a > b) return a;
97 if (a < b) return b;
98 if (b is double) {
99 // Special case for NaN and -0.0. If one argument is NaN return NaN.
100 // [max] must also distinguish between -0.0 and 0.0.
101 if (a is double) {
102 if (a == 0.0) {
103 // a is either 0.0 or -0.0. b is either 0.0, -0.0, or NaN.
104 // The following returns 0.0 if either a or b is 0.0, and it
105 // returns NaN if b is NaN.
106 return a + b;
107 }
108 }
109 // Check for NaN.
Lasse Reichstein Nielsen 2012/07/10 07:35:00 Don't we have b.isNaN()? I'd prefer using that, at
floitsch 2012/07/10 17:32:32 Done.
110 if (b != b) return b;
111 return a;
112 }
113 // max(-0.0, 0) must return 0.
114 if (b == 0 && a.isNegative()) return b;
115 return a;
116 }
117 throw new IllegalArgumentException(b);
118 }
119 throw new IllegalArgumentException(a);
77 } 120 }
78 121
79 /** 122 /**
80 * Returns the arc tangent of [a]/[b] with sign according to quadrant. 123 * Returns the arc tangent of [a]/[b] with sign according to quadrant.
81 */ 124 */
82 static double atan2(num a, num b) => MathNatives.atan2(a, b); 125 static double atan2(num a, num b) => MathNatives.atan2(a, b);
83 126
84 /** 127 /**
85 * If the [exponent] is an integer the result is of the same type as [x]. 128 * If the [exponent] is an integer the result is of the same type as [x].
86 * Otherwise it is a [double]. 129 * Otherwise it is a [double].
87 */ 130 */
88 static num pow(num x, num exponent) => MathNatives.pow(x, exponent); 131 static num pow(num x, num exponent) => MathNatives.pow(x, exponent);
89 132
90 /** 133 /**
91 * Returns a random double greater than or equal to 0.0 and less 134 * Returns a random double greater than or equal to 0.0 and less
92 * than 1.0. 135 * than 1.0.
93 */ 136 */
94 static double random() => MathNatives.random(); 137 static double random() => MathNatives.random();
95 138
96 static double sin(num x) => MathNatives.sin(x); 139 static double sin(num x) => MathNatives.sin(x);
97 static double cos(num x) => MathNatives.cos(x); 140 static double cos(num x) => MathNatives.cos(x);
98 static double tan(num x) => MathNatives.tan(x); 141 static double tan(num x) => MathNatives.tan(x);
99 static double acos(num x) => MathNatives.acos(x); 142 static double acos(num x) => MathNatives.acos(x);
100 static double asin(num x) => MathNatives.asin(x); 143 static double asin(num x) => MathNatives.asin(x);
101 static double atan(num x) => MathNatives.atan(x); 144 static double atan(num x) => MathNatives.atan(x);
102 static double sqrt(num x) => MathNatives.sqrt(x); 145 static double sqrt(num x) => MathNatives.sqrt(x);
103 static double exp(num x) => MathNatives.exp(x); 146 static double exp(num x) => MathNatives.exp(x);
104 static double log(num x) => MathNatives.log(x); 147 static double log(num x) => MathNatives.log(x);
105 } 148 }
OLDNEW
« no previous file with comments | « no previous file | tests/corelib/min_max_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698