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

Side by Side Diff: sdk/lib/math/math.dart

Issue 24918003: Revert "Revert "Reinstate type checks in Math.min and Math.max"" (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 2 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 | « runtime/vm/intermediate_language.h ('k') | tests/lib/math/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) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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 /** 5 /**
6 * Mathematical constants and functions, plus a random number generator. 6 * Mathematical constants and functions, plus a random number generator.
7 */ 7 */
8 library dart.math; 8 library dart.math;
9 9
10 part "random.dart"; 10 part "random.dart";
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
54 /** 54 /**
55 * Returns the lesser of two numbers. 55 * Returns the lesser of two numbers.
56 * 56 *
57 * Returns NaN if either argument is NaN. 57 * Returns NaN if either argument is NaN.
58 * The lesser of [:-0.0:] and [:0.0:] is [:-0.0:]. 58 * The lesser of [:-0.0:] and [:0.0:] is [:-0.0:].
59 * If the arguments are otherwise equal (including int and doubles with the 59 * If the arguments are otherwise equal (including int and doubles with the
60 * same mathematical value) then it is unspecified which of the two arguments 60 * same mathematical value) then it is unspecified which of the two arguments
61 * is returned. 61 * is returned.
62 */ 62 */
63 num min(num a, num b) { 63 num min(num a, num b) {
64 // These partially redundant type checks improve code quality for dart2js.
65 // Most of the improvement is at call sites from the inferred non-null num
66 // return type.
67 if (a is! num) throw new ArgumentError(a);
68 if (b is! num) throw new ArgumentError(b);
69
64 if (a > b) return b; 70 if (a > b) return b;
65 if (a < b) return a; 71 if (a < b) return a;
66 if (b is double) { 72 if (b is double) {
67 // Special case for NaN and -0.0. If one argument is NaN return NaN. 73 // Special case for NaN and -0.0. If one argument is NaN return NaN.
68 // [min] must also distinguish between -0.0 and 0.0. 74 // [min] must also distinguish between -0.0 and 0.0.
69 if (a is double) { 75 if (a is double) {
70 if (a == 0.0) { 76 if (a == 0.0) {
71 // a is either 0.0 or -0.0. b is either 0.0, -0.0 or NaN. 77 // a is either 0.0 or -0.0. b is either 0.0, -0.0 or NaN.
72 // The following returns -0.0 if either a or b is -0.0, and it 78 // The following returns -0.0 if either a or b is -0.0, and it
73 // returns NaN if b is NaN. 79 // returns NaN if b is NaN.
74 return (a + b) * a * b; 80 return (a + b) * a * b;
75 } 81 }
76 } 82 }
77 // Check for NaN and b == -0.0. 83 // Check for NaN and b == -0.0.
78 if (a == 0 && b.isNegative || b.isNaN) return b; 84 if (a == 0 && b.isNegative || b.isNaN) return b;
79 return a; 85 return a;
80 } 86 }
81 return a; 87 return a;
82 } 88 }
83 89
84 /** 90 /**
85 * Returns the larger of two numbers. 91 * Returns the larger of two numbers.
86 * 92 *
87 * Returns NaN if either argument is NaN. 93 * Returns NaN if either argument is NaN.
88 * The larger of [:-0.0:] and [:0.0:] is [:0.0:]. If the arguments are 94 * The larger of [:-0.0:] and [:0.0:] is [:0.0:]. If the arguments are
89 * otherwise equal (including int and doubles with the same mathematical value) 95 * otherwise equal (including int and doubles with the same mathematical value)
90 * then it is unspecified which of the two arguments is returned. 96 * then it is unspecified which of the two arguments is returned.
91 */ 97 */
92 num max(num a, num b) { 98 num max(num a, num b) {
99 // These partially redundant type checks improve code quality for dart2js.
100 // Most of the improvement is at call sites from the inferred non-null num
101 // return type.
102 if (a is! num) throw new ArgumentError(a);
103 if (b is! num) throw new ArgumentError(b);
104
93 if (a > b) return a; 105 if (a > b) return a;
94 if (a < b) return b; 106 if (a < b) return b;
95 if (b is double) { 107 if (b is double) {
96 // Special case for NaN and -0.0. If one argument is NaN return NaN. 108 // Special case for NaN and -0.0. If one argument is NaN return NaN.
97 // [max] must also distinguish between -0.0 and 0.0. 109 // [max] must also distinguish between -0.0 and 0.0.
98 if (a is double) { 110 if (a is double) {
99 if (a == 0.0) { 111 if (a == 0.0) {
100 // a is either 0.0 or -0.0. b is either 0.0, -0.0, or NaN. 112 // a is either 0.0 or -0.0. b is either 0.0, -0.0, or NaN.
101 // The following returns 0.0 if either a or b is 0.0, and it 113 // The following returns 0.0 if either a or b is 0.0, and it
102 // returns NaN if b is NaN. 114 // returns NaN if b is NaN.
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
229 * Returns NaN if [x] is NaN. 241 * Returns NaN if [x] is NaN.
230 */ 242 */
231 external double exp(num x); 243 external double exp(num x);
232 244
233 /** 245 /**
234 * Converts [x] to a double and returns the natural logarithm of the value. 246 * Converts [x] to a double and returns the natural logarithm of the value.
235 * Returns negative infinity if [x] is equal to zero. 247 * Returns negative infinity if [x] is equal to zero.
236 * Returns NaN if [x] is NaN or less than zero. 248 * Returns NaN if [x] is NaN or less than zero.
237 */ 249 */
238 external double log(num x); 250 external double log(num x);
OLDNEW
« no previous file with comments | « runtime/vm/intermediate_language.h ('k') | tests/lib/math/min_max_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698