Files
std/math/inverse.sakura
T
2026-11-14 15:32:55 -06:00

315 lines
10 KiB
Plaintext

#! @file inverse.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the inverse trigonometric functions: the arcsine, the arccosine,
#! and the arctangent, of one real and of a point's two coordinates.
#!
#! Ported from musl 1.2.5, which is Copyright © 2005-2020 Rich Felker, et al.,
#! under the MIT license. Its `asin`, `acos`, `atan`, and `atan2` are from
#! FreeBSD's e_asin.c, e_acos.c, s_atan.c, and e_atan2.c, of Sun's fdlibm, by
#! way of musl. e_asin.c, e_acos.c, and e_atan2.c say:
#!
#! Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
#!
#! Developed at SunSoft, a Sun Microsystems, Inc. business.
#! Permission to use, copy, modify, and distribute this
#! software is freely granted, provided that this notice
#! is preserved.
#!
#! and s_atan.c says:
#!
#! 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.
#!
#! For `f64`, `asin`, `acos`, and `atan` are within 0.9 ulp of the true value,
#! and `atan2` within about 1.5 ulp, since it rounds y / x before taking its
#! arctangent. An `f32` is worked in `f64` and rounded once more.
import core::intrinsics;
module std::math;
#! The arcsine of a real: the angle, in radians, whose sine it is.
#! @tparam T The type of real.
#! @param x The sine, from -1 to 1.
#! @returns The angle, from -pi/2 to pi/2: `-0` for `-0`, and NaN past 1
#! either way or for NaN.
pub def asin<T>(x: T): T where T: real {
return asin_double(x as f64) as T;
}
#! The arccosine of a real: the angle, in radians, whose cosine it is.
#! @tparam T The type of real.
#! @param x The cosine, from -1 to 1.
#! @returns The angle, from 0 to pi: 0 for 1, pi for -1, and NaN past 1 either
#! way or for NaN.
pub def acos<T>(x: T): T where T: real {
return acos_double(x as f64) as T;
}
#! The arctangent of a real: the angle, in radians, whose tangent it is.
#! @tparam T The type of real.
#! @param x The tangent.
#! @returns The angle, from -pi/2 to pi/2: `-0` for `-0`, pi/2 for infinity,
#! -pi/2 for negative infinity, and NaN for NaN.
pub def atan<T>(x: T): T where T: real {
return atan_double(x as f64) as T;
}
#! The angle, in radians, from the positive x axis to the point (x, y): the
#! arctangent of y / x, in the quadrant of the point.
#! @tparam T The type of real.
#! @param y The point's y coordinate.
#! @param x The point's x coordinate.
#! @returns The angle, from -pi to pi, of the sign of y, zeros included. A
#! `±0` y gives `±0` where x is `0` or above, and `±pi` where x is
#! `-0` or below. A zero x, or an infinite y, gives `±pi/2` otherwise,
#! and an infinite x `±0` or `±pi`, or `±pi/4` or `±3pi/4` where y is
#! infinite too. NaN where either is NaN.
pub def atan2<T>(y: T, x: T): T where T: real {
return atan2_double(y as f64, x as f64) as T;
}
# The data `asin` and `acos` share, from musl's asin.c and acos.c.
# pi/2, as a high part and a low part.
const inverse_pio2_high: f64 = 1.5707963267948966;
const inverse_pio2_low: f64 = 6.123233995736766e-17;
# A rational function near (asin(x) - x) / x^3, of x^2, on [0, 0.5]: the
# coefficients of its numerator and of its denominator.
const inverse_p0: f64 = 0.16666666666666666;
const inverse_p1: f64 = -0.3255658186224009;
const inverse_p2: f64 = 0.20121253213486293;
const inverse_p3: f64 = -0.04005553450067941;
const inverse_p4: f64 = 0.0007915349942898145;
const inverse_p5: f64 = 3.479331075960212e-05;
const inverse_q1: f64 = -2.403394911734414;
const inverse_q2: f64 = 2.0209457602335057;
const inverse_q3: f64 = -0.6882839716054533;
const inverse_q4: f64 = 0.07703815055590194;
# The rational function, of z = x^2.
def inverse_rational(z: f64): f64 {
val p := z * (inverse_p0 + z * (inverse_p1 + z * (inverse_p2 + z * (inverse_p3 + z * (inverse_p4 + z * inverse_p5)))));
val q := 1.0 + z * (inverse_q1 + z * (inverse_q2 + z * (inverse_q3 + z * inverse_q4)));
return p / q;
}
# The arcsine, from musl's asin.c.
def asin_double(x: f64): f64 {
val hx := high_word(x);
val ix := hx & 0x7FFFFFFF;
# At 1 and past it either way, or NaN: asin(±1) is ±pi/2.
if ix >= 0x3FF00000 {
return x * inverse_pio2_high if ((ix - 0x3FF00000) | low_word(x)) == 0;
return 0.0 / (x - x);
}
# Below 0.5, asin(x) is x + x^3 R(x^2), and x itself below 2^-26.
if ix < 0x3FE00000 {
return x if ix < 0x3E500000 && ix >= 0x00100000;
return x + x * inverse_rational(x * x);
}
# From 0.5 to 1, asin(x) is pi/2 - 2 asin(s), where s is sqrt((1 - |x|) / 2)
# and its arcsine is s + s z R(z) for z = s^2.
val z := (1.0 - abs(x)) * 0.5;
val s := square_root(z);
val r := inverse_rational(z);
var y: f64 = 0;
if ix >= 0x3FEF3333 {
# Above 0.975.
y = inverse_pio2_high - (2.0 * (s + s * r) - inverse_pio2_low);
} else {
# f + c is s, where f is s with its low word cleared.
val f := with_low_word(s, 0);
val c := (z - f * f) / (s + f);
y = 0.5 * inverse_pio2_high - (2.0 * s * r - (inverse_pio2_low - 2.0 * c) - (0.5 * inverse_pio2_high - 2.0 * f));
}
return (hx >> 31) != 0 ? -y : y;
}
# The arccosine, from musl's acos.c.
def acos_double(x: f64): f64 {
val hx := high_word(x);
val ix := hx & 0x7FFFFFFF;
# At 1 and past it either way, or NaN: acos(1) is 0 and acos(-1) is pi.
if ix >= 0x3FF00000 {
if ((ix - 0x3FF00000) | low_word(x)) == 0 {
return 2.0 * inverse_pio2_high if (hx >> 31) != 0;
return 0.0;
}
return 0.0 / (x - x);
}
# Below 0.5, acos(x) is pi/2 - asin(x), and pi/2 itself below 2^-57.
if ix < 0x3FE00000 {
return inverse_pio2_high if ix <= 0x3C600000;
return inverse_pio2_high - (x - (inverse_pio2_low - x * inverse_rational(x * x)));
}
# Below -0.5, acos(x) is pi - 2 asin(s), where s is sqrt((1 + x) / 2).
if (hx >> 31) != 0 {
val z := (1.0 + x) * 0.5;
val s := square_root(z);
val w := inverse_rational(z) * s - inverse_pio2_low;
return 2.0 * (inverse_pio2_high - (s + w));
}
# Above 0.5, acos(x) is 2 asin(s), where s is sqrt((1 - x) / 2), worked as
# df + c, where df is s with its low word cleared.
val z := (1.0 - x) * 0.5;
val s := square_root(z);
val df := with_low_word(s, 0);
val c := (z - df * df) / (s + df);
val w := inverse_rational(z) * s + c;
return 2.0 * (df + w);
}
# The data of `atan`, from musl's atan.c.
# atan(0.5), atan(1), atan(1.5), and atan(infinity), as high parts and low
# parts.
const atan_high: f64[4] = [
0.4636476090008061, 0.7853981633974483, 0.982793723247329, 1.5707963267948966,
];
const atan_low: f64[4] = [
2.2698777452961687e-17, 3.061616997868383e-17, 1.3903311031230998e-17,
6.123233995736766e-17,
];
# The polynomial of (x - atan(x)) / x^3, of x^2, on [0, 7/16].
const atan_t: f64[11] = [
0.3333333333333293, -0.19999999999876483, 0.14285714272503466,
-0.11111110405462356, 0.09090887133436507, -0.0769187620504483,
0.06661073137387531, -0.058335701337905735, 0.049768779946159324,
-0.036531572744216916, 0.016285820115365782,
];
# The arctangent, from musl's atan.c.
def atan_double(x: f64): f64 {
var ix := high_word(x);
val sign := ix >> 31;
ix &= 0x7FFFFFFF;
# From 2^66 up, atan(x) is ±pi/2, or NaN.
if ix >= 0x44100000 {
return x if x != x;
val z := atan_high[3];
return sign != 0 ? -z : z;
}
# |x| is reduced to t, near one of 0, 0.5, 1, 1.5, and infinity, whose
# arctangent is id's of atan_high and atan_low.
var t := x;
var id: i32 = -1;
if ix < 0x3FDC0000 {
# Below 7/16, and x itself below 2^-27.
return x if ix < 0x3E400000;
} else {
t = abs(x);
if ix < 0x3FF30000 {
if ix < 0x3FE60000 {
# From 7/16 to 11/16.
id = 0;
t = (2.0 * t - 1.0) / (2.0 + t);
} else {
# From 11/16 to 19/16.
id = 1;
t = (t - 1.0) / (t + 1.0);
}
} else {
if ix < 0x40038000 {
# From 19/16 to 39/16.
id = 2;
t = (t - 1.5) / (1.0 + 1.5 * t);
} else {
# From 39/16 to 2^66.
id = 3;
t = -1.0 / t;
}
}
}
# The polynomial, of its odd and even terms apart.
val z := t * t;
val w := z * z;
val s1 := z * (atan_t[0] + w * (atan_t[2] + w * (atan_t[4] + w * (atan_t[6] + w * (atan_t[8] + w * atan_t[10])))));
val s2 := w * (atan_t[1] + w * (atan_t[3] + w * (atan_t[5] + w * (atan_t[7] + w * atan_t[9]))));
return t - t * (s1 + s2) if id < 0;
val k := id as u64;
val y := atan_high[k] - (t * (s1 + s2) - atan_low[k] - t);
return sign != 0 ? -y : y;
}
# The data of `atan2`, from musl's atan2.c: pi, as a high part and a low part.
const atan_pi: f64 = 3.141592653589793;
const atan_pi_low: f64 = 1.2246467991473532e-16;
# The arctangent of y / x, in the point's quadrant, from musl's atan2.c.
def atan2_double(y: f64, x: f64): f64 {
return x + y if x != x || y != y;
var ix := high_word(x);
val lx := low_word(x);
var iy := high_word(y);
val ly := low_word(y);
# atan2(y, 1) is atan(y).
return atan_double(y) if ((ix - 0x3FF00000) | lx) == 0;
# Twice x's sign and y's.
val m := ((iy >> 31) & 1) | ((ix >> 30) & 2);
ix &= 0x7FFFFFFF;
iy &= 0x7FFFFFFF;
# y is zero: ±0 where x's sign is clear, and ±pi where it is set.
if (iy | ly) == 0 {
return y if m < 2;
return m == 2 ? atan_pi : -atan_pi;
}
# x is zero: ±pi/2.
if (ix | lx) == 0 {
return (m & 1) != 0 ? -atan_pi / 2.0 : atan_pi / 2.0;
}
# x is infinite.
if ix == 0x7FF00000 {
if iy == 0x7FF00000 {
return atan_pi / 4.0 if m == 0;
return -atan_pi / 4.0 if m == 1;
return 3.0 * atan_pi / 4.0 if m == 2;
return -3.0 * atan_pi / 4.0;
}
return 0.0 if m == 0;
return -0.0 if m == 1;
return atan_pi if m == 2;
return -atan_pi;
}
# |y / x| is above 2^64, or y is infinite: ±pi/2.
if ix + 0x04000000 < iy || iy == 0x7FF00000 {
return (m & 1) != 0 ? -atan_pi / 2.0 : atan_pi / 2.0;
}
# atan(|y / x|), taken as 0 where it is below 2^-64 and x is negative, so it
# does not underflow.
var z: f64 = 0;
if (m & 2) == 0 || iy + 0x04000000 >= ix {
z = atan_double(abs(y / x));
}
return z if m == 0;
return -z if m == 1;
return atan_pi - (z - atan_pi_low) if m == 2;
return (z - atan_pi_low) - atan_pi;
}