3 ms·
Addition isn't a problem, any nonzero integer plus a nonzero integer is a nonzero integer. For subtraction it becomes fallible. You get back an option, 5 - 4 =
by maxbond 3y ago
Addition isn't a problem, any nonzero integer plus a nonzero integer is a nonzero integer.
For subtraction it becomes fallible. You get back an option, 5 - 4 = Some(1), 4 - 5 = None. (These are probably methods rather than infix operators, your language may vary.)
See for example the methods documented here: https://doc.rust-lang.org/stable/std/num/struct.NonZeroUsize.html https://doc.rust-lang.org/stable/std/num/struct.NonZeroUsize...
As for it's utility, it enables certain optimizations and it can be a useful constraint. Eg for ensuring that you don't divide or multiply by zero.
- pharmakom 3y agoIt would be in interred ring to have a memory bounds for the program too!
- zokier 3y agoIt is worth pointing out that the same problems apply to "normal" integer types like int32/uint32; you can trivially over/underflow the bounds with basic arithmetic. In that sense it doesn't really matter what the bounds are exactly.
- School-Cotton 3y ago> any nonzero integer plus a nonzero integer is a nonzero integer That’s not true. 5 + (-5) is 0.
- maxbond 3y agoYou're right, I was thinking of a nonzero unsigned integer. D'oh! Thanks for the correction!