210 lines
6.5 KiB
Plaintext
210 lines
6.5 KiB
Plaintext
#! @file expm1.sakura
|
|||
|
|
#! @author John Christman (sorakatadzuma@gmail.com)
|
||
|
|
#! @copyright Malunal Studios, LLC.
|
||
|
|
#! @license MIT
|
||
|
|
#!
|
||
|
|
#! Contains e^x - 1 and ln(1 + x), which keep their precision where x is near
|
||
|
|
#! 0, as `exp(x) - 1` and `log(1 + x)` lose it.
|
||
|
|
#!
|
||
|
|
#! Ported from musl 1.2.5, which is Copyright © 2005-2020 Rich Felker, et al.,
|
||
|
|
#! under the MIT license. Its `expm1` and `log1p` are from FreeBSD's s_expm1.c
|
||
|
|
#! and s_log1p.c, which say:
|
||
|
|
#!
|
||
|
|
#! Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
|
||
|
|
#!
|
||
|
|
#! Developed at SunPro, a Sun Microsystems, Inc. business.
|
||
|
|
#! Permission to use, copy, modify, and distribute this
|
||
|
|
#! software is freely granted, provided that this notice
|
||
|
|
#! is preserved.
|
||
|
|
#!
|
||
|
|
#! Each is within 1 ulp of the true value for `f64`. An `f32` is worked in
|
||
|
|
#! `f64` and rounded once more.
|
||
|
|
|
||
|
|
module std::math;
|
||
|
|
|
||
|
|
|
||
|
|
#! e raised to a power, less 1, which is precise where the power is near 0.
|
||
|
|
#! @tparam T The type of real.
|
||
|
|
#! @param x The power.
|
||
|
|
#! @returns e^x - 1: x itself near 0, infinity past about 709.78, and -1 below
|
||
|
|
#! about -37.43 and for negative infinity.
|
||
|
|
pub def expm1<T>(x: T): T where T: real {
|
||
|
|
return expm1_double(x as f64) as T;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The natural logarithm of 1 more than a real, which is precise where the real
|
||
|
|
#! is near 0.
|
||
|
|
#! @tparam T The type of real.
|
||
|
|
#! @param x The real.
|
||
|
|
#! @returns ln(1 + x): x itself near 0, negative infinity at -1, and NaN below
|
||
|
|
#! -1.
|
||
|
|
pub def log1p<T>(x: T): T where T: real {
|
||
|
|
return log1p_double(x as f64) as T;
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
# The constants of `expm1` and `log1p`.
|
||
|
|
|
||
|
|
# Past this, about 709.78, e^x - 1 overflows.
|
||
|
|
const expm1_overflow: f64 = 709.782712893384;
|
||
|
|
|
||
|
|
# ln 2, as a high part, whose last 32 bits are 0 so that multiplying it by a
|
||
|
|
# small integer is exact, and a low part. `log1p` has them too.
|
||
|
|
const expm1_ln2_high: f64 = 0.6931471803691238;
|
||
|
|
const expm1_ln2_low: f64 = 1.9082149292705877e-10;
|
||
|
|
|
||
|
|
# 1 / ln 2.
|
||
|
|
const expm1_inverse_ln2: f64 = 1.4426950408889634;
|
||
|
|
|
||
|
|
# The rational function of r near 0: the coefficients of its polynomial in
|
||
|
|
# (r^2) / 2, each the nth scaled by 2^n.
|
||
|
|
const expm1_q1: f64 = -0.03333333333333313;
|
||
|
|
const expm1_q2: f64 = 0.0015873015872548146;
|
||
|
|
const expm1_q3: f64 = -7.93650757867488e-05;
|
||
|
|
const expm1_q4: f64 = 4.008217827329362e-06;
|
||
|
|
const expm1_q5: f64 = -2.0109921818362437e-07;
|
||
|
|
|
||
|
|
# The polynomial of ln(1 + f), in s^2 where s is f / (2 + f).
|
||
|
|
const log1p_lg1: f64 = 0.6666666666666735;
|
||
|
|
const log1p_lg2: f64 = 0.3999999999940942;
|
||
|
|
const log1p_lg3: f64 = 0.2857142874366239;
|
||
|
|
const log1p_lg4: f64 = 0.22222198432149784;
|
||
|
|
const log1p_lg5: f64 = 0.1818357216161805;
|
||
|
|
const log1p_lg6: f64 = 0.15313837699209373;
|
||
|
|
const log1p_lg7: f64 = 0.14798198605116586;
|
||
|
|
|
||
|
|
|
||
|
|
# e^x - 1, from musl's expm1.c.
|
||
|
|
def expm1_double(argument: f64): f64 {
|
||
|
|
var x := argument;
|
||
|
|
val hx := high_word(x) & 0x7FFFFFFF;
|
||
|
|
val negative := (bits_of(x) >> 63) != 0;
|
||
|
|
|
||
|
|
# |x| at least 56 ln 2, where e^x - 1 is -1 below 0 and may overflow above,
|
||
|
|
# or infinite, or NaN.
|
||
|
|
if hx >= 0x4043687A {
|
||
|
|
return x if x != x;
|
||
|
|
return -1.0 if negative;
|
||
|
|
return x * 8.98846567431158e307 if x > expm1_overflow;
|
||
|
|
}
|
||
|
|
|
||
|
|
# x is k ln 2 + r, where r is at most ln 2 / 2 from 0, as hi - lo, and c is
|
||
|
|
# what rounding r lost.
|
||
|
|
var k: i32 = 0;
|
||
|
|
var c := 0.0;
|
||
|
|
if hx > 0x3FD62E42 {
|
||
|
|
# |x| above ln 2 / 2.
|
||
|
|
var hi := 0.0;
|
||
|
|
var lo := 0.0;
|
||
|
|
if hx < 0x3FF0A2B2 {
|
||
|
|
# And below 3 ln 2 / 2, where k is 1 or -1.
|
||
|
|
if !negative {
|
||
|
|
hi = x - expm1_ln2_high;
|
||
|
|
lo = expm1_ln2_low;
|
||
|
|
k = 1;
|
||
|
|
} else {
|
||
|
|
hi = x + expm1_ln2_high;
|
||
|
|
lo = -expm1_ln2_low;
|
||
|
|
k = -1;
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
k = (expm1_inverse_ln2 * x + (negative ? -0.5 : 0.5)) as i32;
|
||
|
|
val t := k as f64;
|
||
|
|
# Exact, as the high part's last bits are 0.
|
||
|
|
hi = x - t * expm1_ln2_high;
|
||
|
|
lo = t * expm1_ln2_low;
|
||
|
|
}
|
||
|
|
x = hi - lo;
|
||
|
|
c = (hi - x) - lo;
|
||
|
|
} else if hx < 0x3C900000 {
|
||
|
|
# |x| below 2^-54, where e^x - 1 is x.
|
||
|
|
return x;
|
||
|
|
}
|
||
|
|
|
||
|
|
# e^r - 1 is r - e, by a rational function of r^2 / 2.
|
||
|
|
val hfx := 0.5 * x;
|
||
|
|
val hxs := x * hfx;
|
||
|
|
val r1 := 1.0 + hxs * (expm1_q1 + hxs * (expm1_q2 + hxs * (expm1_q3 + hxs * (expm1_q4 + hxs * expm1_q5))));
|
||
|
|
val t := 3.0 - r1 * hfx;
|
||
|
|
var e := hxs * ((r1 - t) / (6.0 - x * t));
|
||
|
|
return x - (x * e - hxs) if k == 0;
|
||
|
|
# c corrected for, e^x is about 2^k (r - e + 1).
|
||
|
|
e = x * (e - c) - c;
|
||
|
|
e -= hxs;
|
||
|
|
return 0.5 * (x - e) - 0.5 if k == -1;
|
||
|
|
if k == 1 {
|
||
|
|
return -2.0 * (e - (x + 0.5)) if x < -0.25;
|
||
|
|
return 1.0 + 2.0 * (x - e);
|
||
|
|
}
|
||
|
|
val twopk := real_of_bits(((0x3FF + k) as u64) << 52);
|
||
|
|
if k < 0 || k > 56 {
|
||
|
|
# k below 0 or past 56, where 2^k (r - e + 1) - 1 is near enough.
|
||
|
|
var y := x - e + 1.0;
|
||
|
|
if k == 1024 {
|
||
|
|
# 2^k overflows, so it is 2 * 2^1023.
|
||
|
|
y = y * 2.0 * 8.98846567431158e307;
|
||
|
|
} else {
|
||
|
|
y = y * twopk;
|
||
|
|
}
|
||
|
|
return y - 1.0;
|
||
|
|
}
|
||
|
|
# Else 2^k (r - e + (1 - 2^-k)), the 1 taken away before scaling.
|
||
|
|
val twomk := real_of_bits(((0x3FF - k) as u64) << 52);
|
||
|
|
return (x - e + (1.0 - twomk)) * twopk if k < 20;
|
||
|
|
return (x - (e + twomk) + 1.0) * twopk;
|
||
|
|
}
|
||
|
|
|
||
|
|
# ln(1 + x), from musl's log1p.c.
|
||
|
|
def log1p_double(x: f64): f64 {
|
||
|
|
val hx := high_word(x);
|
||
|
|
# 1 + x is 2^k (1 + f), where 1 + f is from sqrt(2) / 2 to sqrt 2, and c
|
||
|
|
# corrects for 1 + x being rounded.
|
||
|
|
var k: i32 = 1;
|
||
|
|
var c := 0.0;
|
||
|
|
var f := 0.0;
|
||
|
|
if hx < 0x3FDA827A || (hx >> 31) != 0 {
|
||
|
|
# 1 + x below sqrt 2, a little over.
|
||
|
|
if hx >= 0xBFF00000 {
|
||
|
|
# x at most -1, or NaN of the sign bit.
|
||
|
|
return x / 0.0 if x == -1.0;
|
||
|
|
return (x - x) / 0.0;
|
||
|
|
}
|
||
|
|
# |x| below 2^-53, where ln(1 + x) is x.
|
||
|
|
return x if (hx << 1) < (0x3CA00000 << 1);
|
||
|
|
if hx <= 0xBFD2BEC4 {
|
||
|
|
# 1 + x from sqrt(2) / 2, a little under, where x is f itself.
|
||
|
|
k = 0;
|
||
|
|
c = 0.0;
|
||
|
|
f = x;
|
||
|
|
}
|
||
|
|
} else if hx >= 0x7FF00000 {
|
||
|
|
# Infinite, or NaN.
|
||
|
|
return x;
|
||
|
|
}
|
||
|
|
if k != 0 {
|
||
|
|
var u := 1.0 + x;
|
||
|
|
var hu := high_word(u);
|
||
|
|
hu += 0x3FF00000 - 0x3FE6A09E;
|
||
|
|
k = ((hu >> 20) as i32) - 0x3FF;
|
||
|
|
# ln(1 + x) - ln u is about c / u, which is too small to count past 2^54.
|
||
|
|
if k < 54 {
|
||
|
|
c = k >= 2 ? 1.0 - (u - x) : x - (u - 1.0);
|
||
|
|
c /= u;
|
||
|
|
} else {
|
||
|
|
c = 0.0;
|
||
|
|
}
|
||
|
|
# u scaled into [sqrt(2) / 2, sqrt 2].
|
||
|
|
hu = (hu & 0x000FFFFF) + 0x3FE6A09E;
|
||
|
|
u = with_high_word(u, hu);
|
||
|
|
f = u - 1.0;
|
||
|
|
}
|
||
|
|
val hfsq := 0.5 * f * f;
|
||
|
|
val s := f / (2.0 + f);
|
||
|
|
val z := s * s;
|
||
|
|
val w := z * z;
|
||
|
|
val t1 := w * (log1p_lg2 + w * (log1p_lg4 + w * log1p_lg6));
|
||
|
|
val t2 := z * (log1p_lg1 + w * (log1p_lg3 + w * (log1p_lg5 + w * log1p_lg7)));
|
||
|
|
val r := t2 + t1;
|
||
|
|
val dk := k as f64;
|
||
|
|
return s * (hfsq + r) + (dk * expm1_ln2_low + c) - hfsq + f + dk * expm1_ln2_high;
|
||
|
|
}
|