Part III — BigFloat (BFlt)
Overview (BigFloat)
BigFloat is an arbitrary-precision binary floating-point type, written in
Mojo with nothing to install. Where Decimal keeps digits and a scale,
BigFloat keeps a significand and a power of two, which is what makes it the
right type for scientific work: a square root, an exponential or a sine has
no exact decimal answer, so the question is not whether it rounds but whether
it rounds correctly. Every function here does.
| Property | Value |
|---|---|
| Name | BigFloat |
| Alias | BFlt |
| Value | (-1)^sign * significand * 2^exponent |
| Significand | A BigInt, exactly precision bits when non-zero |
| Exponent | An Int, so from Int.MIN to Int.MAX |
| Precision | A field on the value, in bits, chosen per call |
| Special values | NaN, +Infinity, -Infinity, +0 and -0 |
| Rounding | All seven of decimo’s modes, correctly |
| Dependencies | None |
The precision is a field on the value rather than a global context. Two values of different precisions can be added, and the destination’s precision is an argument to the operation, not a setting somewhere else:
from decimo import BigFloat
from decimo.bigfloat.arithmetics import add
var a = BigFloat.from_string("0.1", 53) # 53 bits
var b = BigFloat.from_string("0.1", 200) # 200 bits
print(a == b) # False: b holds more of a tenth
print(add(a, b, 300).precision) # 300: the caller says
print((a + b).precision) # 200: an operator takes the wider