diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2021-06-01 14:00:54 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-06-01 14:00:54 -0400 |
| commit | f6932472950e7dbb451d4cfef8e5f4a1cc506ac0 (patch) | |
| tree | 986353a4f281ca362f48e9aec31af691d85c8019 /lib/std/fmt.zig | |
| parent | d496400cff8b025dea262a9544e1b20482233089 (diff) | |
| parent | abd1c75c4aa70a83884e0509820dff8a6e51430c (diff) | |
| download | zig-f6932472950e7dbb451d4cfef8e5f4a1cc506ac0.tar.gz zig-f6932472950e7dbb451d4cfef8e5f4a1cc506ac0.zip | |
Merge pull request #8917 from ifreund/fix-float-tokenize2
stage1, stage2: disallow 1.e9 and 0x1.p9 as float literals
Diffstat (limited to 'lib/std/fmt.zig')
| -rw-r--r-- | lib/std/fmt.zig | 15 |
1 files changed, 9 insertions, 6 deletions
diff --git a/lib/std/fmt.zig b/lib/std/fmt.zig index ce7b7d8c4d..9ab655852c 100644 --- a/lib/std/fmt.zig +++ b/lib/std/fmt.zig @@ -1121,9 +1121,12 @@ pub fn formatFloatHexadecimal( try writer.writeAll("0x"); try writer.writeByte(buf[0]); - if (options.precision != @as(usize, 0)) - try writer.writeAll("."); const trimmed = mem.trimRight(u8, buf[1..], "0"); + if (options.precision) |precision| { + if (precision > 0) try writer.writeAll("."); + } else if (trimmed.len > 0) { + try writer.writeAll("."); + } try writer.writeAll(trimmed); // Add trailing zeros if explicitly requested. if (options.precision) |precision| if (precision > 0) { @@ -2048,10 +2051,10 @@ test "float.hexadecimal" { try expectFmt("f64: 0x1.5555555555555p-2", "f64: {x}", .{@as(f64, 1.0 / 3.0)}); try expectFmt("f128: 0x1.5555555555555555555555555555p-2", "f128: {x}", .{@as(f128, 1.0 / 3.0)}); - try expectFmt("f16: 0x1.p-14", "f16: {x}", .{@as(f16, math.f16_min)}); - try expectFmt("f32: 0x1.p-126", "f32: {x}", .{@as(f32, math.f32_min)}); - try expectFmt("f64: 0x1.p-1022", "f64: {x}", .{@as(f64, math.f64_min)}); - try expectFmt("f128: 0x1.p-16382", "f128: {x}", .{@as(f128, math.f128_min)}); + try expectFmt("f16: 0x1p-14", "f16: {x}", .{@as(f16, math.f16_min)}); + try expectFmt("f32: 0x1p-126", "f32: {x}", .{@as(f32, math.f32_min)}); + try expectFmt("f64: 0x1p-1022", "f64: {x}", .{@as(f64, math.f64_min)}); + try expectFmt("f128: 0x1p-16382", "f128: {x}", .{@as(f128, math.f128_min)}); try expectFmt("f16: 0x0.004p-14", "f16: {x}", .{@as(f16, math.f16_true_min)}); try expectFmt("f32: 0x0.000002p-126", "f32: {x}", .{@as(f32, math.f32_true_min)}); |
