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 (function(global, utils) { | 5 (function(global, utils) { |
6 "use strict"; | 6 "use strict"; |
7 | 7 |
8 %CheckIsBootstrapping(); | 8 %CheckIsBootstrapping(); |
9 | 9 |
10 // ------------------------------------------------------------------- | 10 // ------------------------------------------------------------------- |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 | 59 |
60 // ES6 draft 09-27-13, section 20.2.2.3. | 60 // ES6 draft 09-27-13, section 20.2.2.3. |
61 function MathAcosh(x) { | 61 function MathAcosh(x) { |
62 x = TO_NUMBER(x); | 62 x = TO_NUMBER(x); |
63 if (x < 1) return NaN; | 63 if (x < 1) return NaN; |
64 // Idempotent for NaN and +Infinity. | 64 // Idempotent for NaN and +Infinity. |
65 if (!NUMBER_IS_FINITE(x)) return x; | 65 if (!NUMBER_IS_FINITE(x)) return x; |
66 return %math_log(x + %math_sqrt(x + 1) * %math_sqrt(x - 1)); | 66 return %math_log(x + %math_sqrt(x + 1) * %math_sqrt(x - 1)); |
67 } | 67 } |
68 | 68 |
69 // ES6 draft 09-27-13, section 20.2.2.17. | |
70 function MathHypot(x, y) { // Function length is 2. | |
71 // We may want to introduce fast paths for two arguments and when | |
72 // normalization to avoid overflow is not necessary. For now, we | |
73 // simply assume the general case. | |
74 var length = arguments.length; | |
75 var max = 0; | |
76 for (var i = 0; i < length; i++) { | |
77 var n = %math_abs(arguments[i]); | |
78 if (n > max) max = n; | |
79 arguments[i] = n; | |
80 } | |
81 if (max === INFINITY) return INFINITY; | |
82 | |
83 // Kahan summation to avoid rounding errors. | |
84 // Normalize the numbers to the largest one to avoid overflow. | |
85 if (max === 0) max = 1; | |
86 var sum = 0; | |
87 var compensation = 0; | |
88 for (var i = 0; i < length; i++) { | |
89 var n = arguments[i] / max; | |
90 var summand = n * n - compensation; | |
91 var preliminary = sum + summand; | |
92 compensation = (preliminary - sum) - summand; | |
93 sum = preliminary; | |
94 } | |
95 return %math_sqrt(sum) * max; | |
96 } | |
97 | 69 |
98 // ------------------------------------------------------------------- | 70 // ------------------------------------------------------------------- |
99 | 71 |
100 %AddNamedProperty(GlobalMath, toStringTagSymbol, "Math", READ_ONLY | DONT_ENUM); | 72 %AddNamedProperty(GlobalMath, toStringTagSymbol, "Math", READ_ONLY | DONT_ENUM); |
101 | 73 |
102 // Set up math constants. | 74 // Set up math constants. |
103 utils.InstallConstants(GlobalMath, [ | 75 utils.InstallConstants(GlobalMath, [ |
104 "PI", 3.1415926535897932, | 76 "PI", 3.1415926535897932, |
105 "SQRT1_2", 0.7071067811865476, | 77 "SQRT1_2", 0.7071067811865476, |
106 "SQRT2", 1.4142135623730951 | 78 "SQRT2", 1.4142135623730951 |
107 ]); | 79 ]); |
108 | 80 |
109 // Set up non-enumerable functions of the Math object and | 81 // Set up non-enumerable functions of the Math object and |
110 // set their names. | 82 // set their names. |
111 utils.InstallFunctions(GlobalMath, DONT_ENUM, [ | 83 utils.InstallFunctions(GlobalMath, DONT_ENUM, [ |
112 "random", MathRandom, | 84 "random", MathRandom, |
113 "sign", MathSign, | 85 "sign", MathSign, |
114 "asinh", MathAsinh, | 86 "asinh", MathAsinh, |
115 "acosh", MathAcosh, | 87 "acosh", MathAcosh |
116 "hypot", MathHypot, | |
117 ]); | 88 ]); |
118 | 89 |
119 %SetForceInlineFlag(MathRandom); | 90 %SetForceInlineFlag(MathRandom); |
120 %SetForceInlineFlag(MathSign); | 91 %SetForceInlineFlag(MathSign); |
121 | 92 |
122 // ------------------------------------------------------------------- | 93 // ------------------------------------------------------------------- |
123 // Exports | 94 // Exports |
124 | 95 |
125 utils.Export(function(to) { | 96 utils.Export(function(to) { |
126 to.MathRandom = MathRandom; | 97 to.MathRandom = MathRandom; |
127 }); | 98 }); |
128 | 99 |
129 }) | 100 }) |
OLD | NEW |