Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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.

PropertyValue
NameBigFloat
AliasBFlt
Value(-1)^sign * significand * 2^exponent
SignificandA BigInt, exactly precision bits when non-zero
ExponentAn Int, so from Int.MIN to Int.MAX
PrecisionA field on the value, in bits, chosen per call
Special valuesNaN, +Infinity, -Infinity, +0 and -0
RoundingAll seven of decimo’s modes, correctly
DependenciesNone

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