modularml / mojo

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

[BUG] numpy type problem #3146

Open hcye2022 opened 2 months ago

hcye2022 commented 2 months ago

Bug description

This really surprises me --- the example below shows using "2 np.pi" and "np.pi 2" yield different results. Changing the multiplier from 2 to 2.0 and the problem goes away.

Steps to reproduce

from python import Python
fn main() raises:
    var np = Python.import_module("numpy")

    var x = np.linspace(0, 2 * np.pi, 10)
    var y = np.linspace(0, np.pi * 2, 10)

    print(x)
    print(y)

[ 0. -0.22222222 -0.44444444 -0.66666667 -0.88888889 -1.11111111 -1.33333333 -1.55555556 -1.77777778 -2. ] [0. 0.6981317 1.3962634 2.0943951 2.7925268 3.4906585 4.1887902 4.88692191 5.58505361 6.28318531]

System information

Versions:
- Mojo: 24.4.0 (on WSL2 / Ubuntu 22.04.4 LTS)
- Python: 3.10.12
- Numpy: 1.26.4
DWSimmons commented 2 months ago

1> from python import Python

  1. var np = Python.import_module("numpy")
  2. print(2 * np.pi)
  3. print(np.pi * 2)
  4. print(2.0 * np.pi)
  5. -2 6.283185307179586 6.283185307179586

mojo REPL 24.4.0 / Ubuntu 22.04 / Numpy 2.0.0

I get that the int float vs float int is causing an issue/bug. What really makes me scratch my head is why np.pi gets converted to negative 1???