diff options
| author | Marc Tiehuis <marctiehuis@gmail.com> | 2017-06-20 23:01:04 +1200 |
|---|---|---|
| committer | Marc Tiehuis <marctiehuis@gmail.com> | 2017-06-20 23:10:22 +1200 |
| commit | 5bbec42a4edb3f6758a9d67fcf763c660ac3f204 (patch) | |
| tree | 3dc324d083f142935df53c5900edfc04c0f956f8 /std/math/tanh.zig | |
| parent | c9fc8bd802f5ed52c4cc78b93f18fc5dc9b6bb7f (diff) | |
| download | zig-5bbec42a4edb3f6758a9d67fcf763c660ac3f204.tar.gz zig-5bbec42a4edb3f6758a9d67fcf763c660ac3f204.zip | |
Add math special case tests and general fixes
- Should cover special case inputs for most functions
- Fixed a number of runtime panicking behaviour reliant on shift
overflow/division by zero etc.
Diffstat (limited to 'std/math/tanh.zig')
| -rw-r--r-- | std/math/tanh.zig | 37 |
1 files changed, 34 insertions, 3 deletions
diff --git a/std/math/tanh.zig b/std/math/tanh.zig index bf4d86f7e7..a76de9ce22 100644 --- a/std/math/tanh.zig +++ b/std/math/tanh.zig @@ -1,6 +1,12 @@ +// Special Cases: +// +// - sinh(+-0) = +-0 +// - sinh(+-inf) = +-1 +// - sinh(nan) = nan + const math = @import("index.zig"); const assert = @import("../debug.zig").assert; -const expo2 = @import("_expo2.zig").expo2; +const expo2 = @import("expo2.zig").expo2; // TODO issue #393 pub const tanh = tanh_workaround; @@ -64,11 +70,19 @@ fn tanh64(x: f64) -> f64 { var t: f64 = undefined; + // TODO: Shouldn't need these checks. + if (x == 0.0) { + return x; + } + if (math.isNan(x)) { + return x; + } + // |x| < log(3) / 2 ~= 0.5493 or nan - if (w > 0x3Fe193EA) { + if (w > 0x3FE193EA) { // |x| > 20 or nan if (w > 0x40340000) { - t = 1.0 + 0 / x; + t = 1.0; // TODO + 0 / x; } else { t = math.expm1(2 * x); t = 1 - 2 / (t + 2); @@ -121,3 +135,20 @@ test "math.tanh64" { assert(math.approxEq(f64, tanh64(1.5), 0.905148, epsilon)); assert(math.approxEq(f64, tanh64(37.45), 1.0, epsilon)); } + +test "math.tanh32.special" { + // TODO: Error on release (like pow) + assert(tanh32(0.0) == 0.0); + assert(tanh32(-0.0) == -0.0); + assert(tanh32(math.inf(f32)) == 1.0); + assert(tanh32(-math.inf(f32)) == -1.0); + assert(math.isNan(tanh32(math.nan(f32)))); +} + +test "math.tanh64.special" { + assert(tanh64(0.0) == 0.0); + assert(tanh64(-0.0) == -0.0); + assert(tanh64(math.inf(f64)) == 1.0); + assert(tanh64(-math.inf(f64)) == -1.0); + assert(math.isNan(tanh64(math.nan(f64)))); +} |
