typestar

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

  1. The same division runs once in Float32 and once in Float64.
  2. Printing both shows where the 32-bit result stops being accurate.
  3. Widening the Float32 back does not recover what was lost.

Keywords and builtins used here

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.

Type this snippet

Step 5 of 5 in Numbers & time, step 16 of 22 in Parameters & SIMD.

← Previous Next →