Skip to content
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

std.math.isInf either should support ComptimeFloat or should have a clearer error message #22107

Open
ssteinbach opened this issue Nov 30, 2024 · 1 comment
Labels
bug Observed behavior contradicts documented or intended behavior

Comments

@ssteinbach
Copy link

ssteinbach commented Nov 30, 2024

Zig Version

0.14.0-dev.2340+182cdf74b

Steps to Reproduce and Observed Behavior

test {
    _ = std.math.isInf(3.0);
}

Produces error:

~/zig/0.14.0-dev.2340+182cdf74b/files/lib/std/math/isinf.zig:8:55: error: access of union field 'float' while field 'comptime_float' is active
    const TBits = std.meta.Int(.unsigned, @typeInfo(T).float.bits);
                                          ~~~~~~~~~~~~^~~~~~
~/zig/0.14.0-dev.2340+182cdf74b/files/lib/std/builtin.zig:556:18: note: union declared here
pub const Type = union(enum) {
                 ^~~~~
/var/tmp/inftest.zig:21:23: note: called from here
    _ = std.math.isInf(3.0);
        ~~~~~~~~~~~~~~^~~~~

std.math.isFinite produces the same error.

This error is present in zig 0.13.0 as well.

Expected Behavior

Given that std.math.isNan supports ComptimeFloat arguments, I expected std.math.isInf / std.math.isFinite to also support them. If there is a reason why it shouldn't support ComptimeFloat, then a more helpful @compileError() might be helpful. Just flagging this in case it was missed! Thanks all!

@ssteinbach ssteinbach added the bug Observed behavior contradicts documented or intended behavior label Nov 30, 2024
@rootbeer
Copy link
Contributor

I think #21198 and #21205 are relevant here. If I'm reading those correctly, Zig won't support inf/nan in comptime floats (any operations that would generate them at compile time would instead generate a compile error). I believe this work is in progress (see #23177 which went in yesterday).

For now, the work-around seems to be a cast: std.math.isInf(@as(f128, 3.0))

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
Projects
None yet
Development

No branches or pull requests

2 participants