Index: corelib/unified/math/base.dart |
diff --git a/corelib/unified/math/base.dart b/corelib/unified/math/base.dart |
index 75696e842aafc879eb264a9c3482842a095bd88a..1c358c87378c71b36e4f08eee092e850cf30900b 100644 |
--- a/corelib/unified/math/base.dart |
+++ b/corelib/unified/math/base.dart |
@@ -58,21 +58,76 @@ external int parseInt(String str); |
*/ |
external double parseDouble(String 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); |
} |
/** |