Hacker News new | ask | show | jobs
by jerf 1558 days ago
abs on machine-sized integers has another easy-to-make mistake: what's the absolute value of the signed byte -128?

You face some hard decisions on that one. None of the error handling paradigms are good at handling the class of numeric errors that people really, really want to pretend don't exist because no matter what kind of error they throw they're a major pain to deal with correctly.

1 comments

I'd agree, if the result type was the same as the input type. However, notice that the function I wrote returns an unsigned type, so in the case of byte-sized types it would be fn abs(x: i8) -> u8, i.e. there is enough space for 128. Otherwise, good point.