Friday, June 28, 2019

[hiurqved] Numeric types with more errors

We consider a library of numeric types intended to be pedagogically useful -- so performance is not important.  We enumerate some conditions in which operations should result in exceptions.  We list here more error conditions than typically implemented.

Of course, division by zero.

For the integer type, an exception should occur if division does not come out evenly (so has a remainder). Of course, a separate truncating or rounding division operation is also available if you explicitly want to ignore a remainder.  Which way does it round?  Perhaps division by a negative number is not permitted unless it divides cleanly, or not permitted at all.

Exponentiation of a non-negative number by a rational number can come out evenly to an integer or rational.  Exception if the result is non-integer or irrational.

Similarly for logarithms with an integer or rational base.

It might be number theoretically tricky to decide when these operations come out even.

We definitely do not want NaN, Inf, precision underflow, integer wraparound including two's complement (which becomes visible when casting to an unsigned integer type).  It's shocking that machine types with these crazy features are the numeric types that beginners typically have to learn to program with.  These are annoyances that you should only later learn to put up with when you start caring about performance.

No comments :