2017-12-24 03:08:53 +00:00
|
|
|
const std = @import("../index.zig");
|
|
|
|
const math = std.math;
|
2017-06-16 09:26:10 +01:00
|
|
|
const builtin = @import("builtin");
|
bit shifting safety
* add u3, u4, u5, u6, u7 and i3, i4, i5, i6, i7
* shift operations shift amount parameter type is
integer with log2 bit width of other param
- This enforces not violating undefined behavior on
shift amount >= bit width with the type system
* clean up math.log, math.ln, math.log2, math.log10
closes #403
2017-08-19 06:32:15 +01:00
|
|
|
const TypeId = builtin.TypeId;
|
2017-12-24 03:08:53 +00:00
|
|
|
const assert = std.debug.assert;
|
2017-06-16 09:26:10 +01:00
|
|
|
|
2018-01-25 09:10:11 +00:00
|
|
|
pub fn log(comptime T: type, base: T, x: T) T {
|
bit shifting safety
* add u3, u4, u5, u6, u7 and i3, i4, i5, i6, i7
* shift operations shift amount parameter type is
integer with log2 bit width of other param
- This enforces not violating undefined behavior on
shift amount >= bit width with the type system
* clean up math.log, math.ln, math.log2, math.log10
closes #403
2017-08-19 06:32:15 +01:00
|
|
|
if (base == 2) {
|
|
|
|
return math.log2(x);
|
|
|
|
} else if (base == 10) {
|
|
|
|
return math.log10(x);
|
|
|
|
} else if ((@typeId(T) == TypeId.Float or @typeId(T) == TypeId.FloatLiteral) and base == math.e) {
|
|
|
|
return math.ln(x);
|
|
|
|
}
|
|
|
|
|
2017-06-16 09:26:10 +01:00
|
|
|
switch (@typeId(T)) {
|
bit shifting safety
* add u3, u4, u5, u6, u7 and i3, i4, i5, i6, i7
* shift operations shift amount parameter type is
integer with log2 bit width of other param
- This enforces not violating undefined behavior on
shift amount >= bit width with the type system
* clean up math.log, math.ln, math.log2, math.log10
closes #403
2017-08-19 06:32:15 +01:00
|
|
|
TypeId.FloatLiteral => {
|
|
|
|
return @typeOf(1.0)(math.ln(f64(x)) / math.ln(f64(base)));
|
|
|
|
},
|
|
|
|
TypeId.IntLiteral => {
|
|
|
|
return @typeOf(1)(math.floor(math.ln(f64(x)) / math.ln(f64(base))));
|
|
|
|
},
|
2017-06-16 09:26:10 +01:00
|
|
|
builtin.TypeId.Int => {
|
bit shifting safety
* add u3, u4, u5, u6, u7 and i3, i4, i5, i6, i7
* shift operations shift amount parameter type is
integer with log2 bit width of other param
- This enforces not violating undefined behavior on
shift amount >= bit width with the type system
* clean up math.log, math.ln, math.log2, math.log10
closes #403
2017-08-19 06:32:15 +01:00
|
|
|
// TODO implement integer log without using float math
|
|
|
|
return T(math.floor(math.ln(f64(x)) / math.ln(f64(base))));
|
2017-06-16 09:26:10 +01:00
|
|
|
},
|
|
|
|
|
bit shifting safety
* add u3, u4, u5, u6, u7 and i3, i4, i5, i6, i7
* shift operations shift amount parameter type is
integer with log2 bit width of other param
- This enforces not violating undefined behavior on
shift amount >= bit width with the type system
* clean up math.log, math.ln, math.log2, math.log10
closes #403
2017-08-19 06:32:15 +01:00
|
|
|
builtin.TypeId.Float => {
|
|
|
|
switch (T) {
|
|
|
|
f32 => return f32(math.ln(f64(x)) / math.ln(f64(base))),
|
|
|
|
f64 => return math.ln(x) / math.ln(f64(base)),
|
|
|
|
else => @compileError("log not implemented for " ++ @typeName(T)),
|
2017-12-22 05:50:30 +00:00
|
|
|
}
|
2017-06-16 09:26:10 +01:00
|
|
|
},
|
|
|
|
|
2017-06-19 19:36:33 +01:00
|
|
|
else => {
|
|
|
|
@compileError("log expects integer or float, found '" ++ @typeName(T) ++ "'");
|
|
|
|
},
|
2017-06-16 09:26:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-19 19:36:33 +01:00
|
|
|
test "math.log integer" {
|
bit shifting safety
* add u3, u4, u5, u6, u7 and i3, i4, i5, i6, i7
* shift operations shift amount parameter type is
integer with log2 bit width of other param
- This enforces not violating undefined behavior on
shift amount >= bit width with the type system
* clean up math.log, math.ln, math.log2, math.log10
closes #403
2017-08-19 06:32:15 +01:00
|
|
|
assert(log(u8, 2, 0x1) == 0);
|
|
|
|
assert(log(u8, 2, 0x2) == 1);
|
|
|
|
assert(log(i16, 2, 0x72) == 6);
|
|
|
|
assert(log(u32, 2, 0xFFFFFF) == 23);
|
|
|
|
assert(log(u64, 2, 0x7FF0123456789ABC) == 62);
|
2017-06-16 09:26:10 +01:00
|
|
|
}
|
|
|
|
|
2017-06-19 19:36:33 +01:00
|
|
|
test "math.log float" {
|
2017-06-16 09:26:10 +01:00
|
|
|
const epsilon = 0.000001;
|
|
|
|
|
bit shifting safety
* add u3, u4, u5, u6, u7 and i3, i4, i5, i6, i7
* shift operations shift amount parameter type is
integer with log2 bit width of other param
- This enforces not violating undefined behavior on
shift amount >= bit width with the type system
* clean up math.log, math.ln, math.log2, math.log10
closes #403
2017-08-19 06:32:15 +01:00
|
|
|
assert(math.approxEq(f32, log(f32, 6, 0.23947), -0.797723, epsilon));
|
|
|
|
assert(math.approxEq(f32, log(f32, 89, 0.23947), -0.318432, epsilon));
|
|
|
|
assert(math.approxEq(f64, log(f64, 123897, 12389216414), 1.981724596, epsilon));
|
2017-06-16 09:26:10 +01:00
|
|
|
}
|
|
|
|
|
2017-06-19 19:36:33 +01:00
|
|
|
test "math.log float_special" {
|
bit shifting safety
* add u3, u4, u5, u6, u7 and i3, i4, i5, i6, i7
* shift operations shift amount parameter type is
integer with log2 bit width of other param
- This enforces not violating undefined behavior on
shift amount >= bit width with the type system
* clean up math.log, math.ln, math.log2, math.log10
closes #403
2017-08-19 06:32:15 +01:00
|
|
|
assert(log(f32, 2, 0.2301974) == math.log2(f32(0.2301974)));
|
|
|
|
assert(log(f32, 10, 0.2301974) == math.log10(f32(0.2301974)));
|
2017-06-16 09:26:10 +01:00
|
|
|
|
bit shifting safety
* add u3, u4, u5, u6, u7 and i3, i4, i5, i6, i7
* shift operations shift amount parameter type is
integer with log2 bit width of other param
- This enforces not violating undefined behavior on
shift amount >= bit width with the type system
* clean up math.log, math.ln, math.log2, math.log10
closes #403
2017-08-19 06:32:15 +01:00
|
|
|
assert(log(f64, 2, 213.23019799993) == math.log2(f64(213.23019799993)));
|
|
|
|
assert(log(f64, 10, 213.23019799993) == math.log10(f64(213.23019799993)));
|
2017-06-16 09:26:10 +01:00
|
|
|
}
|