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

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

Issue 104173002: Reland "Implement hyperbolic math functions for ES6." (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: better deal with large negative input for asinh Created 7 years 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | test/mjsunit/harmony/math-hyperbolic.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 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 29 matching lines...) Expand all
40 // ES6 draft 09-27-13, section 20.2.2.34. 40 // ES6 draft 09-27-13, section 20.2.2.34.
41 function MathTrunc(x) { 41 function MathTrunc(x) {
42 x = TO_NUMBER_INLINE(x); 42 x = TO_NUMBER_INLINE(x);
43 if (x > 0) return MathFloor(x); 43 if (x > 0) return MathFloor(x);
44 if (x < 0) return MathCeil(x); 44 if (x < 0) return MathCeil(x);
45 if (x === 0) return x; 45 if (x === 0) return x;
46 return NAN; 46 return NAN;
47 } 47 }
48 48
49 49
50 // ES6 draft 09-27-13, section 20.2.2.30.
51 function MathSinh(x) {
52 if (!IS_NUMBER(x)) x = NonNumberToNumber(x);
53 // Idempotent for NaN, +/-0 and +/-Infinity.
54 if (x === 0 || !NUMBER_IS_FINITE(x)) return x;
55 return (MathExp(x) - MathExp(-x)) / 2;
56 }
57
58
59 // ES6 draft 09-27-13, section 20.2.2.12.
60 function MathCosh(x) {
61 if (!IS_NUMBER(x)) x = NonNumberToNumber(x);
62 // Idempotent for NaN and +/-Infinity.
63 if (!NUMBER_IS_FINITE(x)) return x;
64 return (MathExp(x) + MathExp(-x)) / 2;
65 }
66
67
68 // ES6 draft 09-27-13, section 20.2.2.33.
69 function MathTanh(x) {
70 if (!IS_NUMBER(x)) x = NonNumberToNumber(x);
71 // Idempotent for +/-0.
72 if (x === 0) return x;
73 // Returns +/-1 for +/-Infinity.
74 if (!NUMBER_IS_FINITE(x)) return MathSign(x);
75 var exp1 = MathExp(x);
76 var exp2 = MathExp(-x);
77 return (exp1 - exp2) / (exp1 + exp2);
78 }
79
80
81 // ES6 draft 09-27-13, section 20.2.2.5.
82 function MathAsinh(x) {
83 if (!IS_NUMBER(x)) x = NonNumberToNumber(x);
84 // Idempotent for NaN, +/-0 and +/-Infinity.
85 if (x === 0 || !NUMBER_IS_FINITE(x)) return x;
86 if (x > 0) return MathLog(x + MathSqrt(x * x + 1));
87 // This is to prevent numerical errors caused by large negative x.
88 return -MathLog(-x + MathSqrt(x * x + 1));
89 }
90
91
92 // ES6 draft 09-27-13, section 20.2.2.3.
93 function MathAcosh(x) {
94 if (!IS_NUMBER(x)) x = NonNumberToNumber(x);
95 if (x < 1) return NAN;
96 // Idempotent for NaN and +Infinity.
97 if (!NUMBER_IS_FINITE(x)) return x;
98 return MathLog(x + MathSqrt(x + 1) * MathSqrt(x - 1));
99 }
100
101
102 // ES6 draft 09-27-13, section 20.2.2.7.
103 function MathAtanh(x) {
104 if (!IS_NUMBER(x)) x = NonNumberToNumber(x);
105 // Idempotent for +/-0.
106 if (x === 0) return x;
107 // Returns NaN for NaN and +/- Infinity.
108 if (!NUMBER_IS_FINITE(x)) return NAN;
109 return 0.5 * MathLog((1 + x) / (1 - x));
110 }
111
112
50 function ExtendMath() { 113 function ExtendMath() {
51 %CheckIsBootstrapping(); 114 %CheckIsBootstrapping();
52 115
53 // Set up the non-enumerable functions on the Math object. 116 // Set up the non-enumerable functions on the Math object.
54 InstallFunctions($Math, DONT_ENUM, $Array( 117 InstallFunctions($Math, DONT_ENUM, $Array(
55 "sign", MathSign, 118 "sign", MathSign,
56 "trunc", MathTrunc 119 "trunc", MathTrunc,
120 "sinh", MathSinh,
121 "cosh", MathCosh,
122 "tanh", MathTanh,
123 "asinh", MathAsinh,
124 "acosh", MathAcosh,
125 "atanh", MathAtanh
57 )); 126 ));
58 } 127 }
59 128
60 ExtendMath(); 129 ExtendMath();
OLDNEW
« no previous file with comments | « no previous file | test/mjsunit/harmony/math-hyperbolic.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698