Wide and narrow floats
float16, float128 and float256, IEEE 754 formats implemented in software.
Three more floating-point types, next to float (64 bits) and float32.
| Type | Bits | Exponent bits | Significand bits | About |
|---|---|---|---|---|
float16 | 16 | 5 | 11 | 3 digits, largest value 65504 |
float128 | 128 | 15 | 113 | 34 digits, largest value about 1.2e4932 |
float256 | 256 | 19 | 237 | 71 digits |
They are the standard IEEE 754 binary formats: round to nearest even, subnormals, infinities, NaN and signed zero. They are written in Pelago itself, with no hardware support and no C library, so they work on every target the compiler builds for.
- Literals are rounded once, correctly, when the program is compiled.
1e4000is a finefloat128. - Arithmetic is
+ - * / %, negation and the comparisons. Floats do not stop the program on overflow: you getinf,nanor-0as IEEE says. (Integer overflow still stops the program.) A comparison withnanis false, except!=. - Conversion with
towidens when nothing is lost: any integer type to any of these floats (rounded when it must be), and a float to a wider float.x.toFloat()narrows one of these floats to a plainfloat. Narrowing to a smaller soft float is not written yet. - Printing gives the shortest text that reads back as the same value, so
0.1prints0.1.
fn main() {
const a: float128 = 1.0
const b: float128 = 3.0
print(a / b, a + b, a - b, a * b, b % 2.0, -a)
var c: float128 = 0.1
c += 0.2
print(c, c == 0.3, c > 0.3)
const d: float256 = 1.0
print(d / 3.0, (d / 3.0) * 3.0)
const h: float16 = 0.1
print(h, h + h, 65504.0 to float16, h * 1000.0)
print(5 to float128, (7 to int128) to float256, (a / b).toFloat() == 1.0 / 3.0)
const big: float128 = 1e4000
print(big, big * big, -big * big, big * big - big * big)
print(a / (a - a), (a - a) / (a - a))
}0.3333333333333333333333333333333333 4 -2 3 1 -1
0.30000000000000000000000000000000004 false true
0.333333333333333333333333333333333333333333333333333333333333333333333333 1
0.1 0.2 65500 100
5 7 true
1e+4000 inf -inf nan
inf nanThe 0.1 + 0.2 line shows the usual binary rounding: at 128 bits it is still not exactly 0.3, just closer.
Parsing, patterns and JSON
str.parseFloat16(), parseFloat128() and parseFloat256() read decimal text exactly (correctly rounded) and return a result. switch patterns work on these types.
fn name(x: float128) -> string {
switch x {
case 0.0: return "zero"
case 1.5: return "one and a half"
default: return "other"
}
}
fn main() {
print(name(0.0))
print(name(1.5))
print(name(2.0))
print("2.5".parseFloat16())
print("1e5000".parseFloat256())
print("x".parseFloat128())
}zero
one and a half
other
ok(2.5)
ok(1e+5000)
err(not a number)With @derive(toJson) they are written as JSON text, like the wide integers: the shortest text that reads back to the same value, with "inf", "-inf" and "nan" for the special values. fromJson reads that text, and also a plain JSON number. The example on the wide-integers page includes a float128 field.
Functions
Both families have methods with the same names as the ones on float and int.
- Soft floats:
sqrt(correctly rounded),floor,ceil,trunc,round(halves away from zero, likefloat),abs,minandmax. - Wide integers:
abs(signed types),min,maxandpow. Like every other operation,powstops the program on overflow.
fn main() {
const x: float128 = 2.0
print(x.sqrt())
const y: float128 = -2.5
print(y.floor(), y.ceil(), y.trunc(), y.round(), y.abs())
print(x.min(y), x.max(y))
const h: float16 = 2.0
print(h.sqrt())
const a: int128 = -7
const b: uint256 = 3
print(a.abs(), a.min(5), a.max(5))
print(b.pow(100))
print(b.pow(2))
}1.414213562373095048801688724209698
-3 -2 -2 -3 2.5
-2.5 2
1.414
7 -7 5
515377520732011331036461129765621272702107522001
9What is not there yet
- More maths.
powwith a float exponent,exp,log, the trigonometric functions and a fused multiply-add are not built. - Speed. Everything is plain loops, correct first.
%with a huge gap between the exponents is slow. The same speed list as for wide integers applies.
The routines were checked against exact rational arithmetic: 750 random cases for float128 (add, subtract, multiply, divide and remainder, bit for bit), 400 for float16 and 12 for float256, plus literals at all three formats.