summaryrefslogtreecommitdiff
path: root/Saturating a value when wrapping.md
diff options
context:
space:
mode:
Diffstat (limited to 'Saturating a value when wrapping.md')
-rw-r--r--Saturating a value when wrapping.md12
1 files changed, 12 insertions, 0 deletions
diff --git a/Saturating a value when wrapping.md b/Saturating a value when wrapping.md
new file mode 100644
index 0000000..2cc5bf9
--- /dev/null
+++ b/Saturating a value when wrapping.md
@@ -0,0 +1,12 @@
+[[Integers can overflow if compiled with --release]]
+[[Rust]]
+
+This was a bit confusing to me, but [this forum post](https://users.rust-lang.org/t/saturating-what-does-it-really-do-and-when-is-it-useful/52720/2) sums it up quite well:
+
+Instead of wrapping around or error'ing, the maximum or minimum value of the type is chosen depending on which is closest to the result.
+
+Example with `u8`:
+`0 - 1 = 0`
+`7 - 8 = 0`
+`8 + 1 = 8`
+`1 + 8 = 8` \ No newline at end of file