Corgo
Corgo
MModular
Created by Corgo on 9/21/2023 in #questions
Type suffixing?
I was curious if there was a builtin way to define the types of decimal constants. For instance, in Java I can specify the constant as a float by adding an F to the end:
float mult = .6F*.91F;
float acceleration = 0.16277136F / (mult * mult * mult);

System.out.println(acceleration);
float mult = .6F*.91F;
float acceleration = 0.16277136F / (mult * mult * mult);

System.out.println(acceleration);
In Python, I can use numpy to define the type but it's much more annoying to write:
from numpy import float32
mult = float32(.6)*float32(.91)
acceleration = float32(0.16277136) / (mult * mult * mult)

print(acceleration)
from numpy import float32
mult = float32(.6)*float32(.91)
acceleration = float32(0.16277136) / (mult * mult * mult)

print(acceleration)
Since Mojo adds typing, I was curious if there's a way I can do it similar to Java or without having to use numpy. To elaborate a bit, the following code produces a different output than my Java and Python examples (which produce the same):
fn main():
let mult: Float32 = 0.6*0.91
let acceleration: Float32 = 0.16277136 / (mult * mult * mult)
print(acceleration)
fn main():
let mult: Float32 = 0.6*0.91
let acceleration: Float32 = 0.16277136 / (mult * mult * mult)
print(acceleration)
11 replies