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:
In Python, I can use numpy
to define the type but it's much more annoying to write:
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):
5 Replies
@Corgo fn main() raises:
let mult: Float32 = FloatLiteral(0.6) *FloatLiteral(0.91)
let acceleration: Float32 = FloatLiteral(0.16277136) / (mult * mult * mult)
print(acceleration)
1.0000002384185791
yes, the other two print
0.9999998
which is what i'm looking for
FloatLiteral()
casts it to a 64-bit float, whereas i need them to be 32-bit floatsfrom math import mul
from math import div
fn main() raises:
let mult: Float32 = FloatLiteral(0.6) *FloatLiteral(0.91)
let k =mult * mult * mult
let acceleration: Float32 = FloatLiteral(0.16277136) / k
print("accel",acceleration)
let multVector1 = SIMDDType.float32, 1
let multVector2 = SIMDDType.float32, 1
let multResult = mul[DType.float32, 1](multVector1, multVector2)
let squareResult = mul[DType.float32, 1](multResult, multResult)
let cubeResult = mul[DType.float32, 1](multResult, squareResult)
let accelerationNumerator = SIMDDType.float32, 1
let accelerationVector = div[DType.float32, 1](accelerationNumerator, cubeResult)
print(accelerationVector )
@Corgo Could you please try this
0.99999982118606567
@Corgo the accelerationVector print should give you expected value
realized i can just do this