Documentation

Init.Data.Float.Model.Unpacked.Round

Suppose given a real number x : ℝ with binary expansion x = ± x₀.x₁x₂x₃…. Truncating the expansion at some point gives an approximation of x as a floating-point number y. We can then ask how y is related to x. If y is exactly x, then we say that we are exact. Otherwise we are inexact, and y is slightly smaller than x, but x is also smaller than y + 1ulp. Together with the information that we are inexact, we store the information of where x is located relative to y + ½ulp. This is sufficient information for rounding y according to the various IEEE rounding rules.

  • exact : Accuracy

    The approximation is exactly equal to the infinitely precise value.

  • inexact (relativeToPlusOneHalfUlp : Ordering) : Accuracy

    The approximation is strictly smaller than the infinitely precise value, and the given Ordering describes the result of comparing the infinitely precise value and the approximation plus half a ulp.

Instances For

    Rounds the given mantissa with the given accuracy according to the round-to-nearest strategy, with ties breaking in favor of even mantissas.

    Instances For

      Pairs a mantissa with two residual bits, the 'round bit' and the 'sticky bit', which carry precisely the required information to compute the Accuracy of the mantissa.

      • mantissa : Nat

        The mantissa.

      • roundBit : Bool

        The next bit after the least significant bit of the mantissa.

      • stickyBit : Bool

        The bitwise or of all bits that are even less significant than the round bit.

      Instances For

        Extract the accuracy of the mantissa.

        Instances For

          Extracts the mantissa, rounded according to the data in the residual bits.

          Instances For

            Transforms a mantissa and an accuracy into an extended mantissa with the residual bits initialized to represent the given accuracy.

            Instances For

              Shift the mantissa right by one, propagating information into the residual bits as required.

              Instances For
                def Float.Model.UnpackedFloat.shiftToExponent (mantissa : Nat) (exponent : Int) (accuracy : Accuracy) (targetExponent : Int) :

                Shifts the mantissa and exponent and initial accuracy to the target exponent, returning the new extended mantissa and exponent.

                Important: this function will only drop bits from the mantissa and increase the exponent, not the other way around. The result will only conform to the given specification if the exponent was not too large to begin with. If this may be the case, you should call round instead.

                Instances For
                  def Float.Model.UnpackedFloat.shiftToTargetExponent (spec : Format) (mantissa : Nat) (exponent : Int) (accuracy : Accuracy) :

                  Computes the target exponent for the given mantissa and exponent and shifts the mantissa and exponent and initial accuracy to the target exponent, returning the new extended mantissa and exponent.

                  Important: this function will only drop bits from the mantissa and increase the exponent, not the other way around. The result will only conform to the given specification if the exponent was not too large to begin with. If this may be the case, you should call round instead.

                  Instances For
                    def Float.Model.UnpackedFloat.roundWithAccuracy (spec : Format) (sign : Sign) (mantissa : Nat) (exponent : Int) (accuracy : Accuracy) :

                    Given a finite float represented by a sign, mantissa and exponent, together with an Accuracy datum, round it to conform to the given Format.

                    Important: this function will only drop bits from the mantissa and increase the exponent, not the other way around. The result will only conform to the given specification if the exponent was not too large to begin with. If this may be the case, you should call round instead.

                    Instances For
                      def Float.Model.UnpackedFloat.decreaseExponent (mantissa : Nat) (exponent targetExponent : Int) :

                      If the target exponent is smaller than the given exponent, decreases the exponent of the mantissa to the target exponent; otherwise does nothing.

                      Instances For
                        def Float.Model.UnpackedFloat.round (spec : Format) (sign : Sign) (mantissa : Nat) (exponent : Int) :

                        Given a finite float represented by a sign, mantissa and exponent, round it to conform to the given Format.

                        If necessary, this will both decrease and increase the exponent.

                        Instances For
                          def Float.Model.UnpackedFloat.normalize (spec : Format) (mantissa exponent : Int) (zeroSign : Sign) :

                          Given a finite float represented as a signed mantissa and an exponent, round it to conform to the given Format.

                          If necessary, this will both decrease and increase the exponent.

                          Instances For