I doubt that I would ever need a float this large, but I would still like to know why my parse function works for all float types except f128.
const std = @import("std");
const meta = std.meta;
const ArrayList = std.ArrayList;
const TypeInfo = std.builtin.TypeInfo;
const print = std.debug.print;
const ParseError = error{ParseError} || anyerror;
pub fn parse(comptime T: type, buffer: []const u8) ParseError!T {
const typeInfo = @typeInfo(T);
const typeId = std.builtin.TypeId;
_ = typeId;
return switch (typeInfo) {
.Int => std.fmt.parseInt(T, buffer, 10),
.Float => std.fmt.parseFloat(T, buffer),
.Enum => |e| {
inline for (e.fields) |field| {
if (std.mem.eql(u8, field.name, buffer)) {
return @as(T, @enumFromInt(field.value));
}
}
return ParseError.ParseError;
},
.Bool => std.mem.eql(u8, buffer, "true"),
else => {
@compileError("Tried to parse an unsupported type: " ++ @typeName(T));
},
};
}
pub fn main() !void {
print("Type info of f128: {any}\n", .{@typeInfo(f128)});
var parseTest = try parse(f128, "100.0");
print("parseTest: {d} the type is {any}\n", .{ parseTest, @TypeOf(parseTest) });
}
This is the output:
error: expected type 'f64', found 'f128'
var x = @as(f64, value);
^~~~~
referenced by:
formatFloatValue__anon_8701: /home/clinton/zig-linux-x86_64-0.12.0-dev.80+014d88ef6/lib/std/fmt.zig:802:27
formatValue__anon_8700: /home/clinton/zig-linux-x86_64-0.12.0-dev.80+014d88ef6/lib/std/fmt.zig:732:58
remaining reference traces hidden; use '-freference-trace' to see all reference traces
Why is it expecting a f64 in this case instead of just reading the buffer as f128?
Any help or clarification would be appreciated.