sign(-0) is -0 according to spec; add cosh; add tests

This commit is contained in:
rolux 2012-06-13 09:04:16 +02:00
parent a627f41755
commit bc83d22850

View file

@ -3,6 +3,8 @@
/*@
Ox.acosh <f> Inverse hyperbolic cosine
Missing from `Math`.
> Ox.acosh(1)
0
@*/
Ox.acosh = function(x) {
return Math.log(x + Math.sqrt(x * x - 1));
@ -21,11 +23,23 @@ Ox.asinh = function(x) {
/*@
Ox.atanh <f> Inverse hyperbolic tangent
Missing from `Math`.
> Ox.atanh(0)
0
@*/
Ox.atanh = function(x) {
return 0.5 * Math.log((1 + x) / (1 - x));
};
/*@
Ox.cosh <f> Hyperbolic cosine
Missing from `Math`
> Ox.cosh(0)
1
@*/
Ox.cosh = function(x) {
return (Math.exp(x) + Math.exp(-x)) / 2;
};
/*@
Ox.deg <f> Takes radians, returns degrees
Missing from `Math`.
@ -152,14 +166,17 @@ Ox.sign <f> Returns the sign of a number (-1, 0 or 1)
> Ox.sign(-Infinity)
-1
> Ox.sign(-0)
0
-0
> Ox.sign(NaN)
NaN
> Ox.sign(0)
0
> Ox.sign(Infinity)
1
@*/
Ox.sign = function(x) {
return x !== x || x === 0 ? +x : x < 0 ? -1 : 1;
x = +x;
return x !== x || x === 0 ? x : x < 0 ? -1 : 1;
};
/*@
@ -175,6 +192,8 @@ Ox.sinh = function(x) {
/*@
Ox.tanh <f> Hyperbolic tangent
Missing from `Math`.
> Ox.tanh(0)
0
@*/
Ox.tanh = function(x) {
return (Math.exp(x) - Math.exp(-x)) / (Math.exp(x) + Math.exp(-x));