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

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: Address comments. 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 | lib/math/base.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 /**
63 * Returns the minimum of two numbers. If either argument is NaN returns NaN.
64 * The minimum of [:-0.0:] and [:0.0:] is [:-0.0:]. If both arguments are
65 * equal (int and doubles with the same mathematical value are equal) then
66 * it is unspecified which of the two arguments is returned.
67 */
62 static num min(num a, num b) { 68 static num min(num a, num b) {
63 int c = a.compareTo(b); 69 if (a is num) {
64 if (c == 0) return a; 70 // TODO(floitsch): merge this if into the previous one, once dart2js
65 if (c < 0) { 71 // correctly propagates types for logical ands.
66 if ((b is double) && b.isNaN()) return b; 72 if (b is num) {
67 return a; 73 if (a > b) return b;
74 if (a < b) return a;
75 if (b is double) {
76 // Special case for NaN and -0.0. If one argument is NaN return NaN.
77 // [min] must also distinguish between -0.0 and 0.0.
78 if (a is double) {
79 if (a == 0.0) {
80 // a is either 0.0 or -0.0. b is either 0.0, -0.0 or NaN.
81 // The following returns -0.0 if either a or b is -0.0, and it
82 // returns NaN if b is NaN.
83 return (a + b) * a * b;
84 }
85 }
86 // Check for NaN and b == -0.0.
87 if (a == 0 && b.isNegative() || b.isNan()) return b;
88 return a;
89 }
90 return a;
91 }
92 throw new IllegalArgumentException(b);
68 } 93 }
69 if ((a is double) && a.isNaN()) return a; 94 throw new IllegalArgumentException(a);
70 return b;
71 }
72
73 static num max(num a, num b) {
74 // NaNs are handled correctly since the compareTo function always considers
75 // them to be bigger than any other operand.
76 return (a.compareTo(b) < 0) ? b : a;
77 } 95 }
78 96
79 /** 97 /**
98 * Returns the maximum of two numbers. If either argument is NaN returns NaN.
99 * The maximum of [:-0.0:] and [:0.0:] is [:0.0:]. If both arguments are
100 * equal (int and doubles with the same mathematical value are equal) then
101 * it is unspecified which of the two arguments is returned.
102 */
103 static num max(num a, num b) {
104 if (a is num) {
105 // TODO(floitsch): merge this if into the previous one, once dart2js
106 // correctly propagates types for logical ands.
107 if (b is num) {
108 if (a > b) return a;
109 if (a < b) return b;
110 if (b is double) {
111 // Special case for NaN and -0.0. If one argument is NaN return NaN.
112 // [max] must also distinguish between -0.0 and 0.0.
113 if (a is double) {
114 if (a == 0.0) {
115 // a is either 0.0 or -0.0. b is either 0.0, -0.0, or NaN.
116 // The following returns 0.0 if either a or b is 0.0, and it
117 // returns NaN if b is NaN.
118 return a + b;
119 }
120 }
121 // Check for NaN.
122 if (b.isNaN()) return b;
123 return a;
124 }
125 // max(-0.0, 0) must return 0.
126 if (b == 0 && a.isNegative()) return b;
127 return a;
128 }
129 throw new IllegalArgumentException(b);
130 }
131 throw new IllegalArgumentException(a);
132 }
133
134 /**
80 * Returns the arc tangent of [a]/[b] with sign according to quadrant. 135 * Returns the arc tangent of [a]/[b] with sign according to quadrant.
81 */ 136 */
82 static double atan2(num a, num b) => MathNatives.atan2(a, b); 137 static double atan2(num a, num b) => MathNatives.atan2(a, b);
83 138
84 /** 139 /**
85 * If the [exponent] is an integer the result is of the same type as [x]. 140 * If the [exponent] is an integer the result is of the same type as [x].
86 * Otherwise it is a [double]. 141 * Otherwise it is a [double].
87 */ 142 */
88 static num pow(num x, num exponent) => MathNatives.pow(x, exponent); 143 static num pow(num x, num exponent) => MathNatives.pow(x, exponent);
89 144
90 /** 145 /**
91 * Returns a random double greater than or equal to 0.0 and less 146 * Returns a random double greater than or equal to 0.0 and less
92 * than 1.0. 147 * than 1.0.
93 */ 148 */
94 static double random() => MathNatives.random(); 149 static double random() => MathNatives.random();
95 150
96 static double sin(num x) => MathNatives.sin(x); 151 static double sin(num x) => MathNatives.sin(x);
97 static double cos(num x) => MathNatives.cos(x); 152 static double cos(num x) => MathNatives.cos(x);
98 static double tan(num x) => MathNatives.tan(x); 153 static double tan(num x) => MathNatives.tan(x);
99 static double acos(num x) => MathNatives.acos(x); 154 static double acos(num x) => MathNatives.acos(x);
100 static double asin(num x) => MathNatives.asin(x); 155 static double asin(num x) => MathNatives.asin(x);
101 static double atan(num x) => MathNatives.atan(x); 156 static double atan(num x) => MathNatives.atan(x);
102 static double sqrt(num x) => MathNatives.sqrt(x); 157 static double sqrt(num x) => MathNatives.sqrt(x);
103 static double exp(num x) => MathNatives.exp(x); 158 static double exp(num x) => MathNatives.exp(x);
104 static double log(num x) => MathNatives.log(x); 159 static double log(num x) => MathNatives.log(x);
105 } 160 }
OLDNEW
« no previous file with comments | « no previous file | lib/math/base.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698