Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
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

[BUG] 0! == 0 #3360

Open
helehex opened this issue Aug 5, 2024 · 6 comments
Open

[BUG] 0! == 0 #3360

helehex opened this issue Aug 5, 2024 · 6 comments
Labels
bug Something isn't working mojo-repo Tag all issues with this label regression Regression

Comments

@helehex
Copy link
Contributor

helehex commented Aug 5, 2024

Bug description

I've noticed a few weird bugs and discrepancies with for loops.
In this case, I'm using a factorial loop fac(), evaluated at 0, which should give 1, but instead gives 0.
Toggling any one of the comments in the example will fix it.

Steps to reproduce

fn main():
    # @parameter # uncomment this and it works
    for n in range(1):
        # print(n) # uncomment this and it works
        print(fac(n))
        # prints: 0


# @no_inline # uncomment this and it works
fn fac(n: Int) -> Int:
    var r = 1
    var i = n
    while i > 1:
        # This isnt reached, but:
        r *= i # comment this and it works
        i -= 1 # comment this and it works
    return r

System information

Host Information
  ================

  Target Triple: x86_64-unknown-linux
  CPU: skylake
  CPU Features: adx, aes, avx, avx2, bmi, bmi2, clflushopt, cmov, crc32, cx16, cx8, f16c, fma, fsgsbase, fxsr, invpcid, lzcnt, mmx, movbe, pclmul, popcnt, prfchw, rdrnd, rdseed, sahf, sgx, sse, sse2, sse3, sse4.1, sse4.2, ssse3, x87, xsave, xsavec, xsaveopt, xsaves

mojo 2024.8.405 (27e8e5b4)

modular 0.8.0 (39a426b5)
@helehex helehex added bug Something isn't working mojo-repo Tag all issues with this label labels Aug 5, 2024
@helehex helehex changed the title [BUG] Loops causing issues [BUG] 0! == 0 Aug 5, 2024
@soraros
Copy link
Contributor

soraros commented Aug 5, 2024

There are two other curious repros.

fn main():
    var r = 0

    @parameter
    for _ in range(10):
        r = 0

    print(r)  # prints 140726121024432

But the following, which should be semantically equivalent, works just fine.

fn main():
    var r = 0

    @parameter
    for i in range(10):
        @parameter
        if i == i:
            r = 0

    print(r)  # prints 0

@ematejska ematejska added the regression Regression label Aug 6, 2024 — with Linear
@jjvraw
Copy link
Contributor

jjvraw commented Aug 7, 2024

Is the issue exclusive to main? An empty body also compiles and runs:

fn main():
    for i in range(10):

Can you reproduce those examples in other contexts outside of main?

@soraros
Copy link
Contributor

soraros commented Aug 7, 2024

@jjvraw I think that's a separate issue #3280.

@jjvraw
Copy link
Contributor

jjvraw commented Aug 7, 2024

Ah, I agree that the empty body of the for is then more likely related to that issue.

@jjvraw
Copy link
Contributor

jjvraw commented Aug 8, 2024

while i > 1: -> while i >= 1: also returns correctly

@helehex
Copy link
Contributor Author

helehex commented Oct 18, 2024

Original reproduction still gives 0.
Sora's addendum seems to be fixed though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working mojo-repo Tag all issues with this label regression Regression
Projects
None yet
Development

No branches or pull requests

4 participants