Commit d1e0c338 authored by yangguo@chromium.org's avatar yangguo@chromium.org

Implement hyperbolic math functions for ES6.

R=jarin@chromium.org
BUG=v8:2938
LOG=Y

Review URL: https://codereview.chromium.org/102023003

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@18245 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent a3c454da
...@@ -47,13 +47,80 @@ function MathTrunc(x) { ...@@ -47,13 +47,80 @@ function MathTrunc(x) {
} }
// ES6 draft 09-27-13, section 20.2.2.30.
function MathSinh(x) {
if (!IS_NUMBER(x)) x = NonNumberToNumber(x);
// Idempotent for NaN, +/-0 and +/-Infinity.
if (x === 0 || !NUMBER_IS_FINITE(x)) return x;
return (MathExp(x) - MathExp(-x)) / 2;
}
// ES6 draft 09-27-13, section 20.2.2.12.
function MathCosh(x) {
if (!IS_NUMBER(x)) x = NonNumberToNumber(x);
// Idempotent for NaN and +/-Infinity.
if (!NUMBER_IS_FINITE(x)) return x;
return (MathExp(x) + MathExp(-x)) / 2;
}
// ES6 draft 09-27-13, section 20.2.2.33.
function MathTanh(x) {
if (!IS_NUMBER(x)) x = NonNumberToNumber(x);
// Idempotent for +/-0.
if (x === 0) return x;
// Returns +/-1 for +/-Infinity.
if (!NUMBER_IS_FINITE(x)) return MathSign(x);
var exp1 = MathExp(x);
var exp2 = MathExp(-x);
return (exp1 - exp2) / (exp1 + exp2);
}
// ES6 draft 09-27-13, section 20.2.2.5.
function MathAsinh(x) {
if (!IS_NUMBER(x)) x = NonNumberToNumber(x);
// Idempotent for NaN, +/-0 and +/-Infinity.
if (x === 0 || !NUMBER_IS_FINITE(x)) return x;
return %Math_asinh(x);
}
// ES6 draft 09-27-13, section 20.2.2.3.
function MathAcosh(x) {
if (!IS_NUMBER(x)) x = NonNumberToNumber(x);
if (x < 1) return NAN;
// Idempotent for NaN and +Infinity.
if (!NUMBER_IS_FINITE(x)) return x;
return %Math_acosh(x);
}
// ES6 draft 09-27-13, section 20.2.2.7.
function MathAtanh(x) {
if (!IS_NUMBER(x)) x = NonNumberToNumber(x);
// Idempotent for +/-0.
if (x === 0) return x;
// Returns NaN for NaN and +/- Infinity.
if (!NUMBER_IS_FINITE(x)) return NAN;
return %Math_atanh(x);
}
function ExtendMath() { function ExtendMath() {
%CheckIsBootstrapping(); %CheckIsBootstrapping();
// Set up the non-enumerable functions on the Math object. // Set up the non-enumerable functions on the Math object.
InstallFunctions($Math, DONT_ENUM, $Array( InstallFunctions($Math, DONT_ENUM, $Array(
"sign", MathSign, "sign", MathSign,
"trunc", MathTrunc "trunc", MathTrunc,
"sinh", MathSinh,
"cosh", MathCosh,
"tanh", MathTanh,
"asinh", MathAsinh,
"acosh", MathAcosh,
"atanh", MathAtanh
)); ));
} }
......
...@@ -7788,6 +7788,21 @@ RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_pow_cfunction) { ...@@ -7788,6 +7788,21 @@ RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_pow_cfunction) {
} }
#define HYPERBOLIC_MATH_FUNCTION(name) \
RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_##name) { \
SealHandleScope shs(isolate); \
ASSERT(args.length() == 1); \
CONVERT_DOUBLE_ARG_CHECKED(x, 0); \
return isolate->heap()->AllocateHeapNumber(name(x)); \
}
HYPERBOLIC_MATH_FUNCTION(asinh)
HYPERBOLIC_MATH_FUNCTION(acosh)
HYPERBOLIC_MATH_FUNCTION(atanh)
#undef HYPERBOLIC_MATH_FUNCTION
RUNTIME_FUNCTION(MaybeObject*, Runtime_RoundNumber) { RUNTIME_FUNCTION(MaybeObject*, Runtime_RoundNumber) {
SealHandleScope shs(isolate); SealHandleScope shs(isolate);
ASSERT(args.length() == 1); ASSERT(args.length() == 1);
......
...@@ -190,6 +190,9 @@ namespace internal { ...@@ -190,6 +190,9 @@ namespace internal {
F(Math_sin, 1, 1) \ F(Math_sin, 1, 1) \
F(Math_sqrt, 1, 1) \ F(Math_sqrt, 1, 1) \
F(Math_tan, 1, 1) \ F(Math_tan, 1, 1) \
F(Math_asinh, 1, 1) \
F(Math_acosh, 1, 1) \
F(Math_atanh, 1, 1) \
\ \
/* Regular expressions */ \ /* Regular expressions */ \
F(RegExpCompile, 3, 1) \ F(RegExpCompile, 3, 1) \
......
// Copyright 2013 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Flags: --harmony-maths
[Math.sinh, Math.cosh, Math.tanh, Math.asinh, Math.acosh, Math.atanh].
forEach(function(fun) {
assertTrue(isNaN(fun(NaN)));
assertTrue(isNaN(fun("abc")));
assertTrue(isNaN(fun({})));
assertEquals(fun(0), fun([]));
assertTrue(isNaN(fun([1, 1])));
assertEquals(fun(1.11), fun({ toString: function() { return "1.11"; } }));
assertEquals(fun(-3.1), fun({ toString: function() { return -3.1; } }));
assertEquals(fun(-1.1), fun({ valueOf: function() { return "-1.1"; } }));
assertEquals(fun(3.11), fun({ valueOf: function() { return 3.11; } }));
});
function test_id(fun, rev, value) {
assertEqualsDelta(1, rev(fun(value))/value, 1E-7);
}
[Math.PI, 2, 5, 1E-5, 0.3].forEach(function(x) {
test_id(Math.sinh, Math.asinh, x);
test_id(Math.sinh, Math.asinh, -x);
test_id(Math.cosh, Math.acosh, x);
test_id(Math.tanh, Math.atanh, x);
test_id(Math.tanh, Math.atanh, -x);
});
[Math.sinh, Math.asinh, Math.tanh, Math.atanh].forEach(function(fun) {
assertEquals("-Infinity", String(1/fun(-0)));
assertEquals("Infinity", String(1/fun(0)));
});
[Math.sinh, Math.asinh, Math.cosh].forEach(function(fun) {
assertEquals("-Infinity", String(fun(-Infinity)));
assertEquals("Infinity", String(fun(Infinity)));
assertEquals("-Infinity", String(fun("-Infinity")));
assertEquals("Infinity", String(fun("Infinity")));
});
assertEquals("-Infinity", String(Math.atanh(-1)));
assertEquals("Infinity", String(Math.atanh(1)));
// Math.atanh(x) is NaN for |x| > 1 and NaN
[1.000000000001, Math.PI, 10000000, 2, Infinity, NaN].forEach(function(x) {
assertTrue(isNaN(Math.atanh(-x)));
assertTrue(isNaN(Math.atanh(x)));
});
assertEquals(1, Math.tanh(Infinity));
assertEquals(-1, Math.tanh(-Infinity));
assertEquals(1, Math.cosh(0));
assertEquals(1, Math.cosh(-0));
assertEquals(0, Math.acosh(1));
assertEquals("Infinity", String(Math.acosh(Infinity)));
// Math.acosh(x) is NaN for x < 1
[0.99999999999, 0.2, -1000, 0, -0].forEach(function(x) {
assertTrue(isNaN(Math.acosh(x)));
});
// Some random samples.
assertEqualsDelta(0.5210953054937, Math.sinh(0.5), 1E-12);
assertEqualsDelta(74.203210577788, Math.sinh(5), 1E-12);
assertEqualsDelta(-0.5210953054937, Math.sinh(-0.5), 1E-12);
assertEqualsDelta(-74.203210577788, Math.sinh(-5), 1E-12);
assertEqualsDelta(1.1276259652063, Math.cosh(0.5), 1E-12);
assertEqualsDelta(74.209948524787, Math.cosh(5), 1E-12);
assertEqualsDelta(1.1276259652063, Math.cosh(-0.5), 1E-12);
assertEqualsDelta(74.209948524787, Math.cosh(-5), 1E-12);
assertEqualsDelta(0.4621171572600, Math.tanh(0.5), 1E-12);
assertEqualsDelta(0.9999092042625, Math.tanh(5), 1E-12);
assertEqualsDelta(-0.4621171572600, Math.tanh(-0.5), 1E-12);
assertEqualsDelta(-0.9999092042625, Math.tanh(-5), 1E-12);
assertEqualsDelta(0.4812118250596, Math.asinh(0.5), 1E-12);
assertEqualsDelta(2.3124383412727, Math.asinh(5), 1E-12);
assertEqualsDelta(-0.4812118250596, Math.asinh(-0.5), 1E-12);
assertEqualsDelta(-2.3124383412727, Math.asinh(-5), 1E-12);
assertEqualsDelta(0.9624236501192, Math.acosh(1.5), 1E-12);
assertEqualsDelta(2.2924316695612, Math.acosh(5), 1E-12);
assertEqualsDelta(0.4435682543851, Math.acosh(1.1), 1E-12);
assertEqualsDelta(1.3169578969248, Math.acosh(2), 1E-12);
assertEqualsDelta(0.5493061443341, Math.atanh(0.5), 1E-12);
assertEqualsDelta(0.1003353477311, Math.atanh(0.1), 1E-12);
assertEqualsDelta(-0.5493061443341, Math.atanh(-0.5), 1E-12);
assertEqualsDelta(-0.1003353477311, Math.atanh(-0.1), 1E-12);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment