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

Side by Side Diff: runtime/lib/math_patch.dart

Issue 23073005: Make pow fail for non-number arguments. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 4 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 | no next file » | 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 import "dart:typed_data"; 5 import "dart:typed_data";
6 6
7 // A VM patch of the dart:math library. 7 // A VM patch of the dart:math library.
8 8
9 // If [x] is an [int] and [exponent] is a non-negative [int], the result is 9 // If [x] is an [int] and [exponent] is a non-negative [int], the result is
10 // an [int], otherwise the result is a [double]. 10 // an [int], otherwise the result is a [double].
11 patch num pow(num x, num exponent) { 11 patch num pow(num x, num exponent) {
12 if ((x is int) && (exponent is int) && (exponent >= 0)) { 12 if ((x is int) && (exponent is int) && (exponent >= 0)) {
13 return _intPow(x, exponent); 13 return _intPow(x, exponent);
14 } 14 }
15 // doublePow will call exponent.toDouble(). 15 return _doublePow(x.toDouble(), exponent.toDouble());
16 return _doublePow(x.toDouble(), exponent);
17 } 16 }
18 17
19 double _doublePow(double base, num exponent) { 18 double _doublePow(double base, double exponent) {
20 if (exponent == 0) { 19 if (exponent == 0.0) {
21 return 1.0; // ECMA-262 15.8.2.13 20 return 1.0; // ECMA-262 15.8.2.13
22 } 21 }
23 if (base == 1.0) return 1.0; 22 if (base == 1.0) return 1.0;
24 double doubleExponent = exponent.toDouble();
25 if (base.isNaN || exponent.isNaN) { 23 if (base.isNaN || exponent.isNaN) {
26 return double.NAN; 24 return double.NAN;
27 } 25 }
28 return _pow(base, doubleExponent); 26 return _pow(base, exponent);
29 } 27 }
30 28
31 double _pow(double base, double exponent) native "Math_doublePow"; 29 double _pow(double base, double exponent) native "Math_doublePow";
32 30
33 int _intPow(int base, int exponent) { 31 int _intPow(int base, int exponent) {
34 // Exponentiation by squaring. 32 // Exponentiation by squaring.
35 int result = 1; 33 int result = 1;
36 while (exponent != 0) { 34 while (exponent != 0) {
37 if ((exponent & 1) == 1) { 35 if ((exponent & 1) == 1) {
38 result *= base; 36 result *= base;
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 static int _nextSeed() { 159 static int _nextSeed() {
162 if (_prng == null) { 160 if (_prng == null) {
163 // TODO(iposva): Use system to get a random seed. 161 // TODO(iposva): Use system to get a random seed.
164 _prng = new Random(new DateTime.now().millisecondsSinceEpoch); 162 _prng = new Random(new DateTime.now().millisecondsSinceEpoch);
165 } 163 }
166 // Trigger the PRNG once to change the internal state. 164 // Trigger the PRNG once to change the internal state.
167 _prng._nextState(); 165 _prng._nextState();
168 return _prng._state[kSTATE_LO]; 166 return _prng._state[kSTATE_LO];
169 } 167 }
170 } 168 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698