Nice!
Do you have plans to make the implementation robust against spurious overflow?
For example:
let a = Rational<Int8>(16, 35)
let b = Rational<Int8>(39, 85)
let c = a + b // should be 109/119
If I’m reading the code for “+
” properly, the current implementation in RationalModule
will trap when computing c
because some of the intermediate results overflow Int8
. Specifically, the line let t = n1 * (d2 / g) + n2 * s
in Rational+AdditiveArithmetic.swift will trap.
I described this issue, and briefly outlined a solution using full-width multiplication and division, in this thread last year.