#! @file trig.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains the trigonometric functions: sine, cosine, and tangent, of an angle #! in radians. Each reduces its argument by multiples of π/2 to within about #! π/4 of 0, with `reduce_pio2`, exactly however large it is, and works the #! reduced angle with a polynomial. #! #! Ported from musl 1.2.5, which is Copyright © 2005-2020 Rich Felker, et al., #! under the MIT license. Its `sin`, `cos`, and `tan` are from FreeBSD's #! s_sin.c, s_cos.c, and s_tan.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. #! #! Its kernels `__sin` and `__cos` are from k_sin.c and k_cos.c, which 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 `__tan` is from k_tan.c, which says: #! #! Copyright 2004 Sun Microsystems, Inc. All Rights Reserved. #! #! 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; #! The sine of an angle. #! @tparam T The type of real. #! @param x The angle, in radians. #! @returns sin x, from -1 to 1: `-0` for `-0`, and NaN for infinity and NaN. pub def sin(x: T): T where T: real { return sin_double(x as f64) as T; } #! The cosine of an angle. #! @tparam T The type of real. #! @param x The angle, in radians. #! @returns cos x, from -1 to 1: 1 for `±0`, and NaN for infinity and NaN. pub def cos(x: T): T where T: real { return cos_double(x as f64) as T; } #! The tangent of an angle. #! @tparam T The type of real. #! @param x The angle, in radians. #! @returns tan x: `-0` for `-0`, and NaN for infinity and NaN. No double is an #! odd multiple of π/2, so it is always finite, at most about 2.1e18 #! in magnitude. pub def tan(x: T): T where T: real { return tan_double(x as f64) as T; } # sin x, cos x, and tan x, from musl's sin.c, cos.c, and tan.c. Each reduces x # to y = x - n·π/2, and as n modulo 4 is 0, 1, 2, or 3, sin x is sin y, cos y, # -sin y, or -cos y; cos x is cos y, -sin y, -cos y, or sin y; and tan x is # tan y or -1 / tan y. def sin_double(x: f64): f64 { val ix := high_word(x) & 0x7FFFFFFF; # Up to about π/4, where x needs no reducing, and below 2^-26, where sin x # rounds to x. if ix <= 0x3FE921FB { return x if ix < 0x3E500000; return trig_sin_kernel(x, 0.0, false); } # Infinity and NaN. return x - x if ix >= 0x7FF00000; val y := reduce_pio2(x); val n := y.n & 3; return trig_sin_kernel(y.hi, y.lo, true) if n == 0; return trig_cos_kernel(y.hi, y.lo) if n == 1; return -trig_sin_kernel(y.hi, y.lo, true) if n == 2; return -trig_cos_kernel(y.hi, y.lo); } def cos_double(x: f64): f64 { val ix := high_word(x) & 0x7FFFFFFF; # Up to about π/4, and below 2^-27·√2, where cos x rounds to 1. if ix <= 0x3FE921FB { return 1.0 if ix < 0x3E46A09E; return trig_cos_kernel(x, 0.0); } return x - x if ix >= 0x7FF00000; val y := reduce_pio2(x); val n := y.n & 3; return trig_cos_kernel(y.hi, y.lo) if n == 0; return -trig_sin_kernel(y.hi, y.lo, true) if n == 1; return -trig_cos_kernel(y.hi, y.lo) if n == 2; return trig_sin_kernel(y.hi, y.lo, true); } def tan_double(x: f64): f64 { val ix := high_word(x) & 0x7FFFFFFF; # Up to about π/4, and below 2^-27, where tan x rounds to x. if ix <= 0x3FE921FB { return x if ix < 0x3E400000; return trig_tan_kernel(x, 0.0, false); } return x - x if ix >= 0x7FF00000; val y := reduce_pio2(x); return trig_tan_kernel(y.hi, y.lo, (y.n & 1) != 0); } # The kernels, from musl's __sin.c, __cos.c, and __tan.c: each of x + y, where # |x + y| is at most about π/4 and y is below half an ulp of x. # sin x = x + S1·x^3 + ... + S6·x^13, within 2^-58, on [0, π/4]. const trig_s1: f64 = -0.16666666666666632; const trig_s2: f64 = 0.00833333333332249; const trig_s3: f64 = -0.0001984126982985795; const trig_s4: f64 = 2.7557313707070068e-06; const trig_s5: f64 = -2.5050760253406863e-08; const trig_s6: f64 = 1.58969099521155e-10; # sin(x + y), where y is taken as 0 unless `tail` is. Its polynomial doesn't # keep the sign of `-0`, which callers return themselves. def trig_sin_kernel(x: f64, y: f64, tail: bool): f64 { val z := x * x; val w := z * z; val r := trig_s2 + z * (trig_s3 + z * trig_s4) + z * w * (trig_s5 + z * trig_s6); val v := z * x; return x + v * (trig_s1 + z * r) if !tail; # sin(x + y) is about sin x + (1 - x^2/2)·y. return x - ((z * (0.5 * y - v * r) - y) - v * trig_s1); } # cos x = 1 - x^2/2 + C1·x^4 + ... + C6·x^14, within 2^-58, on [0, π/4]. const trig_c1: f64 = 0.0416666666666666; const trig_c2: f64 = -0.001388888888887411; const trig_c3: f64 = 2.480158728947673e-05; const trig_c4: f64 = -2.7557314351390663e-07; const trig_c5: f64 = 2.087572321298175e-09; const trig_c6: f64 = -1.1359647557788195e-11; # cos(x + y). It is about w + ((1 - w - x^2/2) + (r - x·y)), where w is # 1 - x^2/2 rounded, and the first term in parentheses is what that rounding # lost, and the second the rest of the polynomial and the tail's part, as # cos(x + y) is about cos x - x·y. def trig_cos_kernel(x: f64, y: f64): f64 { val z := x * x; var w := z * z; val r := z * (trig_c1 + z * (trig_c2 + z * trig_c3)) + w * w * (trig_c4 + z * (trig_c5 + z * trig_c6)); val hz := 0.5 * z; w = 1.0 - hz; return w + (((1.0 - w) - hz) + (z * r - x * y)); } # tan x = x + T[0]·x^3 + ... + T[12]·x^27, within 2^-59.2, on [0, 0.67434]. const trig_t: f64[13] = [ 0.3333333333333341, 0.13333333333320124, 0.05396825397622605, 0.021869488294859542, 0.0088632398235993, 0.0035920791075913124, 0.0014562094543252903, 0.0005880412408202641, 0.0002464631348184699, 7.817944429395571e-05, 7.140724913826082e-05, -1.8558637485527546e-05, 2.590730518636337e-05, ]; # π/4, as a high part and a low part. const trig_pio4: f64 = 0.7853981633974483; const trig_pio4_low: f64 = 3.061616997868383e-17; # tan(x + y), or -1 / tan(x + y) where `odd` is. Its polynomial doesn't keep the # sign of `-0`, which callers return themselves. def trig_tan_kernel(x0: f64, y0: f64, odd: bool): f64 { var x := x0; var y := y0; val hx := high_word(x); # From about 0.6744 up, tan x is worked as tan(π/4 - x'), where x' is # π/4 - |x|, which is (1 - tan x') / (1 + tan x'). val big := (hx & 0x7FFFFFFF) >= 0x3FE59428; val sign := (hx >> 31) != 0; if big { if sign { x = -x; y = -y; } x = (trig_pio4 - x) + (trig_pio4_low - y); y = 0.0; } val z := x * x; var w := z * z; # x^5 times the polynomial's terms from T[1], in two halves: the odd ones in # w = x^4, and the even ones, times z = x^2. var r := trig_t[1] + w * (trig_t[3] + w * (trig_t[5] + w * (trig_t[7] + w * (trig_t[9] + w * trig_t[11])))); var v := z * (trig_t[2] + w * (trig_t[4] + w * (trig_t[6] + w * (trig_t[8] + w * (trig_t[10] + w * trig_t[12]))))); var s := z * x; # tan(x + y) is about tan x + (1 + x^2)·y. r = y + z * (s * (r + v) + y) + s * trig_t[0]; w = x + r; if big { s = odd ? -1.0 : 1.0; v = s - 2.0 * (x + (r - w * w / (w + s))); return sign ? -v : v; } return w if !odd; # -1 / (x + r), which dividing alone would give within 2 ulp: w0 + v is x + r, # and a0 is -1 / w, each with its low word cleared, so the products are exact. val w0 := with_low_word(w, 0); v = r - (w0 - x); val a := -1.0 / w; val a0 := with_low_word(a, 0); return a0 + a * (1.0 + a0 * w0 + a0 * v); }