diff options
| author | Marc Tiehuis <marctiehuis@gmail.com> | 2017-06-16 20:26:10 +1200 |
|---|---|---|
| committer | Marc Tiehuis <marctiehuis@gmail.com> | 2017-06-16 20:32:31 +1200 |
| commit | 4c16f9a3c35b23b9917f2a27b91ba8cd20e6fd82 (patch) | |
| tree | 778f0f06734f7dc17e9269ee1cf5b513f7b504c0 /std/math/asin.zig | |
| parent | 865b53f2860405a718262abf9a794d2bf9529dbc (diff) | |
| download | zig-4c16f9a3c35b23b9917f2a27b91ba8cd20e6fd82.tar.gz zig-4c16f9a3c35b23b9917f2a27b91ba8cd20e6fd82.zip | |
Add math library
This covers the majority of the functions as covered by the C99
specification for a math library.
Code is adapted primarily from musl libc, with the pow and standard
trigonometric functions adapted from the Go stdlib.
Changes:
- Remove assert expose in index and import as needed.
- Add float log function and merge with existing base 2 integer
implementation.
See https://github.com/tiehuis/zig-fmath.
See #374.
Diffstat (limited to 'std/math/asin.zig')
| -rw-r--r-- | std/math/asin.zig | 157 |
1 files changed, 157 insertions, 0 deletions
diff --git a/std/math/asin.zig b/std/math/asin.zig new file mode 100644 index 0000000000..e671477b3c --- /dev/null +++ b/std/math/asin.zig @@ -0,0 +1,157 @@ +const math = @import("index.zig"); +const assert = @import("../debug.zig").assert; + +pub fn asin(x: var) -> @typeOf(x) { + const T = @typeOf(x); + switch (T) { + f32 => @inlineCall(asin32, x), + f64 => @inlineCall(asin64, x), + else => @compileError("asin not implemented for " ++ @typeName(T)), + } +} + +fn r32(z: f32) -> f32 { + const pS0 = 1.6666586697e-01; + const pS1 = -4.2743422091e-02; + const pS2 = -8.6563630030e-03; + const qS1 = -7.0662963390e-01; + + const p = z * (pS0 + z * (pS1 + z * pS2)); + const q = 1.0 + z * qS1; + p / q +} + +fn asin32(x: f32) -> f32 { + const pio2 = 1.570796326794896558e+00; + + const hx: u32 = @bitCast(u32, x); + const ix: u32 = hx & 0x7FFFFFFF; + + // |x| >= 1 + if (ix >= 0x3F800000) { + // |x| >= 1 + if (ix == 0x3F800000) { + return x * pio2 + 0x1.0p-120; // asin(+-1) = +-pi/2 with inexact + } else { + return 0 / (x - x); // asin(|x| > 1) is nan + } + } + + // |x| < 0.5 + if (ix < 0x3F000000) { + // 0x1p-126 <= |x| < 0x1p-12 + if (ix < 0x39800000 and ix >= 0x00800000) { + return x; + } else { + return x + x * r32(x * x); + } + } + + // 1 > |x| >= 0.5 + const z = (1 - math.fabs(x)) * 0.5; + const s = math.sqrt(z); + const fx = pio2 - 2 * (s + s * r32(z)); + + if (hx >> 31 != 0) { + -fx + } else { + fx + } +} + +fn r64(z: f64) -> f64 { + const pS0: f64 = 1.66666666666666657415e-01; + const pS1: f64 = -3.25565818622400915405e-01; + const pS2: f64 = 2.01212532134862925881e-01; + const pS3: f64 = -4.00555345006794114027e-02; + const pS4: f64 = 7.91534994289814532176e-04; + const pS5: f64 = 3.47933107596021167570e-05; + const qS1: f64 = -2.40339491173441421878e+00; + const qS2: f64 = 2.02094576023350569471e+00; + const qS3: f64 = -6.88283971605453293030e-01; + const qS4: f64 = 7.70381505559019352791e-02; + + const p = z * (pS0 + z * (pS1 + z * (pS2 + z * (pS3 + z * (pS4 + z * pS5))))); + const q = 1.0 + z * (qS1 + z * (qS2 + z * (qS3 + z * qS4))); + p / q +} + +fn asin64(x: f64) -> f64 { + const pio2_hi: f64 = 1.57079632679489655800e+00; + const pio2_lo: f64 = 6.12323399573676603587e-17; + + const ux = @bitCast(u64, x); + const hx = u32(ux >> 32); + const ix = hx & 0x7FFFFFFF; + + // |x| >= 1 or nan + if (ix >= 0x3FF00000) { + const lx = u32(ux & 0xFFFFFFFF); + + // asin(1) = +-pi/2 with inexact + if ((ix - 0x3FF00000) | lx == 0) { + return x * pio2_hi + 0x1.0p-120; + } else { + return 0/ (x - x); + } + } + + // |x| < 0.5 + if (ix < 0x3FE00000) { + // if 0x1p-1022 <= |x| < 0x1p-26 avoid raising overflow + if (ix < 0x3E500000 and ix >= 0x00100000) { + return x; + } else { + return x + x * r64(x * x); + } + } + + // 1 > |x| >= 0.5 + const z = (1 - math.fabs(x)) * 0.5; + const s = math.sqrt(z); + const r = r64(z); + var fx: f64 = undefined; + + // |x| > 0.975 + if (ix >= 0x3FEF3333) { + fx = pio2_hi - 2 * (s + s * r) + } else { + const jx = @bitCast(u64, s); + const df = @bitCast(f64, jx & 0xFFFFFFFF00000000); + const c = (z - df * df) / (s + df); + fx = 0.5 * pio2_hi - (2 * s * r - (pio2_lo - 2 * c) - (0.5 * pio2_hi - 2 * df)); + } + + if (hx >> 31 != 0) { + -fx + } else { + fx + } +} + +test "asin" { + assert(asin(f32(0.0)) == asin32(0.0)); + assert(asin(f64(0.0)) == asin64(0.0)); +} + +test "asin32" { + const epsilon = 0.000001; + + assert(math.approxEq(f32, asin32(0.0), 0.0, epsilon)); + assert(math.approxEq(f32, asin32(0.2), 0.201358, epsilon)); + assert(math.approxEq(f32, asin32(-0.2), -0.201358, epsilon)); + assert(math.approxEq(f32, asin32(0.3434), 0.350535, epsilon)); + assert(math.approxEq(f32, asin32(0.5), 0.523599, epsilon)); + assert(math.approxEq(f32, asin32(0.8923), 1.102415, epsilon)); +} + +test "asin64" { + const epsilon = 0.000001; + + assert(math.approxEq(f64, asin64(0.0), 0.0, epsilon)); + assert(math.approxEq(f64, asin64(0.2), 0.201358, epsilon)); + assert(math.approxEq(f64, asin64(-0.2), -0.201358, epsilon)); + assert(math.approxEq(f64, asin64(0.3434), 0.350535, epsilon)); + assert(math.approxEq(f64, asin64(0.5), 0.523599, epsilon)); + assert(math.approxEq(f64, asin64(0.8923), 1.102415, epsilon)); +} |
