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

Construction (BigFloat)

from decimo import BigFloat
from decimo.bigdecimal.bigdecimal import BigDecimal

var from_text = BigFloat("3.14159")                    # rounds, 53 bits
var wider = BigFloat("3.14159", 120)                   # rounds, 120 bits
var from_int = BigFloat(42)                            # exact
var from_double = BigFloat(0.1)                        # exact: a double is one
var from_decimal = BigFloat.from_bigdecimal(BigDecimal("0.1"), 53)

var zero = BigFloat.zero(53, False)                    # a signed zero
var infinity = BigFloat.infinity(53, True)             # -Infinity
var nan = BigFloat.nan(53)

With a precision, and optionally a rounding mode, each constructor is a from_ function under another name: BigFloat(text, p, mode) is from_string, BigFloat(n, p, mode) is from_int, BigFloat(x, p, mode) is from_float64. Without a precision, text and a Float64 get 53 bits. An Int gets 53 bits, or its own number of significant bits when that is more, so unlike from_int(n) it never rounds:

print(BigFloat(2).precision)                    # 53
print(BigFloat((1 << 60) + 1).precision)        # 61
print(BigFloat(1 << 62).precision)              # 53

An integer literal wider than Int and a BigInt are exact too, by the same rule: var x: BigFloat = 123456789012345678901234567890 has 96 bits. With a precision, BigFloat(2**100 + 1, 10) rounds the whole literal, and BigFloat(n, p, mode) for a BigInt is from_bigint. The exact conversion has its own name, from_bigint_exactly(n); from_bigint(n) rounds to 53 bits.

The Int and the Float64 constructors are implicit, so a literal can stand on either side of an operator, and in-place operators work too. Each operator rounds half to even at the wider of the two precisions, as described in the overview. A literal has at least 53 bits, so it lifts a narrower float to 53 bits: with a 24-bit x, x * 2 has 53. Mojo has no reflected comparisons, so in a comparison the literal goes on the right: x < 2 works and 2 > x does not compile.

A Float64 literal is the double it stands for, not the decimal: x * 0.1 multiplies by the double nearest a tenth. For the decimal, write x * BigFloat("0.1", p), or x / 10.

var x = BigFloat(1.5)
print(1 + x)                                    # 2.5
print(3 / x)                                    # 2
print(x == 1.5)                                 # True
x += 1
x *= 2
print(x)                                        # 5

from_float64 is exact in both directions where there is room for it: a Float64 is a binary float of 53 bits, so at 53 bits or more nothing is lost either way and every double round-trips bit for bit. Two things qualify that. Asking for fewer bits rounds, like any other conversion – from_float64(0.1, 24) is not 0.1. And a NaN does not survive: this type keeps one canonical NaN, so a sign or a payload goes in and does not come out.

from_string and from_bigdecimal round, because almost no decimal is a binary float, and they round correctly in whichever mode is asked for.