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

Appendix B — Traits Implemented

All of these come from the Mojo standard library except Numeric, Parsable and Rootable, which Decimo defines in decimo.traits. BigInt, BigDecimal, BigFloat and Decimal128 conform to all three, and Rational to Numeric and Parsable. Mojo checks conformance nominally, which is why the traits live in Decimo rather than in the consumer.

Numeric gathers zero(), one(), -x, +, -, * and /, so a generic routine — a matrix or polynomial library, say — can be written once against T: Numeric and run over any of them.

Parsable requires the static from_string(value), so the same generic routine can also fill itself from text. The two are separate because the capabilities are: the MPFR-backed MPF, which is in the source but outside the public interface, parses but is Movable without being Copyable, so it can never be Numeric. Ask for T: Numeric & Parsable to get both.

Rootable requires sqrt(), the one operation a Cholesky or QR factorisation needs beyond arithmetic. It is separate for the same reason, and here the evidence is sharper: BigUInt has a square root but is unsigned, so it has no __neg__ and can never be Numeric either. Its supertraits are Deinitable and Movable and no more — Copyable is pointedly absent, so that MPF can conform. MPF and BigUInt therefore conform to Rootable as well, six types in all. Ask for T: Numeric & Rootable in a routine that needs both. What the root means stays the implementing type’s business: on an integral type it truncates, so BigInt("10").sqrt() is 3, exactly as / truncates there. So does what a negative value does — the four exact types raise, while BigFloat and MPF return a NaN, as they do for every other function outside their domain.

Hashable gives hash(x), and with it a key in a Dict. BigUInt, BigInt, BigDecimal, BigFloat, Rational and Decimal128 all conform. The rule a hash has to keep is that equal values hash alike, and each of these types holds values that are equal without being written the same way, so each has something to see through. A decimal drops the trailing zeros it was written with, which is why 1.0 and 1.00 reach the same entry. A binary float shifts its significand down to an odd number, which is why a half at fifty-three bits and a half at five hundred reach the same entry. A zero is one key whatever sign it carries. The converse is not promised: two different numbers may land on the same sixty-four bits, as they may under any hash, and the dictionary settles that with ==.

Every one of these types also answers String(x) and repr(x). Neither needs a trait of its own here: String(x) and print(x) come from Writable, and repr(x) from the type’s own __repr__. Mojo 1.1 has no Stringable or Representable to ask for, so generic code that wants to print its argument asks for Writable.

BigInt

TraitWhat it enables
Absableabs(x)
Comparable<, <=, >, >=, ==, !=
CopyableValue-semantic copy
MovableMove semantics
FloatableRaisingFloat64(x)
Hashablehash(x), a key in a dictionary
IntableRaisingInt(x)
NumericGeneric code over Decimo numbers
ParsableT.from_string(text) in generic code
Rootablex.sqrt() in generic code
Writableprint(x), String(x)

BigDecimal

TraitWhat it enables
Absableabs(x)
Comparable<, <=, >, >=, ==, !=
CopyableValue-semantic copy
MovableMove semantics
FloatableRaisingFloat64(x)
Hashablehash(x), a key in a dictionary
IntableRaisingInt(x)
NumericGeneric code over Decimo numbers
ParsableT.from_string(text) in generic code
Rootablex.sqrt() in generic code
Roundableround(x), round(x, ndigits)
Writableprint(x), String(x)

Decimal128

TraitWhat it enables
Absableabs(x)
BoolableBool(x), if x
Comparable<, <=, >, >=, ==, !=
FloatableFloat64(x), which cannot overflow
Hashablehash(x), a key in a dictionary
IntableRaisingInt(x)
NumericGeneric code over Decimo numbers
ParsableT.from_string(text) in generic code
Rootablex.sqrt() in generic code
Roundableround(x), round(x, ndigits)
TrivialRegisterPassablePassed in registers, copied bitwise
Writableprint(x), String(x)

BigFloat

TraitWhat it enables
Absableabs(x)
Comparable<, <=, >, >=, ==, !=
CopyableValue-semantic copy
Hashablehash(x), a key in a dictionary
MovableMove semantics
NumericGeneric code over Decimo numbers
ParsableT.from_string(text) in generic code
Rootablex.sqrt() in generic code
Writableprint(x), String(x)

Parsable asks for a from_string(text) of one argument, and a binary float has to be told how many bits to keep, so there are two: the one the trait needs, which lands at the default precision of fifty-three bits, and from_string(text, precision, rounding_mode) for everything else. Generic code bounded on T: Parsable therefore gets a float at a double’s precision, which is the only precision it could have asked for without saying so.

Comparable is conformed to with the one reservation IEEE 754 asks for: a NaN is unordered, so <, <=, >, >= and == are all False for it and != is True, which breaks the law that exactly one of less, equal and greater holds. compare_total() is there for code that needs a total order.

Numeric asks for zero() and one() without arguments, so those two give values of fifty-three bits; zero(precision, sign) and one(precision) give any other. The operators keep the wider of the two precisions and round half to even, so generic code works at the precision of the values it is handed.

Built from decimo main. This page in the manual.