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

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

Issue 270743004: Refactor and improve code for pow. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 6 years, 7 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 | runtime/lib/typed_data.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 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 return _doublePow(x.toDouble(), exponent.toDouble()); 15 return _doublePow(x.toDouble(), exponent.toDouble());
16 } 16 }
17 17
18 double _doublePow(double base, double exponent) { 18 double _doublePow(double base, double exponent) {
19 if (exponent == 0.0) { 19 if (exponent == 0.0) {
20 return 1.0; // ECMA-262 15.8.2.13 20 return 1.0; // ECMA-262 15.8.2.13
21 } 21 }
22 // Speed up simple cases.
23 if (exponent == 1.0) return base;
24 if (exponent == 2.0) return base * base;
25 if (exponent == 3.0) return base * base * base;
26
22 if (base == 1.0) return 1.0; 27 if (base == 1.0) return 1.0;
23 28
24 if (base.isNaN || exponent.isNaN) { 29 if (base.isNaN || exponent.isNaN) {
25 return double.NAN; 30 return double.NAN;
26 } 31 }
27 if ((base != -double.INFINITY) && (exponent == 0.5)) { 32 if ((base != -double.INFINITY) && (exponent == 0.5)) {
28 if (base == 0.0) { 33 if (base == 0.0) {
29 return 0.0; 34 return 0.0;
30 } 35 }
31 return sqrt(base); 36 return sqrt(base);
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
157 static Uint32List _setupSeed(int seed) native "Random_setupSeed"; 162 static Uint32List _setupSeed(int seed) native "Random_setupSeed";
158 // Get a seed from the VM's random number provider. 163 // Get a seed from the VM's random number provider.
159 static Uint32List _initialSeed() native "Random_initialSeed"; 164 static Uint32List _initialSeed() native "Random_initialSeed";
160 165
161 static int _nextSeed() { 166 static int _nextSeed() {
162 // Trigger the PRNG once to change the internal state. 167 // Trigger the PRNG once to change the internal state.
163 _prng._nextState(); 168 _prng._nextState();
164 return _prng._state[kSTATE_LO]; 169 return _prng._state[kSTATE_LO];
165 } 170 }
166 } 171 }
OLDNEW
« no previous file with comments | « no previous file | runtime/lib/typed_data.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698