struct Saturating(T)

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

Defined in:

saline/saturating.cr

Constructors

Instance Method Summary

Macro Summary

Constructor Detail

def self.new(value : T) #

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


[View source]

Instance Method Detail

def *(other : Number) : Saturating(T) #

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


[View source]
def *(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.


[View source]
def +(other : Number) : Saturating(T) #

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


[View source]
def +(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.


[View source]
def -(other : Number) : Saturating(T) #

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


[View source]
def -(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.


[View source]
def <=>(other : Saturating(T)) #

[View source]
def <=>(other : Number) #

[View source]
def value : T #

The Number value represented by this Saturating(T)


[View source]

Macro Detail

macro method_missing(call) #

[View source]