4 ms·
There are times when you want to know how much overflow occurred -- think of the way you learn to do multi-digit addition. There is a checked_add that returns a
by re 4y ago
There are times when you want to know how much overflow occurred -- think of the way you learn to do multi-digit addition. There is a checked_add that returns an Option<T> if you only care about success/failure.
- sshine 4y agoAn example is efficient prime-field arithmetic: https://cp4space.hatsya.com/2021/09/01/an-efficient-prime-for-number-theoretic-transforms/ https://cp4space.hatsya.com/2021/09/01/an-efficient-prime-fo... > If A happened to be larger than C, and therefore the result of the subtraction underflowed, then we can correct for this by adding p to the result. [...] we can multiply B by 2^32 using a binary shift and a subtraction, and then add it to the result. We might encounter an overflow, but we can correct for that by subtracting p. (Highlighting the parts that relate to reacting to overflow.)