#! @file hyperbolic.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains the hyperbolic functions, sinh, cosh, and tanh, and their inverses, #! asinh, acosh, and atanh. #! #! Ported from musl 1.2.5, which is Copyright © 2005-2020 Rich Felker, et al., #! under the MIT license. #! #! Each is within 2 ulp of the true value for `f64`, and `cosh` within 1: `tanh` #! comes nearest 2, near 0.2, and `acosh` near 1. An `f32` is worked in `f64` #! and rounded once more. import core::intrinsics; module std::math; #! The hyperbolic sine of a real, (e^x - e^-x) / 2. #! @tparam T The type of real. #! @param x The real. #! @returns sinh x, of x's sign: x itself near 0, and infinity past about #! 710.48. pub def sinh(x: T): T where T: real { return sinh_double(x as f64) as T; } #! The hyperbolic cosine of a real, (e^x + e^-x) / 2. #! @tparam T The type of real. #! @param x The real. #! @returns cosh x, at least 1: 1 near 0, and infinity past about 710.48 either #! way. pub def cosh(x: T): T where T: real { return cosh_double(x as f64) as T; } #! The hyperbolic tangent of a real, sinh x / cosh x. #! @tparam T The type of real. #! @param x The real. #! @returns tanh x, of x's sign: x itself near 0, and 1 past about 19.06, and #! -1 below about -19.06, infinities included. pub def tanh(x: T): T where T: real { return tanh_double(x as f64) as T; } #! The inverse hyperbolic sine of a real, the y whose sinh is x. #! @tparam T The type of real. #! @param x The real. #! @returns asinh x, of x's sign: x itself near 0, and infinities themselves. pub def asinh(x: T): T where T: real { return asinh_double(x as f64) as T; } #! The inverse hyperbolic cosine of a real, the y from 0 up whose cosh is x. #! @tparam T The type of real. #! @param x The real, at least 1. #! @returns acosh x: 0 at 1, infinity for infinity, and NaN below 1. pub def acosh(x: T): T where T: real { return acosh_double(x as f64) as T; } #! The inverse hyperbolic tangent of a real, the y whose tanh is x. #! @tparam T The type of real. #! @param x The real, from -1 to 1. #! @returns atanh x, of x's sign: x itself near 0, infinity of x's sign at 1 #! and -1, and NaN beyond them. pub def atanh(x: T): T where T: real { return atanh_double(x as f64) as T; } # A sign times e^x / 2, for x from about ln(DBL_MAX) up, where e^x overflows, # from musl's __expo2.c: e^(x - k ln 2) times 2^(k - 1). k is odd, so 2^(k - 1) # is the square of a scale, which multiplies twice as the square overflows, # and k ln 2 is a double with little relative error. const hyperbolic_k: i32 = 2043; const hyperbolic_k_ln2: f64 = 1416.0996898839683; def hyperbolic_expo2(x: f64, sign: f64): f64 { val scale := of_words(((0x3FF + hyperbolic_k / 2) as u32) << 20, 0); return exp_double(x - hyperbolic_k_ln2) * (sign * scale) * scale; } # sinh x, from musl's sinh.c, of e^|x| - 1. def sinh_double(x: f64): f64 { var h := 0.5; if (bits_of(x) >> 63) != 0 { h = -h; } val absx := real_of_bits(bits_of(x) & 0x7FFFFFFFFFFFFFFF); val w := high_word(absx); # |x| below ln(DBL_MAX). if w < 0x40862E42 { val t := expm1_double(absx); if w < 0x3FF00000 { # Below 2^-26, sinh x is x. return x if w < 0x3FF00000 - (26 << 20); return h * (2.0 * t - t * t / (t + 1.0)); } return h * (t + t / (t + 1.0)); } # |x| above ln(DBL_MAX), or NaN. return hyperbolic_expo2(absx, 2.0 * h); } # cosh x, from musl's cosh.c. def cosh_double(argument: f64): f64 { val x := real_of_bits(bits_of(argument) & 0x7FFFFFFFFFFFFFFF); val w := high_word(x); # |x| below ln 2, by e^|x| - 1. if w < 0x3FE62E42 { # Below 2^-26, cosh x is 1. return 1.0 if w < 0x3FF00000 - (26 << 20); val t := expm1_double(x); return 1.0 + t * t / (2.0 * (1.0 + t)); } # Below ln(DBL_MAX), by e^|x|. if w < 0x40862E42 { val t := exp_double(x); return 0.5 * (t + 1.0 / t); } # Above ln(DBL_MAX), or NaN. return hyperbolic_expo2(x, 1.0); } # tanh x, from musl's tanh.c, of e^2|x| - 1. def tanh_double(argument: f64): f64 { val negative := (bits_of(argument) >> 63) != 0; val x := real_of_bits(bits_of(argument) & 0x7FFFFFFFFFFFFFFF); val w := high_word(x); var t := 0.0; if w > 0x3FE193EA { # |x| above ln(3) / 2, about 0.5493, or NaN. if w > 0x40340000 { # Above 20, where tanh x is 1, or NaN, which this keeps. t = 1.0 - 0.0 / x; } else { t = expm1_double(2.0 * x); t = 1.0 - 2.0 / (t + 2.0); } } else if w > 0x3FD058AE { # Above ln(5/3) / 2, about 0.2554. t = expm1_double(2.0 * x); t = t / (t + 2.0); } else if w >= 0x00100000 { # Normal. t = expm1_double(-2.0 * x); t = -t / (t + 2.0); } else { # Subnormal, where tanh x is x. t = x; } return negative ? -t : t; } # asinh x, from musl's asinh.c: ln(|x| + sqrt(x^2 + 1)), of x's sign. def asinh_double(argument: f64): f64 { val bits := bits_of(argument); val e := (bits >> 52) & 0x7FF; val negative := (bits >> 63) != 0; var x := real_of_bits(bits & 0x7FFFFFFFFFFFFFFF); if e >= 0x3FF + 26 { # |x| at least 2^26, infinite, or NaN, where it is ln 2|x|. x = log_double(x) + ln_2; } else if e >= 0x3FF + 1 { # At least 2. x = log_double(2.0 * x + 1.0 / (square_root(x * x + 1.0) + x)); } else if e >= 0x3FF - 26 { # At least 2^-26. x = log1p_double(x + x * x / (square_root(x * x + 1.0) + 1.0)); } # Below 2^-26, asinh x is x. return negative ? -x : x; } # acosh x, from musl's acosh.c: ln(x + sqrt(x^2 - 1)). def acosh_double(x: f64): f64 { # Below 1, NaN. musl leaves this to the functions it calls, which give it but # for some x from -2^26 to -2, where they give a number; the NaN is the one # they give elsewhere. return invalid(x) if x < 1.0; val e := (bits_of(x) >> 52) & 0x7FF; # Below 2. return log1p_double(x - 1.0 + square_root((x - 1.0) * (x - 1.0) + 2.0 * (x - 1.0))) if e < 0x3FF + 1; # Below 2^26. return log_double(2.0 * x - 1.0 / (x + square_root(x * x - 1.0))) if e < 0x3FF + 26; # At least 2^26, or NaN, where it is ln 2x. return log_double(x) + ln_2; } # atanh x, from musl's atanh.c: ln((1 + x) / (1 - x)) / 2, of x's sign. def atanh_double(argument: f64): f64 { val bits := bits_of(argument); val e := (bits >> 52) & 0x7FF; val negative := (bits >> 63) != 0; var y := real_of_bits(bits & 0x7FFFFFFFFFFFFFFF); if e < 0x3FF - 1 { # |x| below 1/2, and below 2^-32, where atanh x is x. if e >= 0x3FF - 32 { y = 0.5 * log1p_double(2.0 * y + 2.0 * y * y / (1.0 - y)); } } else { # Divided first, so it doesn't overflow. y = 0.5 * log1p_double(2.0 * (y / (1.0 - y))); } return negative ? -y : y; }