2019-05-01 18:15:57 +12:00
|
|
|
// Ported from musl, which is licensed under the MIT license:
|
|
|
|
|
// https://git.musl-libc.org/cgit/musl/tree/COPYRIGHT
|
|
|
|
|
//
|
|
|
|
|
// https://git.musl-libc.org/cgit/musl/tree/src/math/logf.c
|
|
|
|
|
// https://git.musl-libc.org/cgit/musl/tree/src/math/log.c
|
|
|
|
|
|
2019-03-02 16:46:04 -05:00
|
|
|
const std = @import("../std.zig");
|
2017-12-23 22:08:53 -05:00
|
|
|
const math = std.math;
|
2019-02-08 18:18:47 -05:00
|
|
|
const expect = std.testing.expect;
|
2017-06-16 20:26:10 +12:00
|
|
|
|
2019-05-01 18:15:57 +12:00
|
|
|
/// Returns the logarithm of x for the provided base.
|
2018-01-25 04:10:11 -05: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 01:32:15 -04:00
|
|
|
if (base == 2) {
|
|
|
|
|
return math.log2(x);
|
|
|
|
|
} else if (base == 10) {
|
|
|
|
|
return math.log10(x);
|
2024-08-28 02:35:53 +01:00
|
|
|
} else if ((@typeInfo(T) == .float or @typeInfo(T) == .comptime_float) and base == math.e) {
|
2022-04-26 10:13:55 -07:00
|
|
|
return @log(x);
|
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 01:32:15 -04:00
|
|
|
}
|
|
|
|
|
|
2018-06-17 02:57:07 -04:00
|
|
|
const float_base = math.lossyCast(f64, base);
|
2020-02-24 23:03:30 +02:00
|
|
|
switch (@typeInfo(T)) {
|
2024-08-28 02:35:53 +01:00
|
|
|
.comptime_float => {
|
2022-04-26 10:13:55 -07:00
|
|
|
return @as(comptime_float, @log(@as(f64, x)) / @log(float_base));
|
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 01:32:15 -04:00
|
|
|
},
|
2023-09-14 21:33:56 +02:00
|
|
|
|
2024-08-28 02:35:53 +01:00
|
|
|
.comptime_int => {
|
2024-01-12 17:28:56 +01:00
|
|
|
return @as(comptime_int, math.log_int(comptime_int, base, x));
|
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 01:32:15 -04:00
|
|
|
},
|
2021-04-02 14:52:47 +02:00
|
|
|
|
2024-08-28 02:35:53 +01:00
|
|
|
.int => |IntType| switch (IntType.signedness) {
|
2021-11-24 14:47:33 -07:00
|
|
|
.signed => @compileError("log not implemented for signed integers"),
|
2023-09-14 21:33:56 +02:00
|
|
|
.unsigned => return @as(T, math.log_int(T, base, x)),
|
2017-06-16 20:26:10 +12:00
|
|
|
},
|
|
|
|
|
|
2024-08-28 02:35:53 +01:00
|
|
|
.float => {
|
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 01:32:15 -04:00
|
|
|
switch (T) {
|
2023-06-22 18:46:56 +01:00
|
|
|
f32 => return @as(f32, @floatCast(@log(@as(f64, x)) / @log(float_base))),
|
2022-04-26 10:13:55 -07:00
|
|
|
f64 => return @log(x) / @log(float_base),
|
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 01:32:15 -04:00
|
|
|
else => @compileError("log not implemented for " ++ @typeName(T)),
|
2017-12-22 00:50:30 -05:00
|
|
|
}
|
2017-06-16 20:26:10 +12:00
|
|
|
},
|
|
|
|
|
|
2017-06-19 14:36:33 -04:00
|
|
|
else => {
|
|
|
|
|
@compileError("log expects integer or float, found '" ++ @typeName(T) ++ "'");
|
|
|
|
|
},
|
2017-06-16 20:26:10 +12:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-25 23:31:53 -08:00
|
|
|
test "log integer" {
|
2021-05-04 20:47:26 +03:00
|
|
|
try expect(log(u8, 2, 0x1) == 0);
|
|
|
|
|
try expect(log(u8, 2, 0x2) == 1);
|
|
|
|
|
try expect(log(u16, 2, 0x72) == 6);
|
|
|
|
|
try expect(log(u32, 2, 0xFFFFFF) == 23);
|
|
|
|
|
try expect(log(u64, 2, 0x7FF0123456789ABC) == 62);
|
2017-06-16 20:26:10 +12:00
|
|
|
}
|
|
|
|
|
|
2024-02-25 23:31:53 -08:00
|
|
|
test "log float" {
|
2017-06-16 20:26:10 +12:00
|
|
|
const epsilon = 0.000001;
|
|
|
|
|
|
2021-05-04 20:47:26 +03:00
|
|
|
try expect(math.approxEqAbs(f32, log(f32, 6, 0.23947), -0.797723, epsilon));
|
|
|
|
|
try expect(math.approxEqAbs(f32, log(f32, 89, 0.23947), -0.318432, epsilon));
|
|
|
|
|
try expect(math.approxEqAbs(f64, log(f64, 123897, 12389216414), 1.981724596, epsilon));
|
2017-06-16 20:26:10 +12:00
|
|
|
}
|
|
|
|
|
|
2024-02-25 23:31:53 -08:00
|
|
|
test "log float_special" {
|
2021-05-04 20:47:26 +03:00
|
|
|
try expect(log(f32, 2, 0.2301974) == math.log2(@as(f32, 0.2301974)));
|
|
|
|
|
try expect(log(f32, 10, 0.2301974) == math.log10(@as(f32, 0.2301974)));
|
2017-06-16 20:26:10 +12:00
|
|
|
|
2021-05-04 20:47:26 +03:00
|
|
|
try expect(log(f64, 2, 213.23019799993) == math.log2(@as(f64, 213.23019799993)));
|
|
|
|
|
try expect(log(f64, 10, 213.23019799993) == math.log10(@as(f64, 213.23019799993)));
|
2017-06-16 20:26:10 +12:00
|
|
|
}
|