authorgravatar for datamanrb@gmail.comdata-man <datamanrb@gmail.com> 2020-05-25 11:05:08+05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-05-25 12:48:59-04:00
log0ecdbdb3cb3d9558b5d2dbd928ead124d98c74ca
tree103b859d0c732c79bb8d675cc37bbe4a93d86825
parent0fd77c2de3b0355a6e66bf7919cd25612c73654f

Support comptime floats in std.fmt


1 files changed, 14 insertions(+), 2 deletions(-)

lib/std/fmt.zig+14-2
......@@ -330,7 +330,7 @@ pub fn formatType(
330330 }
331331
332332 switch (@typeInfo(T)) {
333 .ComptimeInt, .Int, .Float => {
333 .ComptimeInt, .Int, .ComptimeFloat, .Float => {
334334 return formatValue(value, fmt, options, out_stream);
335335 },
336336 .Void => {
......@@ -493,7 +493,7 @@ fn formatValue(
493493
494494 const T = @TypeOf(value);
495495 switch (@typeInfo(T)) {
496 .Float => return formatFloatValue(value, fmt, options, out_stream),
496 .Float, .ComptimeFloat => return formatFloatValue(value, fmt, options, out_stream),
497497 .Int, .ComptimeInt => return formatIntValue(value, fmt, options, out_stream),
498498 .Bool => return formatBuf(if (value) "true" else "false", options, out_stream),
499499 else => comptime unreachable,
......@@ -1594,6 +1594,18 @@ test "formatIntValue with comptime_int" {
15941594 std.testing.expect(mem.eql(u8, fbs.getWritten(), "123456789123456789"));
15951595}
15961596
1597test "formatFloatValue with comptime_float" {
1598 const value: comptime_float = 1.0;
1599
1600 var buf: [20]u8 = undefined;
1601 var fbs = std.io.fixedBufferStream(&buf);
1602 try formatFloatValue(value, "", FormatOptions{}, fbs.outStream());
1603 std.testing.expect(mem.eql(u8, fbs.getWritten(), "1.0e+00"));
1604
1605 try testFmt("1.0e+00", "{}", .{value});
1606 try testFmt("1.0e+00", "{}", .{1.0});
1607}
1608
15971609test "formatType max_depth" {
15981610 const Vec2 = struct {
15991611 const SelfType = @This();