dymokomi/luce-std
Source of release 0.7.4
0.7.4 / src / math / hyperbolic.lucb
#==============================================================================================
#
# hyperbolic - sinh, cosh and tanh
#
# DESCRIPTION:
# sinh, cosh and tanh first try the fast path at the end of this file, which returns
# only results it proves correctly rounded. From their definitions in e^x, as fdlibm does, but with e^|x| as exp.lucb's pair
# (about 2^-68) and the reciprocal, sum, difference and quotient taken as pairs, so
# each result rounds once. Below 2^-7, where e^x - e^-x would cancel, sinh and tanh are
# their Taylor series (to x^7 and x^9, below 2^-70); from 22 on, e^-|x| is under 2^-63
# of e^|x| and sinh and cosh are e^|x|/2, scaled without overflowing early. Written
# anew from the method.
#
#==============================================================================================
## e^a as a pair, for 0 <= a <= 22, where 2^m scales the pair exactly.
inline func exp_scaled_pair(a: f64) -> (f64, f64):
let (vh, vl, m) = exp_pair(a, 0.0)
let s = power_of_two(m)
return (vh * s, vl * s)
## The hyperbolic sine: odd, overflowing past about 710.48.
func sinh_accurate(x: f64) -> f64:
let a = abs(x)
if not (a < 746.0):
# past the overflow, infinity with x's sign; NaN stays NaN
return x * infinity
if a < 0.0078125:
if a < 1.4901161193847656e-08:
# below 2^-26, x³/6 is under half an ulp of x: x itself, signed zero kept
return x
let z = x * x
return x + x * z * (0.16666666666666666 + z * (0.008333333333333333 + z * 0.0001984126984126984))
if a >= 22.0:
let (vh, vl, m) = exp_pair(a, 0.0)
return copysign(scale_pair(vh, vl, m - 1), x)
let (eh, el) = exp_scaled_pair(a)
let (ih, il) = divide_pairs(1.0, 0.0, eh, el)
let (dh, dl) = add_pairs(eh, el, -ih, -il)
return copysign(0.5 * (dh + dl), x)
## The hyperbolic cosine: even, at least 1, overflowing past about 710.48.
func cosh_accurate(x: f64) -> f64:
let a = abs(x)
if not (a < 746.0):
return a * infinity
if a < 7.450580596923828e-09:
# below 2^-27, x²/2 is under half an ulp of 1
return 1.0
if a >= 22.0:
let (vh, vl, m) = exp_pair(a, 0.0)
return scale_pair(vh, vl, m - 1)
let (eh, el) = exp_scaled_pair(a)
let (ih, il) = divide_pairs(1.0, 0.0, eh, el)
let (sh, sl) = add_pairs(eh, el, ih, il)
return 0.5 * (sh + sl)
## The hyperbolic tangent: odd, (e^2x - 1)/(e^2x + 1), within half an ulp of ±1 from 22.
func tanh_accurate(x: f64) -> f64:
let a = abs(x)
if not (a < 22.0):
return x + x if x != x else copysign(1.0, x)
if a < 0.0078125:
if a < 7.450580596923828e-09:
# below 2^-27, x³/3 is under half an ulp of x: x itself, signed zero kept
return x
let z = x * x
return x + x * z * (-0.3333333333333333 + z * (0.13333333333333333 + z * (-0.05396825396825397 + z * 0.021869488536155203)))
let (eh, el) = exp_scaled_pair(a + a)
let (nh, nl) = add_pairs(eh, el, -1.0, 0.0)
let (dh, dl) = add_pairs(eh, el, 1.0, 0.0)
let (qh, ql) = divide_pairs(nh, nl, dh, dl)
return copysign(qh + ql, x)
# mark: The fast path =========================================================================
# e^a from exp.lucb's fast path as a pair E = (h + l)·2^m (within 2^-67), its reciprocal
# as a pair by a fused multiply-add's exact remainder, and the sum or difference with its
# error kept: cosh a = (E + 1/E)/2 for a below 709.7, within 2^-66; sinh a = (E - 1/E)/2
# from a = 2^-5, where the difference is at least 2^-4 and its error under 2^-66
# absolute; tanh their quotient as pairs from 2^-5 to 22, under 2^-61.9. Below 2^-5, sinh
# and tanh are their Taylor series in a, the first term exact and the rest a double under
# 2^-11.6 of it, carrying four roundings (under 2^-62.6 of a) and a truncation under 2^-68.
# The tests allow 2^-63 (cosh), 2^-62 of the result and 2^-65 (sinh), and 2^-60 and 2^-61
# (tanh), so a fast return is the correctly rounded value; the rest take the method above.
## e^a and e^-a as pairs, for 0 <= a < 709.7.
inline func exp_both(a: f64) -> (f64, f64, f64, f64):
# exp_parts' low part reaches 2^-17 of the high: renormalize before the reciprocal
let (h0, l0, m) = exp_parts(a)
let (h, l) = fast_two_sum(h0, l0)
let s = power_of_two(m)
let eh = h * s
let el = l * s
let ih = 1.0 / eh
let il = ((-ih).mul_add(eh, 1.0) - ih * el) / eh
return (eh, el, ih, il)
## Whether h + l rounds the same at both ends of `relative`·|h|.
inline func hyperbolic_rounds(h: f64, l: f64, relative: f64) -> bool:
let bound = abs(h) * relative
return h + (l - bound) == h + (l + bound)
## The hyperbolic sine: odd, overflowing past about 710.48.
func sinh_value(x: f64) -> f64:
let a = abs(x)
if a >= 0.03125 and a < 709.7:
let (eh, el, ih, il) = exp_both(a)
let (d, de) = fast_two_sum(eh, -ih)
let low = de + (el - il)
# 2^-62 of the difference, and 2^-65 for e^a's and e^-a's errors near a = 2^-5
let bound = abs(d) * 2.168404344971009e-19 + 2.710505431213761e-20
if d + (low - bound) == d + (low + bound):
return copysign(0.5 * (d + low), x)
elif a >= 1.4901161193847656e-08 and a < 0.03125:
let z = a * a
let t = a * z * z.mul_add(z.mul_add(z.mul_add(z.mul_add(2.505210838544172e-08, 2.7557319223985893e-06), 0.0001984126984126984), 0.008333333333333333), 0.16666666666666666)
if hyperbolic_rounds(a, t, 2.168404344971009e-19):
return copysign(a + t, x)
return sinh_accurate(x)
## The hyperbolic cosine: even, at least 1, overflowing past about 710.48.
func cosh_value(x: f64) -> f64:
let a = abs(x)
if a < 709.7:
let (eh, el, ih, il) = exp_both(a)
let (s, se) = fast_two_sum(eh, ih)
let low = se + (el + il)
if hyperbolic_rounds(s, low, 1.0842021724855044e-19):
return 0.5 * (s + low)
return cosh_accurate(x)
## The hyperbolic tangent: odd, (e^a - e^-a)/(e^a + e^-a), within half an ulp of ±1 from 22.
func tanh_value(x: f64) -> f64:
let a = abs(x)
if a >= 0.03125 and a < 22.0:
let (eh, el, ih, il) = exp_both(a)
let (n0, n1) = fast_two_sum(eh, -ih)
let (nh, nl) = fast_two_sum(n0, n1 + (el - il))
let (d0, d1) = fast_two_sum(eh, ih)
let (dh, dl) = fast_two_sum(d0, d1 + (el + il))
let qh = nh / dh
let ql = ((-qh).mul_add(dh, nh) + (nl - qh * dl)) / dh
if hyperbolic_rounds(qh, ql, 8.673617379884035e-19):
return copysign(qh + ql, x)
elif a >= 7.450580596923828e-09 and a < 0.03125:
let z = a * a
let t = a * z * z.mul_add(z.mul_add(z.mul_add(z.mul_add(-0.008863235529902197, 0.021869488536155203), -0.05396825396825397), 0.13333333333333333), -0.3333333333333333)
if hyperbolic_rounds(a, t, 4.336808689942018e-19):
return copysign(a + t, x)
return tanh_accurate(x)