numbers
python.Number
Section titled “python.Number”data NumberWhat a value is as a number: an int (a bool as 0 or 1), a float, or not a number.
python.Number.of
Section titled “python.Number.of”def Number.of (v : Value) : NumberThe value v as a number.
python.Number.format
Section titled “python.Number.format”def Number.format : FloatFormatbinary64, Python’s float.
python.Number.isInfinite
Section titled “python.Number.isInfinite”def Number.isInfinite (x : Float) : BoolWhether the float x is infinite.
python.Number.toFloat
Section titled “python.Number.toFloat”def Number.toFloat (n : Int) : Option FloatThe int n as a float, rounded; none when it is too large (Python raises OverflowError: “int too large to convert
to float”).
python.Number.floorDivide
Section titled “python.Number.floorDivide”def Number.floorDivide (a : Int) (b : Int) : Inta // b on ints (b not zero): the quotient rounded toward negative infinity.
python.Number.modulo
Section titled “python.Number.modulo”def Number.modulo (a : Int) (b : Int) : Inta % b on ints (b not zero): a - b * (a // b), with the divisor’s sign.
python.Number.isNegative
Section titled “python.Number.isNegative”def Number.isNegative (x : Float) : BoolWhether the float x is negative (NaN counts as positive).
python.Number.floatModulo
Section titled “python.Number.floatModulo”def Number.floatModulo (x : Float) (y : Float) : FloatThe floats’ x % y (CPython’s float_divmod): fmod, moved by the divisor y when the signs differ; a zero
remainder has the divisor’s sign.
python.Number.floatFloorDivide
Section titled “python.Number.floatFloorDivide”def Number.floatFloorDivide (x : Float) (y : Float) : FloatA float’s // (CPython’s float_divmod): (x - fmod) / y, one less when the signs differ, floored, and one more when
the floor is more than half below; a zero quotient has the sign of x / y.
python.Rational
Section titled “python.Rational”record RationalA rational number: its sign, numerator and denominator (not zero).
python.Rational.ofInt
Section titled “python.Rational.ofInt”def Rational.ofInt (n : Int) : RationalThe int n as a rational.
python.Rational.ofFinite
Section titled “python.Rational.ofFinite”def Rational.ofFinite (negative : Bool) (m : Nat) (e : Int) : RationalThe finite float with the mantissa m and the exponent e (m * 2^e), negative or not, as a rational.
python.Rational.sign
Section titled “python.Rational.sign”def Rational.sign (a : Rational) : IntThe sign of a rational: -1, 0 or 1.
python.Rational.compareNat
Section titled “python.Rational.compareNat”def Rational.compareNat (x : Nat) (y : Nat) : IntComparing the naturals x and y: -1, 0 or 1.
python.Rational.compare
Section titled “python.Rational.compare”def Rational.compare (a : Rational) (b : Rational) : IntComparing the rationals a and b: -1, 0 or 1 (the signs first, then the magnitudes cross-multiplied).
python.Number.compareIntFloat
Section titled “python.Number.compareIntFloat”def Number.compareIntFloat (n : Int) (y : Float) : Option IntComparing the int n with the float y exactly: -1, 0 or 1; none when the float is NaN.
python.Number.compareFloats
Section titled “python.Number.compareFloats”def Number.compareFloats (x : Float) (y : Float) : Option IntComparing the floats x and y: -1, 0 or 1; none when either is NaN.
python.Number.compare
Section titled “python.Number.compare”def Number.compare (a : Number) (b : Number) : Option IntComparing the numbers a and b by their exact values: -1, 0 or 1; none when a NaN takes part or one is not a
number.
python.Number.magnitude
Section titled “python.Number.magnitude”def Number.magnitude (n : Int) : NatThe magnitude of the int n.
python.Number.widthFor
Section titled “python.Number.widthFor”def Number.widthFor (a : Int) (b : Int) : NatA two’s complement width that holds both ints a and b with room for the sign.
python.Number.signedAt
Section titled “python.Number.signedAt”def Number.signedAt (width : Nat) (bits : Nat) : IntThe unsigned result bits at the width read as the signed int it is.
python.Number.bitAnd
Section titled “python.Number.bitAnd”def Number.bitAnd (a : Int) (b : Int) : Inta & b on ints (infinite two’s complement).
python.Number.bitOr
Section titled “python.Number.bitOr”def Number.bitOr (a : Int) (b : Int) : Inta | b on ints (infinite two’s complement).
python.Number.bitXor
Section titled “python.Number.bitXor”def Number.bitXor (a : Int) (b : Int) : Inta ^ b on ints (infinite two’s complement).
python.Number.invert
Section titled “python.Number.invert”def Number.invert (a : Int) : Int~a on an int: -a - 1.
python.Number.shiftLeft
Section titled “python.Number.shiftLeft”def Number.shiftLeft (a : Int) (k : Nat) : Inta << k (k not negative).
python.Number.shiftRight
Section titled “python.Number.shiftRight”def Number.shiftRight (a : Int) (k : Nat) : Inta >> k (k not negative): rounded toward negative infinity.
python.Number.truncate
Section titled “python.Number.truncate”def Number.truncate (x : Float) : Option IntA float truncated toward zero as an int (int(x)): none for infinity or NaN (Python raises OverflowError for
infinity and ValueError for NaN).
python.Number.index
Section titled “python.Number.index”def Number.index (v : Value) : Option IntThe int the value v stands for as an index (operator.index): an int, or a bool as 0 or 1.
python.Number.hashModulus
Section titled “python.Number.hashModulus”def Number.hashModulus : NatThe modulus of numeric hashes, the prime 2^61 - 1.
python.Number.hashed
Section titled “python.Number.hashed”def Number.hashed (negative : Bool) (x : Nat) : IntThe hash x with its sign (minus when negative), -1 replaced by -2 (-1 is CPython’s error value).
python.Number.hashInt
Section titled “python.Number.hashInt”def Number.hashInt (n : Int) : Inthash(n) of an int.
python.Number.hashFloat
Section titled “python.Number.hashFloat”def Number.hashFloat (x : Float) : Option Inthash(x) of a float: none for NaN. A finite x = m * 2^e hashes as m times the inverse of 2^-e modulo the prime,
which is 2 to the power (-e mod 61) since 2^61 is 1 there.