struct Saturating(T)
- Saturating(T)
- Struct
- Value
- Object
Overview
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.
Included Modules
- Comparable(Saturating(T))
- Comparable(T)
Defined in:
saline/saturating.crConstructors
-
.new(value : T)
Create a new
Saturating(T)
with the given value.
Instance Method Summary
-
#*(other : Number) : Saturating(T)
Returns the result of multiplying
self
and other. -
#*(other : Saturating(T)) : Saturating(T)
Returns the result of multiplying
self
and other. -
#+(other : Number) : Saturating(T)
Returns the result of adding
self
and other. -
#+(other : Saturating(T)) : Saturating(T)
Returns the result of adding
self
and other. -
#-(other : Number) : Saturating(T)
Returns the result of subtracting other from
self
. -
#-(other : Saturating(T)) : Saturating(T)
Returns the result of subtracting other from
self
. - #<=>(other : Saturating(T))
- #<=>(other : Number)
-
#value : T
The
Number
value represented by thisSaturating(T)
Macro Summary
Constructor Detail
Instance Method Detail
Returns the result of multiplying self
and other.
Returns T::MAX
or T::MIN
(as appropriate) in case of overflow.
Returns the result of multiplying self
and other.
Returns T::MAX
or T::MIN
(as appropriate) in case of overflow.
Returns the result of adding self
and other.
Returns T::MAX
or T::MIN
(as appropriate) in case of overflow.
Returns the result of adding self
and other.
Returns T::MAX
or T::MIN
(as appropriate) in case of overflow.
Returns the result of subtracting other from self
.
Returns T::MAX
or T::MIN
(as appropriate) in case of overflow.
Returns the result of subtracting other from self
.
Returns T::MAX
or T::MIN
(as appropriate) in case of overflow.