Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
luca-i opened this issue Nov 19, 2023 · 4 comments · Fixed by #19413
Closed

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

luca-i opened this issue Nov 19, 2023 · 4 comments · Fixed by #19413
Labels
bug Observed behavior contradicts documented or intended behavior contributor friendly This issue is limited in scope and/or knowledge of Zig internals. standard library This issue involves writing Zig code for the standard library.
Milestone

Comments

@luca-i
Copy link
Contributor

luca-i commented Nov 19, 2023

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:

const std = @import("std");


pub fn main() void {
    const value = 30.0;
    const value_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:

const std = @import("std");


pub fn main() void {
    const value = 30.1;
    const value_to_str = std.fmt.comptimePrint("{d}", .{value});
    std.debug.print("value = {s}\n", .{value_to_str});
}

Output:

value = 30.1
@Vexu Vexu added bug Observed behavior contradicts documented or intended behavior contributor friendly This issue is limited in scope and/or knowledge of Zig internals. standard library This issue involves writing Zig code for the standard library. labels Nov 20, 2023
@Vexu Vexu added this to the 0.14.0 milestone Nov 20, 2023
@mlugg mlugg changed the title comptimePrint fails to handle some decimal values std.fmt on comptime_int fails to handle some decimal values Nov 20, 2023
@luca-i
Copy link
Contributor Author

luca-i commented Nov 20, 2023

I think the offending line could be 1366 in lib.std.fmt.zig

    var i = num_digits_whole_no_pad;

I'm not an expert of Zig by no means, but it seems to me that i takes size 1 bit (u1).
Could somebody confirm this?

Changing the line to

    var i: u8 = num_digits_whole_no_pad;

compiles.

If I'm right, could somebody spend some time to mentor me (I'm absolutely a newbie) how to prove my solution has no side effects / regression and how to submit a pull request?

Thank you, Luca-

@Vexu
Copy link
Member

Vexu commented Nov 20, 2023

That's correct but it needs to be a usize to also work for runtime known values.

how to prove my solution has no side effects / regression

Add a new test to fmt.zig using expectFmt and as long as the new test and all old ones pass you're good.

how to submit a pull request?

That depends on your setup, GitHub probably has tutorials for all the common ones.

@Vexu Vexu closed this as completed Nov 24, 2023
@Vexu Vexu modified the milestones: 0.14.0, 0.12.0 Nov 24, 2023
@karlseguin
Copy link
Contributor

I believe this has regressed (or maybe it was never fully fixed). Using the latest 0.12.0-dev.3428+d8bb139da the original code works, but changing value to 0.05 gives an overflow:

const std = @import("std");
pub fn main() void {
    const value = 0.05;
    const value_to_str = std.fmt.comptimePrint("{d}", .{value});
    std.debug.print("value = {s}\n", .{value_to_str});
}

@Vexu
Copy link
Member

Vexu commented Mar 23, 2024

Probably regressed with #19229

@Vexu Vexu reopened this Mar 23, 2024
tiehuis added a commit to tiehuis/zig that referenced this issue Mar 23, 2024
tiehuis added a commit to tiehuis/zig that referenced this issue Mar 23, 2024
tiehuis added a commit to tiehuis/zig that referenced this issue Mar 23, 2024
@Vexu Vexu closed this as completed in 091aa54 Mar 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior contributor friendly This issue is limited in scope and/or knowledge of Zig internals. standard library This issue involves writing Zig code for the standard library.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants