TorchLean API

FloatLib.Numerics.Core.Value

Values of encoded numerical systems #

An encoded numerical system may contain ordinary values, signed infinities, and exceptional bit patterns. None of this is specific to IEEE floating point: posits have NaR, some low-precision formats reserve individual words, and fixed-point systems may have no exceptional values at all.

NumericalValue records this common semantic shape without prescribing a storage layout. The finite value type is a parameter, so a system may denote real numbers, rational numbers, integers, complex numbers, or another exact mathematical domain.

Why an encoded word has no ordinary numerical value.

  • nan (payload : Option Nat := none) (negative signaling : Bool := false) : ExceptionalValue

    A NaN encoding, optionally carrying its diagnostic payload, together with its sign bit and whether it is a signaling NaN. The payload excludes encoding markers such as an IEEE quiet bit, which source decoders remove before constructing this representation-independent value. Encodings without these distinctions use the defaults: no payload, positive, and quiet.

  • notAReal : ExceptionalValue

    The single not-a-real value used by posit systems.

  • reserved (payload : Option Nat := none) : ExceptionalValue

    A representation-specific reserved word, optionally retaining its encoded payload.

  • undefined : ExceptionalValue

    An operation whose result is intentionally outside the scalar semantics.

Instances For
    @[inline]

    Whether an exceptional observation is a signaling NaN.

    Instances For

      The mathematical meaning of one encoded value.

      The sign on infinity is true for negative infinity. Signed finite zero, when a format distinguishes it, belongs to the finite semantic type: FloatLib's exact Dyadic representation, for example, stores the sign of a zero mantissa.

      Instances For
        @[instance_reducible]
        def FloatLib.Numerics.instDecidableEqNumericalValue.decEq {α✝ : Type u_1} [DecidableEq α✝] (x✝ x✝¹ : NumericalValue α✝) :
        Decidable (x✝ = x✝¹)
        Instances For
          Instances For
            @[instance_reducible]
            instance FloatLib.Numerics.instReprNumericalValue {α✝ : Type u_1} [Repr α✝] :
            @[instance_reducible]

            Text of a value: the scalar's own text, inf or -inf, nan, and NaR, reserved or undefined for the other exceptional values.

            def FloatLib.Numerics.NumericalValue.map {α : Type u} {β : Type v} (f : α → β) :

            Apply a function to an ordinary value while preserving infinities and exceptional values.

            Instances For
              @[simp]
              theorem FloatLib.Numerics.NumericalValue.map_finite {α : Type u} {β : Type v} (f : α → β) (value : α) :
              map f (finite value) = finite (f value)

              Mapping a finite value applies the supplied function to its mathematical payload.

              @[simp]
              theorem FloatLib.Numerics.NumericalValue.map_infinity {α : Type u} {β : Type v} (f : α → β) (negative : Bool) :
              map f (infinity negative) = infinity negative

              Mapping preserves an infinity, including its sign.

              @[simp]
              theorem FloatLib.Numerics.NumericalValue.map_exceptional {α : Type u} {β : Type v} (f : α → β) (value : ExceptionalValue) :
              map f (exceptional value) = exceptional value

              Mapping preserves an exceptional value because it has no ordinary payload to transform.

              Extract the ordinary value, returning none for infinity or an exceptional encoding.

              Instances For

                Whether this semantic value is ordinary and finite.

                Instances For
                  @[simp]
                  theorem FloatLib.Numerics.NumericalValue.finite?_finite {α : Type u} (value : α) :
                  (finite value).finite? = some value

                  Extracting the payload of a finite observation succeeds.

                  @[simp]

                  Infinity has no finite payload.

                  @[simp]

                  An exceptional observation has no finite payload.

                  @[simp]

                  A finite constructor is recognized as finite.

                  @[simp]

                  Infinity is not finite.

                  @[simp]

                  An exceptional observation is not finite.

                  @[simp]

                  Mapping the identity function leaves every numerical value unchanged.

                  @[simp]
                  theorem FloatLib.Numerics.NumericalValue.map_comp {α : Type u} {β : Type v} {γ : Type w} (f : α → β) (g : β → γ) (x : NumericalValue α) :
                  map g (map f x) = map (g ∘ f) x

                  Successive maps fuse into a single map of the composed function.

                  @[simp]
                  theorem FloatLib.Numerics.NumericalValue.finite?_map {α : Type u} {β : Type v} (f : α → β) (x : NumericalValue α) :

                  Extracting a finite payload commutes with mapping its scalar value.