summaryrefslogtreecommitdiff
path: root/Saturating a value when wrapping.md
diff options
context:
space:
mode:
authorJasper Ras <jras@hostnet.nl>2025-08-03 22:29:47 +0200
committerJasper Ras <jras@hostnet.nl>2025-08-03 22:29:47 +0200
commit931eb0894e7cd3717f1218f0eb06382b11734c13 (patch)
treeb6ad9b0d98a8f26e496c91ad3f24d1c855b654df /Saturating a value when wrapping.md
parentaf98004d2cf437603f47f4224110a80d76d0d7de (diff)
vault backup: 2025-08-03 22:29:47
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