Segmentation Fault, Got Object when returning a Float

mojo test.🔥 /mnt/d/Projects/QUk.YW5k.TUw/Cartoonify/test.🔥:18:32: error: cannot implicitly convert 'object' value to 'FloatLiteral' in assignment pi = monte_carlo_simulation(10) ~~~~^~ mojo: error: failed to parse the provided Mojo
7 Replies
꧁✧Ɲ_44✧꧂
꧁✧Ɲ_44✧꧂OP11mo ago
from python import Python

def monte_carlo_simulation(n: Int):
let random = Python.import_module("random")
var count = 0
for i in range(n):
let x = random.random()
let y = random.random()
let sum = x * x + y * y
if sum <= 1:
count += 1
return 4 * (count / n)

fn main() raises:
let py = Python.import_module("builtins")
let n = int(py.input("Enter the number of iterations: "))
var pi = 0.0
pi = monte_carlo_simulation(10)
print(pi)

return None
from python import Python

def monte_carlo_simulation(n: Int):
let random = Python.import_module("random")
var count = 0
for i in range(n):
let x = random.random()
let y = random.random()
let sum = x * x + y * y
if sum <= 1:
count += 1
return 4 * (count / n)

fn main() raises:
let py = Python.import_module("builtins")
let n = int(py.input("Enter the number of iterations: "))
var pi = 0.0
pi = monte_carlo_simulation(10)
print(pi)

return None
This resulted in
mojo test.🔥
/mnt/d/Projects/QUk.YW5k.TUw/Cartoonify/test.🔥:18:32: error: cannot implicitly convert 'object' value to 'FloatLiteral' in assignment
pi = monte_carlo_simulation(10)
~~~~~~~~~~~~~~~~~~~~~~^~~~
mojo: error: failed to parse the provided Mojo
mojo test.🔥
/mnt/d/Projects/QUk.YW5k.TUw/Cartoonify/test.🔥:18:32: error: cannot implicitly convert 'object' value to 'FloatLiteral' in assignment
pi = monte_carlo_simulation(10)
~~~~~~~~~~~~~~~~~~~~~~^~~~
mojo: error: failed to parse the provided Mojo
I am new to mojo, started yesterday so i dont know much
Michael K
Michael K11mo ago
You hav ea type mismatch when assigning pi = monte_carlo_simulation(10). When you initially set pi to 0, mojo will infer the type as FloatLiteral and expect the variable to always hold that type. The type returned from monte_carlo_simulation is an object. Anything returned from a python function is an object I believe. The fix is just to use let pi = monte_carlo_simulation(10) and remove the var pi line. That way it is inferring the correct type for your return value.
꧁✧Ɲ_44✧꧂
꧁✧Ɲ_44✧꧂OP11mo ago
Ok but the segmentation fault?
[1980:1980:20240121,114858.370016:ERROR file_io_posix.cc:144] open /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq: No such file or directory (2)
[1980:1980:20240121,114858.370119:ERROR file_io_posix.cc:144] open /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq: No such file or directory (2)
Please submit a bug report to https://github.com/modularml/mojo/issues and include the crash backtrace along with all the relevant source codes.
Stack dump:
0. Program arguments: mojo test.\360\237\224\245
Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
0 mojo 0x0000556fa6fb7ee7
1 mojo 0x0000556fa6fb5abe
2 mojo 0x0000556fa6fb85bf
3 libpthread.so.0 0x00007f2c8ec10420
[1] 1978 segmentation fault mojo test.🔥
[1980:1980:20240121,114858.370016:ERROR file_io_posix.cc:144] open /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq: No such file or directory (2)
[1980:1980:20240121,114858.370119:ERROR file_io_posix.cc:144] open /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq: No such file or directory (2)
Please submit a bug report to https://github.com/modularml/mojo/issues and include the crash backtrace along with all the relevant source codes.
Stack dump:
0. Program arguments: mojo test.\360\237\224\245
Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
0 mojo 0x0000556fa6fb7ee7
1 mojo 0x0000556fa6fb5abe
2 mojo 0x0000556fa6fb85bf
3 libpthread.so.0 0x00007f2c8ec10420
[1] 1978 segmentation fault mojo test.🔥
Oh wait a new one I used to build it previously Now i just ran it instead
Michael K
Michael K11mo ago
I think the fix I provided is all that is needed for the code you provided. It runs on my machine with output of 3.20 after asking me to input a number of iterations. So if problems persist for you, I would try reinstalling Mojo.
꧁✧Ɲ_44✧꧂
꧁✧Ɲ_44✧꧂OP11mo ago
Oh I am using windows, wsl Maybe thats why?
Michael K
Michael K11mo ago
Maybe. I am on a Macbook. But I think Windows via WSL should work. Are you using WSL 1 or WSL 2? Their is a comment in the docs to prefer 2.
GitHub
mojo/docs/roadmap.md at 700bc916fb6db60a5c1ad565c47c9d2dc49abd02 · ...
The Mojo Programming Language. Contribute to modularml/mojo development by creating an account on GitHub.
꧁✧Ɲ_44✧꧂
꧁✧Ɲ_44✧꧂OP11mo ago
Ws; 2
Want results from more Discord servers?
Add your server