mirror of
https://github.com/ziglang/zig.git
synced 2025-12-06 14:23:09 +00:00
* unify the logic for exporting math functions from compiler-rt,
with the appropriate suffixes and prefixes.
- add all missing f128 and f80 exports. Functions with missing
implementations call other functions and have TODO comments.
- also add f16 functions
* move math functions from freestanding libc to compiler-rt (#7265)
* enable all the f128 and f80 code in the stage2 compiler and behavior
tests (#11161).
* update std lib to use builtins rather than `std.math`.
35 lines
1.0 KiB
Zig
35 lines
1.0 KiB
Zig
const std = @import("../std.zig");
|
|
const math = std.math;
|
|
const testing = std.testing;
|
|
|
|
/// Returns the natural logarithm of x.
|
|
///
|
|
/// Special Cases:
|
|
/// - ln(+inf) = +inf
|
|
/// - ln(0) = -inf
|
|
/// - ln(x) = nan if x < 0
|
|
/// - ln(nan) = nan
|
|
/// TODO remove this in favor of `@log`.
|
|
pub fn ln(x: anytype) @TypeOf(x) {
|
|
const T = @TypeOf(x);
|
|
switch (@typeInfo(T)) {
|
|
.ComptimeFloat => {
|
|
return @as(comptime_float, @log(x));
|
|
},
|
|
.Float => return @log(x),
|
|
.ComptimeInt => {
|
|
return @as(comptime_int, @floor(@log(@as(f64, x))));
|
|
},
|
|
.Int => |IntType| switch (IntType.signedness) {
|
|
.signed => @compileError("ln not implemented for signed integers"),
|
|
.unsigned => return @as(T, @floor(@log(@as(f64, x)))),
|
|
},
|
|
else => @compileError("ln not implemented for " ++ @typeName(T)),
|
|
}
|
|
}
|
|
|
|
test "math.ln" {
|
|
try testing.expect(ln(@as(f32, 0.2)) == @log(0.2));
|
|
try testing.expect(ln(@as(f64, 0.2)) == @log(0.2));
|
|
}
|