Docs

f32: a float of 32 bits

Most boards with a float unit, a Cortex-M4F, an M33 or an ESP32, have one of 32 bits. A float in Zeno has 64, and on those boards every float sum is done in software. f32 is a float of 32 bits, as C's float: about 7 digits, sums done by the board's own float unit, and four bytes in a field.

let gain: f32 = 0.1
var total: f32 = 0.0
for i in range(10) {
    total += gain
}
print(gain)              # 0.1
print(total)             # 1.0000001
print(float(gain))       # 0.10000000149011612

The reasons for each choice are in chapter 41 of decisions.md.

Writing one

A number written where an f32 is wanted becomes the nearest f32, with nothing more to write. There is no 0.1f.

let gain: f32 = 0.1
let readings: list[f32] = [0.5, 1.25, -2.0]

A written number too big for an f32 (about 3.4e38) is an error before the program runs. A value that is a float is made one with f32( ), which rounds it:

let x = 2.0 / 3.0
let y: f32 = f32(x)      # 0.6666667
let n: f32 = f32(7)      # 7.0

f32(x) of a float too big for an f32 is an OverflowError.

Sums

Beside an f32 Gives
an f32 an f32
an int, as in gain * 3 an f32
a written number, as in gain + 1.5 an f32
a float value an error before the program runs

+ - * / are done in 32 bits. A comparison compares the two f32s. An f32 and a float are never mixed by guess, because either guess can be wrong: 64 bits would put the sums back in software, and 32 would throw away digits the float had.

let gain: f32 = 0.5
let offset = 1.5
print(gain + offset)
error: `+` between f32 and float would have to choose between 32 bits and 64. Write `f32( )` around the float to work in 32, or `float( )` around the f32 to work in 64

Where it goes by itself

An f32 goes wherever a float is wanted, a parameter, a variable or a return, since every f32 is exactly a float:

import math

let gain: f32 = 0.1
let wide: float = gain
print(math.sqrt(gain))   # 0.3162277683729184, a float

A list[f32] is not a list[float]: it is shared, and whoever held it as a list[float] could put in a float that is not an f32. Copy it with [float(x) for x in readings].

abs, min, max and sum of f32s give an f32. The functions of math give a float.

Printing

An f32 is printed with the fewest digits that make the same f32 again, so 0.1 prints as 0.1. float(x) shows every digit of the float it is exactly. Formats work as for a float: f"{gain:.3f}" is 0.100.

In a built program

zeno build writes C's float: four bytes in a field, and the board's float instructions with -O2. LLVM keeps the value in a double and rounds after each step, which gives the same answer bit for bit. Every target language, from Python to Rust, does the same rounding and prints the same digits.

Not yet

json.write of an f32, f32("0.5") from text, math in 32 bits, pointer[f32] and a register that holds one. See 41.9 in decisions.md.

This page is docs/f32.md in the language repository.