Chromium Code Reviews| Index: corelib/src/math.dart |
| diff --git a/corelib/src/math.dart b/corelib/src/math.dart |
| index 1acd20df3c724f3c71902fdba37eeb5341055b1f..f602d82abec67e93008e477e3bfac574619608bf 100644 |
| --- a/corelib/src/math.dart |
| +++ b/corelib/src/math.dart |
| @@ -60,20 +60,63 @@ class Math { |
| static double parseDouble(String str) => MathNatives.parseDouble(str); |
| 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.
|
| - 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; |
|
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
|
| + } |
| + } |
| + // 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
|
| + if (a == 0 && b.isNegative() || b != b) return b; |
| + return a; |
| + } |
| + return a; |
| + } |
| + throw new IllegalArgumentException(b); |
| } |
| - if ((a is double) && a.isNaN()) return a; |
| - return b; |
| + throw new IllegalArgumentException(a); |
| } |
| static 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. |
|
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.
|
| + if (b != b) 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); |
| } |
| /** |