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

Floats and compile time ints #12873

Closed
CarlOlson opened this issue Sep 16, 2022 · 4 comments
Closed

Floats and compile time ints #12873

CarlOlson opened this issue Sep 16, 2022 · 4 comments
Labels
bug Observed behavior contradicts documented or intended behavior

Comments

@CarlOlson
Copy link

Zig Version

0.9.1

Steps to Reproduce

const std = @import("std");

test {
    // Case 1: Pass
    var i: i32 = 1 / 3;
    try std.testing.expectEqual(@as(i32, 0), i);

    // Case 2: Fail
    var f: f32 = 1 / 3;
    try std.testing.expectEqual(@as(f32, 1.0 / 3.0), f);

    // Case 3: Compile Error
    var one: i32 = 1;
    var three: i32 = 3;
    _ = one / three;

   // Case 4: Compile Error
   var f2 = 1.0 / 3;
   _ = f2;
}

Expected Behavior

I expect case 1 to be a compile error.

I expect either a compile error or f == 1.0 / 3.0 for case 2.

The current behavior allows for errors to be added easily and can it be hard to track down the cause. I was getting a chain of NaN.

Actual Behavior

Case 2

Test [0/1] test ""... expected 3.33333343e-01, found 0.0e+00
Test [1/1] test ""... FAIL (TestExpectedEqual)
.../zig-linux-x86_64-0.9.1/lib/std/testing.zig:79:17: 0x215b61 in std.testing.expectEqual (test)
                return error.TestExpectedEqual;
                ^
.../src/test.zig:8:5: 0x21550b in test "" (test)
    try std.testing.expectEqual(@as(f32, 1.0 / 3.0), f);
    ^
0 passed; 0 skipped; 1 failed.

Case 3

./src/test.zig:12:13: error: division with 'i32' and 'i32': signed integers must use @divTrunc, @divFloor, or @divExact
    _ = one / three;

Case 4

./src/test.zig:13:19: error: float value 1.000000 cannot be coerced to type 'comptime_int'
    var f2: f32 = 1.0 / 3;
@CarlOlson CarlOlson added the bug Observed behavior contradicts documented or intended behavior label Sep 16, 2022
@cod1r
Copy link
Contributor

cod1r commented Sep 16, 2022

I think this is working as expected no?

@nektro
Copy link
Contributor

nektro commented Sep 16, 2022

working as intended

@nektro
Copy link
Contributor

nektro commented Sep 16, 2022

1 is comptime_int
1.0 is comptime_float

@Vexu
Copy link
Member

Vexu commented Sep 16, 2022

Working as intended with current semantics but would work as you expected if #7967 is accepted.

@Vexu Vexu closed this as completed Sep 16, 2022
topolarity added a commit to topolarity/zig that referenced this issue Oct 15, 2022
This PR (ziglang#12873) in combination with this particular test exposed
a pre-existing bug (ziglang#13175).

This means that the test for ziglang#13038 has regressed.
topolarity added a commit to topolarity/zig that referenced this issue Oct 15, 2022
This PR (ziglang#12873) in combination with this particular test exposed
a pre-existing bug (ziglang#13175).

This means that the test for ziglang#13038 has regressed.
topolarity added a commit to topolarity/zig that referenced this issue Oct 15, 2022
This PR (ziglang#12873) in combination with this particular test exposed
a pre-existing bug (ziglang#13175).

This means that the test for ziglang#13038 has regressed.
topolarity added a commit to topolarity/zig that referenced this issue Oct 16, 2022
This PR (ziglang#12873) in combination with this particular test exposed
a pre-existing bug (ziglang#13175).

This means that the test for ziglang#13038 has regressed
topolarity added a commit to topolarity/zig that referenced this issue Oct 20, 2022
This PR (ziglang#12873) in combination with this particular test exposed
a pre-existing bug (ziglang#13175).

This means that the test for ziglang#13038 has regressed
topolarity added a commit to topolarity/zig that referenced this issue Oct 21, 2022
This PR (ziglang#12873) in combination with this particular test exposed
a pre-existing bug (ziglang#13175).

This means that the test for ziglang#13038 has regressed
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

4 participants