# `Toleranced`

`Toleranced` is a useful tool for managing values with some tolerance.

## Data Types

``````public defstruct Toleranced :
typ:  Double
tol+: Double
tol-: Double
``````

`Toleranced` is a generic value with upper and lower bound tolerances that may be off-center (upper tolerance != lower tolerance). It represents the inequality `(typ - tol-) <= T <= (typ + tol+)`.

• `typ`: the typical value
• `tol+`: the upper tolerance
• `tol-`: the lower tolerance

## Functions

``````public defn min-typ-max (min:Double, typ:Double, max:Double) -> Toleranced
``````

Creates a `Toleranced` value from minimum/typical/maximum values.

``````public defn min-max (min:Double, max:Double) -> Toleranced
``````

Creates a `Toleranced` value from minimum/maximum values with typ as the midpoint.

``````public defn tol% (typ:Double, tol+:Double, tol-:Double) -> Toleranced
``````

Creates a `Toleranced` value using percentages (0.0 <= tol+/tol- <= 100.0) for +/- tolerance.

``````public defn tol% (typ:Double, tol:Double) -> Toleranced
``````

Creates a symmetric `Toleranced` value using percentages (0.0 <= tol <= 100.0) for +/- tolerance.

``````public defn tol (typ:Double, tol+:Double, tol-:Double) -> Toleranced
``````

Creates a `Toleranced` value using relative values for +/- tolerance.

``````public defn tol (typ:Double, tol:Double) -> Toleranced
``````

Creates a `Toleranced` value using the symmetric range for +/- tolerance.

``````public defn tol+% (t:Toleranced) -> Double
``````

Returns the upper tolerance, as a percentage.

``````public defn tol-% (t:Toleranced) -> Double
``````

Returns the lower tolerance, as a percentage.

``````public defn max-value (t:Toleranced) -> Double
``````

Returns the maximum value of the tolerance.

``````public defn typ-value (t:Toleranced) -> Double
``````

Returns the typ value of the tolerance (not necessarily the center value).

``````public defn min-value (t:Toleranced) -> Double
``````

Returns the minimum value of the tolerance.

``````public defn center-value (t:Toleranced) -> Double
``````

Returns the center value of the tolerance, which may not be the typical value.

``````public defn in-range? (t:Toleranced, value:Double) -> True|False
``````

Checks if a value is within the tolerance's range.

``````public defn in-range? (t:Toleranced, value:Toleranced) -> True|False
``````

Checks if a value Toleranced min/max is within the first argument tolerance's min/max.

``````public defn tolerance-range (t:Toleranced) -> Double
``````

Returns the difference between maximum and minimum values of the tolerance.

``````public defn minus (a:Toleranced, b:Toleranced) -> Toleranced
public defn minus (a:Double, b:Toleranced) -> Toleranced
``````

Subtraction of Toleranced values. Arithmetic with Toleranced values are useful for the worst case analysis, but be aware the bounds can grow quickly so it's important to use realistic ranges.

``````public defn plus (a:Toleranced, b:Toleranced) -> Toleranced
public defn plus (a:Double, b:Toleranced) -> Toleranced
``````

Addition of Toleranced values. See note under toleranced subtraction.

``````public defn divide (a:Toleranced, b:Toleranced) -> Toleranced
public defn divide (a:Double, b:Toleranced) -> Toleranced
``````

Division of Toleranced values. See note under toleranced subtraction.

``````public defn times (a:Toleranced, b:Toleranced) -> Toleranced
public defn times (a:Double, b:Toleranced) -> Toleranced
``````

Multiplication of Toleranced values. See note under toleranced subtraction.

## Example usage

``````; Create a range between -0.3 and 5.0
val range-1 = min-max(-0.3, 5.0)

; Create a range within 20% of 500.0
val range-2 = tol%(500.0, 20.0)

; Check if range-2 includes range-1
in-range?(range-2, range-1)

; Get the minimum value of range-1
min-value(range-1)
``````