diff options
| author | Andrew Kelley <superjoe30@gmail.com> | 2017-06-19 14:36:33 -0400 |
|---|---|---|
| committer | Andrew Kelley <superjoe30@gmail.com> | 2017-06-19 14:36:33 -0400 |
| commit | c9fc8bd802f5ed52c4cc78b93f18fc5dc9b6bb7f (patch) | |
| tree | 8ddb992d7c1b4ede1b6a99e32fad16c1a476e0c1 /std/math/log.zig | |
| parent | 799c69910172a7248ab9db366e6e3a6556e7d626 (diff) | |
| download | zig-c9fc8bd802f5ed52c4cc78b93f18fc5dc9b6bb7f.tar.gz zig-c9fc8bd802f5ed52c4cc78b93f18fc5dc9b6bb7f.zip | |
workaround for llvm bug
See #393 for details
Diffstat (limited to 'std/math/log.zig')
| -rw-r--r-- | std/math/log.zig | 42 |
1 files changed, 17 insertions, 25 deletions
diff --git a/std/math/log.zig b/std/math/log.zig index abd480b6a6..90f28ffc52 100644 --- a/std/math/log.zig +++ b/std/math/log.zig @@ -2,7 +2,10 @@ const math = @import("index.zig"); const builtin = @import("builtin"); const assert = @import("../debug.zig").assert; -pub fn log(comptime base: usize, x: var) -> @typeOf(x) { +// TODO issue #393 +pub const log = log_workaround; + +pub fn log_workaround(comptime base: usize, x: var) -> @typeOf(x) { const T = @typeOf(x); switch (@typeId(T)) { builtin.TypeId.Int => { @@ -13,41 +16,30 @@ pub fn log(comptime base: usize, x: var) -> @typeOf(x) { } }, - builtin.TypeId.Float => { - return logf(base, x); - }, - - else => { - @compileError("log expects integer or float, found '" ++ @typeName(T) ++ "'"); - }, - } -} - -fn logf(comptime base: usize, x: var) -> @typeOf(x) { - const T = @typeOf(x); - switch (T) { - f32 => { - switch (base) { + builtin.TypeId.Float => switch (T) { + f32 => switch (base) { 2 => return math.log2(x), 10 => return math.log10(x), else => return f32(math.ln(f64(x)) / math.ln(f64(base))), - } - }, + }, - f64 => { - switch (base) { + f64 => switch (base) { 2 => return math.log2(x), 10 => return math.log10(x), // NOTE: This likely is computed with reduced accuracy. else => return math.ln(x) / math.ln(f64(base)), - } + }, + + else => @compileError("log not implemented for " ++ @typeName(T)), }, - else => @compileError("log not implemented for " ++ @typeName(T)), + else => { + @compileError("log expects integer or float, found '" ++ @typeName(T) ++ "'"); + }, } } -test "log_integer" { +test "math.log integer" { assert(log(2, u8(0x1)) == 0); assert(log(2, u8(0x2)) == 1); assert(log(2, i16(0x72)) == 6); @@ -55,7 +47,7 @@ test "log_integer" { assert(log(2, u64(0x7FF0123456789ABC)) == 62); } -test "log_float" { +test "math.log float" { const epsilon = 0.000001; assert(math.approxEq(f32, log(6, f32(0.23947)), -0.797723, epsilon)); @@ -63,7 +55,7 @@ test "log_float" { assert(math.approxEq(f64, log(123897, f64(12389216414)), 1.981724596, epsilon)); } -test "log_float_special" { +test "math.log float_special" { assert(log(2, f32(0.2301974)) == math.log2(f32(0.2301974))); assert(log(10, f32(0.2301974)) == math.log10(f32(0.2301974))); |
