saline
Saturating arithmetic for Crystal
Usage
The Saturating
generic type lets you impose saturating arithmetic on any
Number
type. This means that in any case when the wrapped type would overflow
or underflow, it instead clamps to the relevant boundary (the type's MAX
in
case of overflow or MIN
in case of underflow).
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)
Development
This shard currently uses an initial pure-Crystal approach. While I want to keep that approach around, saturating arithmetic is implemented already in LLVM, which undergirds Crystal. I'd like to have create bindings to these functions eventually, with the ability to use each implementation as desired.
Contributing
- Fork it (https://github.com/kinxer/saline/fork)
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
Contributors
- Benjamin Wade - creator and maintainer