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

Side by Side Diff: src/js/math.js

Issue 1669773002: [math] Fix Math.hypot to properly call ToNumber on all arguments. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 4 years, 10 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
« no previous file with comments | « no previous file | test/mjsunit/to_number_order.js » ('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 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 (function(global, utils) { 5 (function(global, utils) {
6 "use strict"; 6 "use strict";
7 7
8 %CheckIsBootstrapping(); 8 %CheckIsBootstrapping();
9 9
10 // ------------------------------------------------------------------- 10 // -------------------------------------------------------------------
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
157 // Returns NaN for NaN and +/- Infinity. 157 // Returns NaN for NaN and +/- Infinity.
158 if (!NUMBER_IS_FINITE(x)) return NaN; 158 if (!NUMBER_IS_FINITE(x)) return NaN;
159 return 0.5 * MathLog((1 + x) / (1 - x)); 159 return 0.5 * MathLog((1 + x) / (1 - x));
160 } 160 }
161 161
162 // ES6 draft 09-27-13, section 20.2.2.17. 162 // ES6 draft 09-27-13, section 20.2.2.17.
163 function MathHypot(x, y) { // Function length is 2. 163 function MathHypot(x, y) { // Function length is 2.
164 // We may want to introduce fast paths for two arguments and when 164 // We may want to introduce fast paths for two arguments and when
165 // normalization to avoid overflow is not necessary. For now, we 165 // normalization to avoid overflow is not necessary. For now, we
166 // simply assume the general case. 166 // simply assume the general case.
167 var length = %_ArgumentsLength(); 167 var length = arguments.length;
168 var args = new InternalArray(length);
169 var max = 0; 168 var max = 0;
170 for (var i = 0; i < length; i++) { 169 for (var i = 0; i < length; i++) {
171 var n = %_Arguments(i); 170 var n = MathAbs(arguments[i]);
172 n = TO_NUMBER(n);
173 if (n === INFINITY || n === -INFINITY) return INFINITY;
174 n = MathAbs(n);
175 if (n > max) max = n; 171 if (n > max) max = n;
176 args[i] = n; 172 arguments[i] = n;
177 } 173 }
174 if (max === INFINITY) return INFINITY;
178 175
179 // Kahan summation to avoid rounding errors. 176 // Kahan summation to avoid rounding errors.
180 // Normalize the numbers to the largest one to avoid overflow. 177 // Normalize the numbers to the largest one to avoid overflow.
181 if (max === 0) max = 1; 178 if (max === 0) max = 1;
182 var sum = 0; 179 var sum = 0;
183 var compensation = 0; 180 var compensation = 0;
184 for (var i = 0; i < length; i++) { 181 for (var i = 0; i < length; i++) {
185 var n = args[i] / max; 182 var n = arguments[i] / max;
186 var summand = n * n - compensation; 183 var summand = n * n - compensation;
187 var preliminary = sum + summand; 184 var preliminary = sum + summand;
188 compensation = (preliminary - sum) - summand; 185 compensation = (preliminary - sum) - summand;
189 sum = preliminary; 186 sum = preliminary;
190 } 187 }
191 return %_MathSqrt(sum) * max; 188 return %_MathSqrt(sum) * max;
192 } 189 }
193 190
194 // ES6 draft 09-27-13, section 20.2.2.16. 191 // ES6 draft 09-27-13, section 20.2.2.16.
195 function MathFroundJS(x) { 192 function MathFroundJS(x) {
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
289 // Exports 286 // Exports
290 287
291 utils.Export(function(to) { 288 utils.Export(function(to) {
292 to.MathAbs = MathAbs; 289 to.MathAbs = MathAbs;
293 to.MathExp = MathExp; 290 to.MathExp = MathExp;
294 to.MathFloor = MathFloorJS; 291 to.MathFloor = MathFloorJS;
295 to.IntRandom = MathRandomRaw; 292 to.IntRandom = MathRandomRaw;
296 }); 293 });
297 294
298 }) 295 })
OLDNEW
« no previous file with comments | « no previous file | test/mjsunit/to_number_order.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698