struct

Saturating(T)

Inherits Comparable / Comparable / Struct / Value / Object

A Saturating Number clamps to its maximum or minimum values instead of overflowing.

This wrapper type changes overflow behavior to saturate at the contained Number type's minimum and maximum. The code snippet below demonstrates this concept with an Int32.

n = Saturating(Int32).new(Int32::MAX)
n += 1 # => 2147483647 (Int32::MAX)
n = Saturating(Int32).new(Int32::MIN)
n -= 1 # => -2147483648 (Int32::MIN)

Note: This class will only work with Number types. In order to avoid unexpected behavior, T is checked at compile-time to determine whether it is a Number type.

Constructors

new(value : T)

Create a new Saturating(T) with the given value.

Source

Instance methods

*(other : Number) : Saturating(T)

Returns the result of multiplying self and other. Returns T::MAX or T::MIN (as appropriate) in case of overflow.

Source
*(other : Saturating(T)) : Saturating(T)

Returns the result of multiplying self and other. Returns T::MAX or T::MIN (as appropriate) in case of overflow.

Source
+(other : Number) : Saturating(T)

Returns the result of adding self and other. Returns T::MAX or T::MIN (as appropriate) in case of overflow.

Source
+(other : Saturating(T)) : Saturating(T)

Returns the result of adding self and other. Returns T::MAX or T::MIN (as appropriate) in case of overflow.

Source
-(other : Number) : Saturating(T)

Returns the result of subtracting other from self. Returns T::MAX or T::MIN (as appropriate) in case of overflow.

Source
-(other : Saturating(T)) : Saturating(T)

Returns the result of subtracting other from self. Returns T::MAX or T::MIN (as appropriate) in case of overflow.

Source
<=>(other : Saturating(T))
Source
<=>(other : Number)
Source
value

The Number value represented by this Saturating(T)

Source

Macros

method_missing(call)
Source