mirror of
https://github.com/jbranchaud/til
synced 2026-01-03 07:08:01 +00:00
Add When Overflow Is Desired as a clojure til.
This commit is contained in:
@@ -18,6 +18,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
|
|||||||
- [Splitting On Whitespace](clojure/splitting-on-whitespace.md)
|
- [Splitting On Whitespace](clojure/splitting-on-whitespace.md)
|
||||||
- [Swap Two Items in a Vector](clojure/swap-two-items-in-a-vector.md)
|
- [Swap Two Items in a Vector](clojure/swap-two-items-in-a-vector.md)
|
||||||
- [Type of Anything](clojure/type-of-anything.md)
|
- [Type of Anything](clojure/type-of-anything.md)
|
||||||
|
- [When Overflow Is Desired](clojure/when-overflow-is-desired.md)
|
||||||
|
|
||||||
### devops
|
### devops
|
||||||
|
|
||||||
|
|||||||
18
clojure/when-overflow-is-desired.md
Normal file
18
clojure/when-overflow-is-desired.md
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
# When Overflow Is Desired
|
||||||
|
|
||||||
|
If you try to add two `MAX_VALUE` Longs, Clojure is kind enough to warn you.
|
||||||
|
|
||||||
|
```clojure
|
||||||
|
> (+ Long/MAX_VALUE Long/MAX_VALUE)
|
||||||
|
ArithmeticException integer overflow clojure.lang.Numbers.throwIntOverflow (Numbers.java:1424)
|
||||||
|
```
|
||||||
|
|
||||||
|
However, when overflow is desired, you can use the *unchecked* operators
|
||||||
|
(e.g. `unchecked-add`).
|
||||||
|
|
||||||
|
```clojure
|
||||||
|
> (unchecked-add Long/MAX_VALUE Long/MAX_VALUE)
|
||||||
|
-2
|
||||||
|
```
|
||||||
|
|
||||||
|
See also `unchecked-subtract` and `unchecked-multiply`.
|
||||||
Reference in New Issue
Block a user