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 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 // Binary search. | 167 // Binary search. |
168 if ((x & 0xFFFF0000) === 0) { x <<= 16; result += 16; }; | 168 if ((x & 0xFFFF0000) === 0) { x <<= 16; result += 16; }; |
169 if ((x & 0xFF000000) === 0) { x <<= 8; result += 8; }; | 169 if ((x & 0xFF000000) === 0) { x <<= 8; result += 8; }; |
170 if ((x & 0xF0000000) === 0) { x <<= 4; result += 4; }; | 170 if ((x & 0xF0000000) === 0) { x <<= 4; result += 4; }; |
171 if ((x & 0xC0000000) === 0) { x <<= 2; result += 2; }; | 171 if ((x & 0xC0000000) === 0) { x <<= 2; result += 2; }; |
172 if ((x & 0x80000000) === 0) { x <<= 1; result += 1; }; | 172 if ((x & 0x80000000) === 0) { x <<= 1; result += 1; }; |
173 return result; | 173 return result; |
174 } | 174 } |
175 | 175 |
176 | 176 |
177 //ES6 draft 09-27-13, section 20.2.2.9. | 177 // ES6 draft 09-27-13, section 20.2.2.9. |
178 function MathCbrt(x) { | 178 function MathCbrt(x) { |
179 return %Math_cbrt(TO_NUMBER_INLINE(x)); | 179 return %Math_cbrt(TO_NUMBER_INLINE(x)); |
180 } | 180 } |
181 | 181 |
182 | 182 |
183 //ES6 draft 09-27-13, section 20.2.2.14. | 183 // ES6 draft 09-27-13, section 20.2.2.14. |
| 184 // Use Taylor series to approximate. |
| 185 // exp(x) - 1 at 0 == -1 + exp(0) + exp'(0)*x/1! + exp''(0)*x^2/2! + ... |
| 186 // == x/1! + x^2/2! + x^3/3! + ... |
| 187 // The closer x is to 0, the fewer terms are required. |
184 function MathExpm1(x) { | 188 function MathExpm1(x) { |
185 return %Math_expm1(TO_NUMBER_INLINE(x)); | 189 if (!IS_NUMBER(x)) x = NonNumberToNumber(x); |
| 190 var xabs = MathAbs(x); |
| 191 if (xabs < 2E-7) { |
| 192 return x * (1 + x * (1/2)); |
| 193 } else if (xabs < 6E-5) { |
| 194 return x * (1 + x * (1/2 + x * (1/6))); |
| 195 } else if (xabs < 2E-2) { |
| 196 return x * (1 + x * (1/2 + x * (1/6 + |
| 197 x * (1/24 + x * (1/120 + x * (1/720)))))); |
| 198 } else { // Use regular exp if not close enough to 0. |
| 199 return MathExp(x) - 1; |
| 200 } |
186 } | 201 } |
187 | 202 |
188 | 203 |
189 //ES6 draft 09-27-13, section 20.2.2.20. | 204 // ES6 draft 09-27-13, section 20.2.2.20. |
| 205 // Use Taylor series to approximate. With y = x + 1; |
| 206 // log(y) at 1 == log(1) + log'(1)(y-1)/1! + log''(1)(y-1)^2/2! + ... |
| 207 // == 0 + x - x^2/2 + x^3/3 ... |
| 208 // The closer x is to 0, the fewer terms are required. |
190 function MathLog1p(x) { | 209 function MathLog1p(x) { |
191 return %Math_log1p(TO_NUMBER_INLINE(x)); | 210 if (!IS_NUMBER(x)) x = NonNumberToNumber(x); |
| 211 var xabs = MathAbs(x); |
| 212 if (xabs < 1E-7) { |
| 213 return x * (1 - x * (1/2)); |
| 214 } else if (xabs < 3E-5) { |
| 215 return x * (1 - x * (1/2 - x * (1/3))); |
| 216 } else if (xabs < 7E-3) { |
| 217 return x * (1 - x * (1/2 - x * (1/3 - x * (1/4 - |
| 218 x * (1/5 - x * (1/6 - x * (1/7))))))); |
| 219 } else { // Use regular log if not close enough to 0. |
| 220 return MathLog(1 + x); |
| 221 } |
192 } | 222 } |
193 | 223 |
194 | 224 |
195 function ExtendMath() { | 225 function ExtendMath() { |
196 %CheckIsBootstrapping(); | 226 %CheckIsBootstrapping(); |
197 | 227 |
198 // Set up the non-enumerable functions on the Math object. | 228 // Set up the non-enumerable functions on the Math object. |
199 InstallFunctions($Math, DONT_ENUM, $Array( | 229 InstallFunctions($Math, DONT_ENUM, $Array( |
200 "sign", MathSign, | 230 "sign", MathSign, |
201 "trunc", MathTrunc, | 231 "trunc", MathTrunc, |
202 "sinh", MathSinh, | 232 "sinh", MathSinh, |
203 "cosh", MathCosh, | 233 "cosh", MathCosh, |
204 "tanh", MathTanh, | 234 "tanh", MathTanh, |
205 "asinh", MathAsinh, | 235 "asinh", MathAsinh, |
206 "acosh", MathAcosh, | 236 "acosh", MathAcosh, |
207 "atanh", MathAtanh, | 237 "atanh", MathAtanh, |
208 "log10", MathLog10, | 238 "log10", MathLog10, |
209 "log2", MathLog2, | 239 "log2", MathLog2, |
210 "hypot", MathHypot, | 240 "hypot", MathHypot, |
211 "fround", MathFround, | 241 "fround", MathFround, |
212 "clz32", MathClz32, | 242 "clz32", MathClz32, |
213 "cbrt", MathCbrt, | 243 "cbrt", MathCbrt, |
214 "log1p", MathLog1p, | 244 "log1p", MathLog1p, |
215 "expm1", MathExpm1 | 245 "expm1", MathExpm1 |
216 )); | 246 )); |
217 } | 247 } |
218 | 248 |
219 | 249 |
220 ExtendMath(); | 250 ExtendMath(); |
OLD | NEW |