modularml / mojo

The Mojo Programming Language
https://docs.modular.com/mojo/manual/
Other
23.26k stars 2.6k forks source link

[BUG] `0/0 != 1` returns `False` #3321

Open helehex opened 3 months ago

helehex commented 3 months ago

Bug description

Maybe I'm mistaken, but shouldn't nan != x always return True? When using the math.nan[]() function to provide the nan, it returns True. This means that nan behaves differently right now, depending on it's payload bits.

Steps to reproduce

fn main():
    print(0/0 != 1)

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.7.2705 (4b7a0ee2)

modular 0.8.0 (39a426b5)
helehex commented 3 months ago

BTW: I found this because I was noticing false positives in my unit tests. So be careful I guess.

luanon404 commented 3 months ago

even use != or == the result still False

soraros commented 3 months ago

0 / 0 doesn't run at compile time at all:

fn main():
  alias a = 0 / 0
  print(a)

Error message:

/__w/modular/modular/open-source/mojo/stdlib/stdlib/builtin/_startup.mojo:113:4: error: call expansion failed
/__w/modular/modular/open-source/mojo/stdlib/stdlib/builtin/_startup.mojo:44:4: note: function instantiation failed
<unknown>:0: note: failed to compile-time evaluate function call
/__w/modular/modular/open-source/mojo/stdlib/stdlib/builtin/int.mojo:552:8: note: failed to interpret function @stdlib::builtin::int::Int::__truediv__(::Int,::Int)
/__w/modular/modular/open-source/mojo/stdlib/stdlib/builtin/int.mojo:561:30: note: call expansion failed
/__w/modular/modular/open-source/mojo/stdlib/stdlib/builtin/simd.mojo:563:35: note: failed to fold operation pop.div(#pop<simd "0"> : !pop.scalar<f64>, #pop<simd "0"> : !pop.scalar<f64>)
/home/vscode/.modular/pkg/packages.modular.com_nightly_mojo/bin/mojo: error: failed to run the pass manager

Maybe we should just implement a __truediv__ -> FloatLiteral on IntLiteral?