OLD | NEW |
1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 function MathAtanh(x) { | 103 function MathAtanh(x) { |
104 if (!IS_NUMBER(x)) x = NonNumberToNumber(x); | 104 if (!IS_NUMBER(x)) x = NonNumberToNumber(x); |
105 // Idempotent for +/-0. | 105 // Idempotent for +/-0. |
106 if (x === 0) return x; | 106 if (x === 0) return x; |
107 // Returns NaN for NaN and +/- Infinity. | 107 // Returns NaN for NaN and +/- Infinity. |
108 if (!NUMBER_IS_FINITE(x)) return NAN; | 108 if (!NUMBER_IS_FINITE(x)) return NAN; |
109 return 0.5 * MathLog((1 + x) / (1 - x)); | 109 return 0.5 * MathLog((1 + x) / (1 - x)); |
110 } | 110 } |
111 | 111 |
112 | 112 |
| 113 //ES6 draft 09-27-13, section 20.2.2.17. |
| 114 function MathHypot(x, y) { // Function length is 2. |
| 115 // We may want to introduce fast paths for two arguments and when |
| 116 // normalization to avoid overflow is not necessary. For now, we |
| 117 // simply assume the general case. |
| 118 var length = %_ArgumentsLength(); |
| 119 var args = new InternalArray(length); |
| 120 var max = 0; |
| 121 for (var i = 0; i < length; i++) { |
| 122 var n = %_Arguments(i); |
| 123 if (!IS_NUMBER(n)) n = NonNumberToNumber(n); |
| 124 if (n === INFINITY || n === -INFINITY) return INFINITY; |
| 125 n = MathAbs(n); |
| 126 if (n > max) max = n; |
| 127 args[i] = n; |
| 128 } |
| 129 |
| 130 // Kahan summation to avoid rounding errors. |
| 131 // Normalize the numbers to the largest one to avoid overflow. |
| 132 if (max === 0) max = 1; |
| 133 var sum = 0; |
| 134 var compensation = 0; |
| 135 for (var i = 0; i < length; i++) { |
| 136 var n = args[i] / max; |
| 137 var summand = n * n - compensation; |
| 138 var preliminary = sum + summand; |
| 139 compensation = (preliminary - sum) - summand; |
| 140 sum = preliminary; |
| 141 } |
| 142 return MathSqrt(sum) * max; |
| 143 } |
| 144 |
| 145 |
113 function ExtendMath() { | 146 function ExtendMath() { |
114 %CheckIsBootstrapping(); | 147 %CheckIsBootstrapping(); |
115 | 148 |
116 // Set up the non-enumerable functions on the Math object. | 149 // Set up the non-enumerable functions on the Math object. |
117 InstallFunctions($Math, DONT_ENUM, $Array( | 150 InstallFunctions($Math, DONT_ENUM, $Array( |
118 "sign", MathSign, | 151 "sign", MathSign, |
119 "trunc", MathTrunc, | 152 "trunc", MathTrunc, |
120 "sinh", MathSinh, | 153 "sinh", MathSinh, |
121 "cosh", MathCosh, | 154 "cosh", MathCosh, |
122 "tanh", MathTanh, | 155 "tanh", MathTanh, |
123 "asinh", MathAsinh, | 156 "asinh", MathAsinh, |
124 "acosh", MathAcosh, | 157 "acosh", MathAcosh, |
125 "atanh", MathAtanh | 158 "atanh", MathAtanh, |
| 159 "hypot", MathHypot |
126 )); | 160 )); |
127 } | 161 } |
128 | 162 |
129 ExtendMath(); | 163 ExtendMath(); |
OLD | NEW |