Float32 against Float64 in Mojo
Narrower floats are faster and smaller, but they carry fewer digits.
def main():
var narrow = Float32(1.0) / Float32(3.0)
var wide = Float64(1.0) / Float64(3.0)
print(narrow)
print(wide)
print(Float64(narrow) == wide)
print(Float32.MAX_FINITE, Float64.MAX_FINITE)
How it works
- The same division runs once in
Float32and once inFloat64. - Printing both shows where the 32-bit result stops being accurate.
- Widening the
Float32back does not recover what was lost.
Keywords and builtins used here
Float32Float64defmainprintvar
The run, in numbers
- Lines
- 7
- Characters to type
- 194
- Tokens
- 54
- Three-star pace
- 75 tpm
At the three-star pace of 75 tokens a minute, this run takes about 43 seconds.
Step 5 of 5 in Numbers & time, step 16 of 22 in Parameters & SIMD.