Bit-level utilities for Float and Float32 #
Classification, sign manipulation and neighbor functions from IEEE 754-2019 §5.3.1, §5.5.1 and §5.7.2, together with unit in the last place and distance in units in the last place.
All functions are defined in terms of toBits and ofBits and check for NaN first. The logical
model of Lean's floats has a single canonical NaN, while compiled code may see any NaN bit
pattern, so this keeps the compiled behavior in line with the model. As a consequence, NaN has
no sign here: signBit nan = false, and copySign x nan is x with a positive sign.
The class of a floating-point number, following IEEE 754-2019 §5.7.2.
- nan : Category
Not a number.
- infinite : Category
Positive or negative infinity.
- zero : Category
Positive or negative zero.
- subnormal : Category
A nonzero number whose exponent field is zero.
- normal : Category
A finite number whose exponent field is not zero.
Instances For
Equations
- Float.instReprCategory.repr Float.Category.nan prec✝ = Repr.addAppParen (Std.Format.nest (if prec✝ ≥ 1024 then 1 else 2) (Std.Format.text "Float.Category.nan")).group prec✝
- Float.instReprCategory.repr Float.Category.infinite prec✝ = Repr.addAppParen (Std.Format.nest (if prec✝ ≥ 1024 then 1 else 2) (Std.Format.text "Float.Category.infinite")).group prec✝
- Float.instReprCategory.repr Float.Category.zero prec✝ = Repr.addAppParen (Std.Format.nest (if prec✝ ≥ 1024 then 1 else 2) (Std.Format.text "Float.Category.zero")).group prec✝
- Float.instReprCategory.repr Float.Category.subnormal prec✝ = Repr.addAppParen (Std.Format.nest (if prec✝ ≥ 1024 then 1 else 2) (Std.Format.text "Float.Category.subnormal")).group prec✝
- Float.instReprCategory.repr Float.Category.normal prec✝ = Repr.addAppParen (Std.Format.nest (if prec✝ ≥ 1024 then 1 else 2) (Std.Format.text "Float.Category.normal")).group prec✝
Instances For
Equations
- Float.instReprCategory = { reprPrec := Float.instReprCategory.repr }
Equations
The class of x, as in IEEE 754-2019 §5.7.2.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Whether x is a normal number: finite, nonzero and not subnormal.
Equations
- x.isNormal = (x.classify == Float.Category.normal)
Instances For
Whether x is a subnormal number.
Equations
Instances For
The least floating-point number that compares greater than x, as in IEEE 754-2019 §5.3.1.
Special cases: nextUp nan = nan, nextUp inf = inf, nextUp (-inf) is the most negative
finite number, and nextUp of either zero is the smallest positive subnormal number.
Equations
- One or more equations did not get rendered due to their size.
Instances For
The unit in the last place of x: the distance from |x| to the next number of larger
magnitude. For the largest finite number, it is the distance to the next number of smaller
magnitude. This agrees with Julia's eps(x).
Special cases: ulp 0 is the smallest positive subnormal number, and ulp x = nan when x is
infinite or NaN.
Equations
- One or more equations did not get rendered due to their size.
Instances For
The position of x on the number line that counts floating-point numbers, with both zeros at
0, positive numbers at their bit pattern and negative numbers at the negation of the bit
pattern of their absolute value. Infinities sit one step past the largest finite numbers.
x must not be NaN; see also toOrdinal? and toOrdinal!.
Equations
Instances For
The position of x on the number line that counts floating-point numbers, as in
toOrdinal. Panics when x is NaN.
Equations
- x.toOrdinal! = if h : x.isNaN = false then x.toOrdinal h else panicWithPosWithDecl "Batteries.Data.Float.Bits" "Float.toOrdinal!" 131 49 "toOrdinal!: NaN argument"
Instances For
The number of floating-point numbers between x and y, counting one of the endpoints: the
distance between them in units in the last place. Both zeros count as the same point and the
infinities are one step beyond the largest finite numbers. Neither argument may be NaN; see
also ulpDistance? and ulpDistance!.
Instances For
The distance between x and y in units in the last place, as in ulpDistance, or
none when either argument is NaN.
Equations
Instances For
The distance between x and y in units in the last place, as in ulpDistance.
Panics when either argument is NaN.
Equations
- x.ulpDistance! y = if h : x.isNaN = false ∧ y.isNaN = false then x.ulpDistance y ⋯ ⋯ else panicWithPosWithDecl "Batteries.Data.Float.Bits" "Float.ulpDistance!" 152 7 "ulpDistance!: NaN argument"
Instances For
The class of x, as in IEEE 754-2019 §5.7.2.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Whether x is a normal number: finite, nonzero and not subnormal.
Equations
- x.isNormal = (x.classify == Float.Category.normal)
Instances For
Whether x is a subnormal number.
Equations
Instances For
The least floating-point number that compares greater than x, as in IEEE 754-2019 §5.3.1.
Special cases: nextUp nan = nan, nextUp inf = inf, nextUp (-inf) is the most negative
finite number, and nextUp of either zero is the smallest positive subnormal number.
Equations
- One or more equations did not get rendered due to their size.
Instances For
The unit in the last place of x: the distance from |x| to the next number of larger
magnitude. For the largest finite number, it is the distance to the next number of smaller
magnitude. This agrees with Julia's eps(x).
Special cases: ulp 0 is the smallest positive subnormal number, and ulp x = nan when x is
infinite or NaN.
Equations
- One or more equations did not get rendered due to their size.
Instances For
The position of x on the number line that counts floating-point numbers, with both zeros at
0, positive numbers at their bit pattern and negative numbers at the negation of the bit
pattern of their absolute value. Infinities sit one step past the largest finite numbers.
x must not be NaN; see also toOrdinal? and toOrdinal!.
Equations
Instances For
The position of x on the number line that counts floating-point numbers, as in
toOrdinal. Panics when x is NaN.
Equations
- x.toOrdinal! = if h : x.isNaN = false then x.toOrdinal h else panicWithPosWithDecl "Batteries.Data.Float.Bits" "Float32.toOrdinal!" 241 49 "toOrdinal!: NaN argument"
Instances For
The number of floating-point numbers between x and y, counting one of the endpoints: the
distance between them in units in the last place. Both zeros count as the same point and the
infinities are one step beyond the largest finite numbers. Neither argument may be NaN; see
also ulpDistance? and ulpDistance!.
Instances For
The distance between x and y in units in the last place, as in ulpDistance, or
none when either argument is NaN.
Equations
Instances For
The distance between x and y in units in the last place, as in ulpDistance.
Panics when either argument is NaN.
Equations
- One or more equations did not get rendered due to their size.