Documentation

Batteries.Data.Float.Bits

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.

inductive Float.Category :

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
    @[instance_reducible]
    Equations

    The sign bit of a Float.

    Equations
    Instances For

      The exponent field of a Float.

      Equations
      Instances For

        The significand field of a Float.

        Equations
        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
            Instances For

              Whether x is a subnormal number.

              Equations
              Instances For

                Whether the sign bit of x is set, as in IEEE 754-2019 §5.7.2 isSignMinus.

                This is true for -0 and false for NaN, since Lean's floats have a single NaN value.

                Equations
                Instances For

                  The value x with the sign of y, as in IEEE 754-2019 §5.5.1.

                  NaN inputs follow signBit: copySign nan y = nan and copySign x nan has a positive sign.

                  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 greatest floating-point number that compares less than x, as in IEEE 754-2019 §5.3.1.

                      This is -nextUp (-x).

                      Equations
                      Instances For
                        def Float.ulp (x : Float) :

                        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, or none when x is NaN.

                            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
                              Instances For
                                def Float.ulpDistance (x y : Float) (hx : x.isNaN = false) (hy : y.isNaN = false) :

                                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!.

                                Equations
                                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
                                    Instances For
                                      theorem Float.ulpDistance?_eq_some {x y : Float} (hx : x.isNaN = false) (hy : y.isNaN = false) :
                                      x.ulpDistance? y = some (x.ulpDistance y hx hy)
                                      theorem Float.ulpDistance!_eq {x y : Float} (hx : x.isNaN = false) (hy : y.isNaN = false) :
                                      x.ulpDistance! y = x.ulpDistance y hx hy

                                      The sign bit of a Float32.

                                      Equations
                                      Instances For

                                        The exponent field of a Float32.

                                        Equations
                                        Instances For

                                          The significand field of a Float32.

                                          Equations
                                          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
                                              Instances For

                                                Whether x is a subnormal number.

                                                Equations
                                                Instances For

                                                  Whether the sign bit of x is set, as in IEEE 754-2019 §5.7.2 isSignMinus.

                                                  This is true for -0 and false for NaN, since Lean's floats have a single NaN value.

                                                  Equations
                                                  Instances For

                                                    The value x with the sign of y, as in IEEE 754-2019 §5.5.1.

                                                    NaN inputs follow signBit: copySign nan y = nan and copySign x nan has a positive sign.

                                                    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 greatest floating-point number that compares less than x, as in IEEE 754-2019 §5.3.1.

                                                        This is -nextUp (-x).

                                                        Equations
                                                        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, or none when x is NaN.

                                                              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
                                                                Instances For
                                                                  def Float32.ulpDistance (x y : Float32) (hx : x.isNaN = false) (hy : y.isNaN = false) :

                                                                  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!.

                                                                  Equations
                                                                  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.
                                                                      Instances For
                                                                        theorem Float32.ulpDistance?_eq_some {x y : Float32} (hx : x.isNaN = false) (hy : y.isNaN = false) :
                                                                        x.ulpDistance? y = some (x.ulpDistance y hx hy)
                                                                        theorem Float32.ulpDistance!_eq {x y : Float32} (hx : x.isNaN = false) (hy : y.isNaN = false) :
                                                                        x.ulpDistance! y = x.ulpDistance y hx hy