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

Unified Diff: lib/math/base.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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « corelib/src/math.dart ('k') | tests/corelib/min_max_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/math/base.dart
diff --git a/lib/math/base.dart b/lib/math/base.dart
index f983e966963922870f4f50171b09e3e627487225..db2de6904410b201e1dfdff3c7fa345444d853c2 100644
--- a/lib/math/base.dart
+++ b/lib/math/base.dart
@@ -58,21 +58,76 @@ int parseInt(String str) => MathNatives.parseInt(str);
*/
double parseDouble(String str) => MathNatives.parseDouble(str);
+/**
+ * Returns the minimum of two numbers. If either argument is NaN returns NaN.
+ * The minimum of [:-0.0:] and [:0.0:] is [:-0.0:]. If both arguments are
+ * equal (int and doubles with the same mathematical value are equal) then
+ * it is unspecified which of the two arguments is returned.
+ */
num min(num a, num b) {
- int c = a.compareTo(b);
- if (c == 0) return a;
- if (c < 0) {
- if ((b is double) && b.isNaN()) return b;
- return a;
+ if (a is num) {
+ // TODO(floitsch): merge this if into the previous one, once dart2js
+ // correctly propagates types for logical ands.
+ if (b is num) {
+ if (a > b) return b;
+ if (a < b) return a;
+ if (b is double) {
+ // Special case for NaN and -0.0. If one argument is NaN return NaN.
+ // [min] must also distinguish between -0.0 and 0.0.
+ if (a is double) {
+ if (a == 0.0) {
+ // a is either 0.0 or -0.0. b is either 0.0, -0.0 or NaN.
+ // The following returns -0.0 if either a or b is -0.0, and it
+ // returns NaN if b is NaN.
+ return (a + b) * a * b;
+ }
+ }
+ // Check for NaN and b == -0.0.
+ if (a == 0 && b.isNegative() || b.isNan()) return b;
+ return a;
+ }
+ return a;
+ }
+ throw new IllegalArgumentException(b);
}
- if ((a is double) && a.isNaN()) return a;
- return b;
+ throw new IllegalArgumentException(a);
}
+/**
+ * Returns the maximum of two numbers. If either argument is NaN returns NaN.
+ * The maximum of [:-0.0:] and [:0.0:] is [:0.0:]. If both arguments are
+ * equal (int and doubles with the same mathematical value are equal) then
+ * it is unspecified which of the two arguments is returned.
+ */
num max(num a, num b) {
- // NaNs are handled correctly since the compareTo function always considers
- // them to be bigger than any other operand.
- return (a.compareTo(b) < 0) ? b : a;
+ if (a is num) {
+ // TODO(floitsch): merge this if into the previous one, once dart2js
+ // correctly propagates types for logical ands.
+ if (b is num) {
+ if (a > b) return a;
+ if (a < b) return b;
+ if (b is double) {
+ // Special case for NaN and -0.0. If one argument is NaN return NaN.
+ // [max] must also distinguish between -0.0 and 0.0.
+ if (a is double) {
+ if (a == 0.0) {
+ // a is either 0.0 or -0.0. b is either 0.0, -0.0, or NaN.
+ // The following returns 0.0 if either a or b is 0.0, and it
+ // returns NaN if b is NaN.
+ return a + b;
+ }
+ }
+ // Check for NaN.
+ if (b.isNaN()) return b;
+ return a;
+ }
+ // max(-0.0, 0) must return 0.
+ if (b == 0 && a.isNegative()) return b;
+ return a;
+ }
+ throw new IllegalArgumentException(b);
+ }
+ throw new IllegalArgumentException(a);
}
/**
« no previous file with comments | « corelib/src/math.dart ('k') | tests/corelib/min_max_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698