Code:
const zMath = std.math;
pub const Pi: f32 = 3.14159265358979311599796346854;
pub const Deg2Rad: f32 = Pi / 180.0;
/// Returns the cosine of `x` (measured in radians).
pub inline fn Cos(value: anytype) f32 {
const T = @TypeOf(value);
const info = @typeInfo(T);
return switch (info) {
.Float => return zMath.cos(value),
.ComptimeFloat, .Int, .ComptimeInt => return zMath.cos(@as(f32, value)),
else => @compileError("Unsupported type"),
};
}
pub fn main() void {
std.debug.print("Val: {d}\n", .{Cos(0 * Math.Deg2Rad)});
std.debug.print("Val: {d}\n", .{Cos(30 * Math.Deg2Rad)});
std.debug.print("Val: {d}\n", .{Cos(45 * Math.Deg2Rad)});
std.debug.print("Val: {d}\n", .{Cos(60 * Math.Deg2Rad)});
std.debug.print("Val: {d}\n", .{Cos(90 * Math.Deg2Rad)});
}
Console:
Val: 1
Val: 0.8660253882408142
Val: 0.7071067690849304
Val: 0.4999999701976776
Val: -0.00000004371138828673793
cos(90) should be 1. But somehow I'm losing precision on calculation. What am I doing wrong? Can It be Deg2Rad? I guess it is correct on first 6 fractal. Maybe I should round to ceil?