Skip to content

std.fmt on comptime_int fails to handle some decimal values #18046

Description

@luca-i

comptimePrint doesn't handle correctly some decimal values (such as 300.0, but other are fine, such as 300.5).

How to replicate the issue:

conststd=@import("std");
pubfnmain() void {
constvalue=30.0;
constvalue_to_str=std.fmt.comptimePrint("{d}", .{value});
std.debug.print("value = {s}\n", .{value_to_str});
}

Expected output:

value = 30.0

Retrieved output:

An error occurred:
/usr/local/bin/lib/std/fmt.zig:1367:47: error: overflow of integer type 'u1' with value '2'
/usr/local/bin/lib/std/fmt.zig:802:27: note: called from here
/usr/local/bin/lib/std/fmt.zig:732:58: note: called from here
/usr/local/bin/lib/std/fmt.zig:487:31: note: called from here
/usr/local/bin/lib/std/fmt.zig:184:23: note: called from here
/usr/local/bin/lib/std/fmt.zig:2008:11: note: called from here
/usr/local/bin/lib/std/fmt.zig:2052:83: note: called from here

Code that works:

conststd=@import("std");
pubfnmain() void {
constvalue=30.1;
constvalue_to_str=std.fmt.comptimePrint("{d}", .{value});
std.debug.print("value = {s}\n", .{value_to_str});
}

Output:

value = 30.1

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behaviorcontributor friendlyThis issue is limited in scope and/or knowledge of Zig internals.standard libraryThis issue involves writing Zig code for the standard library.

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions