OLD | NEW |
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 "use strict"; | 5 "use strict"; |
6 | 6 |
7 // This file relies on the fact that the following declarations have been made | 7 // This file relies on the fact that the following declarations have been made |
8 // in runtime.js: | 8 // in runtime.js: |
9 // var $Object = global.Object; | 9 // var $Object = global.Object; |
10 | 10 |
(...skipping 329 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
340 } else if (xabs < 6E-5) { | 340 } else if (xabs < 6E-5) { |
341 return x * (1 + x * (1/2 + x * (1/6))); | 341 return x * (1 + x * (1/2 + x * (1/6))); |
342 } else if (xabs < 2E-2) { | 342 } else if (xabs < 2E-2) { |
343 return x * (1 + x * (1/2 + x * (1/6 + | 343 return x * (1 + x * (1/2 + x * (1/6 + |
344 x * (1/24 + x * (1/120 + x * (1/720)))))); | 344 x * (1/24 + x * (1/120 + x * (1/720)))))); |
345 } else { // Use regular exp if not close enough to 0. | 345 } else { // Use regular exp if not close enough to 0. |
346 return MathExp(x) - 1; | 346 return MathExp(x) - 1; |
347 } | 347 } |
348 } | 348 } |
349 | 349 |
350 // ES6 draft 09-27-13, section 20.2.2.20. | |
351 // Use Taylor series to approximate. With y = x + 1; | |
352 // log(y) at 1 == log(1) + log'(1)(y-1)/1! + log''(1)(y-1)^2/2! + ... | |
353 // == 0 + x - x^2/2 + x^3/3 ... | |
354 // The closer x is to 0, the fewer terms are required. | |
355 function MathLog1p(x) { | |
356 if (!IS_NUMBER(x)) x = NonNumberToNumber(x); | |
357 var xabs = MathAbs(x); | |
358 if (xabs < 1E-7) { | |
359 return x * (1 - x * (1/2)); | |
360 } else if (xabs < 3E-5) { | |
361 return x * (1 - x * (1/2 - x * (1/3))); | |
362 } else if (xabs < 7E-3) { | |
363 return x * (1 - x * (1/2 - x * (1/3 - x * (1/4 - | |
364 x * (1/5 - x * (1/6 - x * (1/7))))))); | |
365 } else { // Use regular log if not close enough to 0. | |
366 return MathLog(1 + x); | |
367 } | |
368 } | |
369 | |
370 // ------------------------------------------------------------------- | 350 // ------------------------------------------------------------------- |
371 | 351 |
372 function SetUpMath() { | 352 function SetUpMath() { |
373 %CheckIsBootstrapping(); | 353 %CheckIsBootstrapping(); |
374 | 354 |
375 %SetPrototype($Math, $Object.prototype); | 355 %SetPrototype($Math, $Object.prototype); |
376 %AddNamedProperty(global, "Math", $Math, DONT_ENUM); | 356 %AddNamedProperty(global, "Math", $Math, DONT_ENUM); |
377 %FunctionSetInstanceClassName(MathConstructor, 'Math'); | 357 %FunctionSetInstanceClassName(MathConstructor, 'Math'); |
378 | 358 |
379 // Set up math constants. | 359 // Set up math constants. |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
421 "tanh", MathTanh, | 401 "tanh", MathTanh, |
422 "asinh", MathAsinh, | 402 "asinh", MathAsinh, |
423 "acosh", MathAcosh, | 403 "acosh", MathAcosh, |
424 "atanh", MathAtanh, | 404 "atanh", MathAtanh, |
425 "log10", MathLog10, | 405 "log10", MathLog10, |
426 "log2", MathLog2, | 406 "log2", MathLog2, |
427 "hypot", MathHypot, | 407 "hypot", MathHypot, |
428 "fround", MathFroundJS, | 408 "fround", MathFroundJS, |
429 "clz32", MathClz32, | 409 "clz32", MathClz32, |
430 "cbrt", MathCbrt, | 410 "cbrt", MathCbrt, |
431 "log1p", MathLog1p, | 411 "log1p", MathLog1p, // implemented by third_party/fdlibm |
432 "expm1", MathExpm1 | 412 "expm1", MathExpm1 |
433 )); | 413 )); |
434 | 414 |
435 %SetInlineBuiltinFlag(MathCeil); | 415 %SetInlineBuiltinFlag(MathCeil); |
436 %SetInlineBuiltinFlag(MathRandom); | 416 %SetInlineBuiltinFlag(MathRandom); |
437 %SetInlineBuiltinFlag(MathSin); | 417 %SetInlineBuiltinFlag(MathSin); |
438 %SetInlineBuiltinFlag(MathCos); | 418 %SetInlineBuiltinFlag(MathCos); |
439 } | 419 } |
440 | 420 |
441 SetUpMath(); | 421 SetUpMath(); |
OLD | NEW |