Documentation

Init.Data.Float.Float32

structure Float32 :

32-bit floating-point numbers.

Float32 corresponds to the IEEE 754 binary32 format (float in C or f32 in Rust). Floating-point numbers are a finite representation of a subset of the real numbers, extended with extra “sentinel” values that represent undefined and infinite results as well as separate positive and negative zeroes. Arithmetic on floating-point numbers approximates the corresponding operations on the real numbers by rounding the results to numbers that are representable, propagating error and infinite values.

Floating-point numbers include subnormal numbers. Their special values are:

  • NaN, which denotes a class of “not a number” values that result from operations such as dividing zero by zero, and
  • Inf and -Inf, which represent positive and infinities that result from dividing non-zero values by zero.

Like other low-level types, Float32 is special-cased by the Lean compiler to correspond to the C float type. From the point of view of Lean's logic, Float32 is equivalent to Float32.Model (via the functions Float32.toModel and Float32.ofModel), which is itself a subtype of UInt32. Some of the operations on Float32 are defined in terms of their Float32.Model counterparts, while others are opaque to Lean's kernel.

Instances For
    def instDecidableEqFloat32.decEq (x✝ x✝¹ : Float32) :
    Decidable (x✝ = x✝¹)
    Equations
    Instances For
      @[extern lean_float32_add]

      Adds two 32-bit floating-point numbers according to IEEE 754. Typically used via the + operator.

      This function has a logical model in terms of Float32.Model. It is compiled to the C addition operator.

      Equations
      Instances For
        @[extern lean_float32_sub]

        Subtracts 32-bit floating-point numbers according to IEEE 754. Typically used via the - operator.

        This function has a logical model in terms of Float32.Model. It is compiled to the C subtraction operator.

        Equations
        Instances For
          @[extern lean_float32_mul]

          Multiplies 32-bit floating-point numbers according to IEEE 754. Typically used via the * operator.

          This function has a logical model in terms of Float32.Model. It is compiled to the C multiplication operator.

          Equations
          Instances For
            @[extern lean_float32_div]

            Divides 32-bit floating-point numbers according to IEEE 754. Typically used via the / operator.

            In Lean, division by zero typically yields zero. For Float32, it instead yields either Inf, -Inf, or NaN.

            This function has a logical model in terms of Float32.Model. It is compiled to the C division operator.

            Equations
            Instances For
              @[extern lean_float32_negate]

              Negates 32-bit floating-point numbers according to IEEE 754. Typically used via the - prefix operator.

              This function has a logical model in terms of Float32.Model. It is compiled to the C negation operator.

              Equations
              Instances For
                @[extern lean_float32_decLt]

                Strict inequality of floating-point numbers. Typically used via the < operator.

                Equations
                Instances For
                  @[extern lean_float32_decLe]

                  Non-strict inequality of floating-point numbers. Typically used via the operator.

                  Equations
                  Instances For
                    @[extern lean_float32_of_bits]

                    Bit-for-bit conversion from UInt32. Interprets a UInt32 as a Float32, ignoring the numeric value and treating the UInt32's bit pattern as a Float32.

                    Float32s and UInt32s have the same endianness on all supported platforms. IEEE 754 very precisely specifies the bit layout of floats.

                    This function has a logical model in terms of Float32.Model.

                    Equations
                    Instances For
                      @[extern lean_float32_to_bits]

                      Bit-for-bit conversion to UInt32. Interprets a Float32 as a UInt32, ignoring the numeric value and treating the Float32's bit pattern as a UInt32.

                      Float32s and UInt32s have the same endianness on all supported platforms. IEEE 754 very precisely specifies the bit layout of floats.

                      This function is distinct from Float.toUInt32, which attempts to preserve the numeric value rather than reinterpreting the bit pattern.

                      Equations
                      Instances For
                        @[instance_reducible]
                        Equations
                        @[instance_reducible]
                        Equations
                        @[instance_reducible]
                        Equations
                        @[instance_reducible]
                        Equations
                        @[instance_reducible]
                        Equations
                        @[instance_reducible]
                        Equations
                        @[instance_reducible]
                        Equations
                        @[extern lean_float32_beq]
                        def Float32.beq (a b : Float32) :

                        Checks whether two floating-point numbers are equal according to IEEE 754.

                        Floating-point equality does not correspond with propositional equality. In particular, it is not reflexive since NaN != NaN, and it is not a congruence because 0.0 == -0.0, but 1.0 / 0.0 != 1.0 / -0.0.

                        This function does not reduce in the kernel. It is compiled to the C equality operator.

                        Equations
                        Instances For
                          @[instance_reducible]
                          Equations
                          @[instance_reducible, extern lean_float32_decLt]
                          instance Float32.decLt (a b : Float32) :
                          Decidable (a < b)

                          Compares two floating point numbers for strict inequality.

                          This function does not reduce in the kernel. It is compiled to the C inequality operator.

                          Equations
                          @[instance_reducible, extern lean_float32_decLe]
                          instance Float32.decLe (a b : Float32) :

                          Compares two floating point numbers for non-strict inequality.

                          This function does not reduce in the kernel. It is compiled to the C inequality operator.

                          Equations
                          @[extern lean_float32_to_string]

                          Converts a floating-point number to a string.

                          This function does not reduce in the kernel.

                          @[extern lean_float32_to_uint8]

                          Converts a floating-point number to an 8-bit unsigned integer.

                          If the given Float32 is non-negative, truncates the value to a positive integer, rounding down and clamping to the range of UInt8. Returns 0 if the Float32 is negative or NaN, and returns the largest UInt8 value (i.e. UInt8.size - 1) if the float is larger than it.

                          This function has a logical model in terms of Float32.Model.

                          Equations
                          Instances For
                            @[extern lean_float32_to_uint16]

                            Converts a floating-point number to a 16-bit unsigned integer.

                            If the given Float32 is non-negative, truncates the value to a positive integer, rounding down and clamping to the range of UInt16. Returns 0 if the Float32 is negative or NaN, and returns the largest UInt16 value (i.e. UInt16.size - 1) if the float is larger than it.

                            This function has a logical model in terms of Float32.Model.

                            Equations
                            Instances For
                              @[extern lean_float32_to_uint32]

                              Converts a floating-point number to a 32-bit unsigned integer.

                              If the given Float32 is non-negative, truncates the value to a positive integer, rounding down and clamping to the range of UInt32. Returns 0 if the Float32 is negative or NaN, and returns the largest UInt32 value (i.e. UInt32.size - 1) if the float is larger than it.

                              This function has a logical model in terms of Float32.Model.

                              Equations
                              Instances For
                                @[extern lean_float32_to_uint64]

                                Converts a floating-point number to a 64-bit unsigned integer.

                                If the given Float32 is non-negative, truncates the value to a positive integer, rounding down and clamping to the range of UInt64. Returns 0 if the Float32 is negative or NaN, and returns the largest UInt64 value (i.e. UInt64.size - 1) if the float is larger than it.

                                This function has a logical model in terms of Float32.Model.

                                Equations
                                Instances For
                                  @[extern lean_float32_to_usize]

                                  Converts a floating-point number to a word-sized unsigned integer.

                                  If the given Float32 is non-negative, truncates the value to a positive integer, rounding down and clamping to the range of USize. Returns 0 if the Float32 is negative or NaN, and returns the largest USize value (i.e. USize.size - 1) if the float is larger than it.

                                  This function has a logical model in terms of Float32.Model.

                                  Equations
                                  Instances For
                                    @[extern lean_float32_isnan]

                                    Checks whether a floating point number is NaN ("not a number") value.

                                    NaN values result from operations that might otherwise be errors, such as dividing zero by zero.

                                    This function has a logical model in terms of Float32.Model. It is compiled to the C operator isnan.

                                    Equations
                                    Instances For
                                      @[extern lean_float32_isfinite]

                                      Checks whether a floating-point number is finite, that is, whether it is normal, subnormal, or zero, but not infinite or NaN.

                                      This function has a logical model in terms of Float32.Model. It is compiled to the C operator isfinite.

                                      Equations
                                      Instances For
                                        @[extern lean_float32_isinf]

                                        Checks whether a floating-point number is a positive or negative infinite number, but not a finite number or NaN.

                                        This function has a logical model in terms of Float32.Model. It is compiled to the C operator isinf.

                                        Equations
                                        Instances For
                                          @[extern lean_float32_frexp]

                                          Splits the given float x into a significand/exponent pair (s, i) such that x = s * 2^i where s ∈ (-1;-0.5] ∪ [0.5; 1). Returns an undefined value if x is not finite.

                                          This function does not reduce in the kernel. It is implemented in compiled code by the C function frexp.

                                          @[instance_reducible]
                                          Equations
                                          @[extern lean_uint8_to_float32]

                                          Obtains the Float32 whose value is the same as the given UInt8.

                                          Equations
                                          Instances For
                                            @[extern lean_uint16_to_float32]

                                            Obtains the Float32 whose value is the same as the given UInt16.

                                            Equations
                                            Instances For
                                              @[extern lean_uint32_to_float32]

                                              Obtains a Float32 whose value is near the given UInt32.

                                              It will be exactly the value of the given UInt32 if such a Float32 exists. If no such Float32 exists, the returned value will either be the smallest Float32 that is larger than the given value, or the largest Float32 that is smaller than the given value.

                                              This function has a logical model in terms of Float32.Model, but is overridden at runtime with an efficient implementation.

                                              Equations
                                              Instances For
                                                @[extern lean_uint64_to_float32]

                                                Obtains a Float32 whose value is near the given UInt64.

                                                It will be exactly the value of the given UInt64 if such a Float32 exists. If no such Float32 exists, the returned value will either be the smallest Float32 that is larger than the given value, or the largest Float32 that is smaller than the given value.

                                                This function has a logical model in terms of Float32.Model, but is overridden at runtime with an efficient implementation.

                                                Equations
                                                Instances For
                                                  @[extern lean_usize_to_float32]

                                                  Obtains a Float32 whose value is near the given USize.

                                                  It will be exactly the value of the given USize if such a Float32 exists. If no such Float32 exists, the returned value will either be the smallest Float32 that is larger than the given value, or the largest Float32 that is smaller than the given value.

                                                  This function has a logical model in terms of Float32.Model, but is overridden at runtime with an efficient implementation.

                                                  Equations
                                                  Instances For
                                                    @[instance_reducible]
                                                    Equations
                                                    @[instance_reducible]
                                                    Equations
                                                    @[instance_reducible]
                                                    Equations
                                                    @[extern sinf]

                                                    Computes the sine of a floating-point number in radians.

                                                    This function does not reduce in the kernel. It is implemented in compiled code by the C function sinf.

                                                    @[extern cosf]

                                                    Computes the cosine of a floating-point number in radians.

                                                    This function does not reduce in the kernel. It is implemented in compiled code by the C function cosf.

                                                    @[extern tanf]

                                                    Computes the tangent of a floating-point number in radians.

                                                    This function does not reduce in the kernel. It is implemented in compiled code by the C function tanf.

                                                    @[extern asinf]

                                                    Computes the arc sine (inverse sine) of a floating-point number in radians.

                                                    This function does not reduce in the kernel. It is implemented in compiled code by the C function asinf.

                                                    @[extern acosf]

                                                    Computes the arc cosine (inverse cosine) of a floating-point number in radians.

                                                    This function does not reduce in the kernel. It is implemented in compiled code by the C function acosf.

                                                    @[extern atanf]

                                                    Computes the arc tangent (inverse tangent) of a floating-point number in radians.

                                                    This function does not reduce in the kernel. It is implemented in compiled code by the C function atanf.

                                                    @[extern atan2f]

                                                    Computes the arc tangent (inverse tangent) of y / x in radians, in the range π. The signs of the arguments determine the quadrant of the result.

                                                    This function does not reduce in the kernel. It is implemented in compiled code by the C function atan2f.

                                                    @[extern sinhf]

                                                    Computes the hyperbolic sine of a floating-point number.

                                                    This function does not reduce in the kernel. It is implemented in compiled code by the C function sinhf.

                                                    @[extern coshf]

                                                    Computes the hyperbolic cosine of a floating-point number.

                                                    This function does not reduce in the kernel. It is implemented in compiled code by the C function coshf.

                                                    @[extern tanhf]

                                                    Computes the hyperbolic tangent of a floating-point number.

                                                    This function does not reduce in the kernel. It is implemented in compiled code by the C function tanhf.

                                                    @[extern asinhf]

                                                    Computes the hyperbolic arc sine (inverse sine) of a floating-point number.

                                                    This function does not reduce in the kernel. It is implemented in compiled code by the C function asinhf.

                                                    @[extern acoshf]

                                                    Computes the hyperbolic arc cosine (inverse cosine) of a floating-point number.

                                                    This function does not reduce in the kernel. It is implemented in compiled code by the C function acoshf.

                                                    @[extern atanhf]

                                                    Computes the hyperbolic arc tangent (inverse tangent) of a floating-point number.

                                                    This function does not reduce in the kernel. It is implemented in compiled code by the C function atanhf.

                                                    @[extern expf]

                                                    Computes the exponential e^x of a floating-point number.

                                                    This function does not reduce in the kernel. It is implemented in compiled code by the C function expf.

                                                    @[extern exp2f]

                                                    Computes the base-2 exponential 2^x of a floating-point number.

                                                    This function does not reduce in the kernel. It is implemented in compiled code by the C function exp2f.

                                                    @[extern logf]

                                                    Computes the natural logarithm ln x of a floating-point number.

                                                    This function does not reduce in the kernel. It is implemented in compiled code by the C function logf.

                                                    @[extern log2f]

                                                    Computes the base-2 logarithm of a floating-point number.

                                                    This function does not reduce in the kernel. It is implemented in compiled code by the C function log2f.

                                                    @[extern log10f]

                                                    Computes the base-10 logarithm of a floating-point number.

                                                    This function does not reduce in the kernel. It is implemented in compiled code by the C function log10f.

                                                    @[extern powf]

                                                    Raises one floating-point number to the power of another. Typically used via the ^ operator.

                                                    This function does not reduce in the kernel. It is implemented in compiled code by the C function powf.

                                                    @[extern sqrtf]

                                                    Computes the square root of a floating-point number.

                                                    This function has a logical model in terms of Float32.Model. It is implemented in compiled code by the C function sqrtf.

                                                    Equations
                                                    Instances For
                                                      @[extern cbrtf]

                                                      Computes the cube root of a floating-point number.

                                                      This function does not reduce in the kernel. It is implemented in compiled code by the C function cbrtf.

                                                      @[extern ceilf]

                                                      Computes the ceiling of a floating-point number, which is the smallest integer that's no smaller than the given number.

                                                      This function does not reduce in the kernel. It is implemented in compiled code by the C function ceilf.

                                                      Examples:

                                                      @[extern floorf]

                                                      Computes the floor of a floating-point number, which is the largest integer that's no larger than the given number.

                                                      This function does not reduce in the kernel. It is implemented in compiled code by the C function floorf.

                                                      Examples:

                                                      @[extern roundf]

                                                      Rounds to the nearest integer, rounding away from zero at half-way points.

                                                      This function does not reduce in the kernel. It is implemented in compiled code by the C function roundf.

                                                      @[extern fabsf]

                                                      Computes the absolute value of a floating-point number.

                                                      This function has a logical model in terms of Float32.Model. It is implemented in compiled code by the C function fabsf.

                                                      Equations
                                                      Instances For
                                                        @[instance_reducible]
                                                        Equations
                                                        @[instance_reducible]
                                                        Equations
                                                        @[extern lean_float32_scaleb]
                                                        opaque Float32.scaleB (x : Float32) (i : Int) :

                                                        Efficiently computes x * 2^i.

                                                        This function does not reduce in the kernel.

                                                        @[extern lean_float32_to_float]

                                                        Converts a 32-bit floating-point number to a 64-bit floating-point number.

                                                        This function does not reduce in the kernel.

                                                        @[extern lean_float_to_float32]

                                                        Converts a 64-bit floating-point number to a 32-bit floating-point number. This may lose precision.

                                                        This function does not reduce in the kernel.