Hacker News new | past | comments | ask | show | jobs | submit login

> Also, it's not clear to me that your proposed wraparound addition operator is actually fully associative in all possible cases of overflow.

Proof sketch: write out (an/ad + (bn/bd) + cn/cd)) and ((an/ad + bn/bd) + cn/cd) symbolically, and apply the fact that twos complement arithmetic is associative, commutative and distributive in all possible cases of overflow.

> An addition operator where adding two positive numbers results in a negative number can hardly be called "the mathematically natural" variant.

It's exactly how integer arithmetic works on every CPU designed after 1980, to general satisfaction. In any case, it's mathematically natural because it's exactly how you would construct the field of fractions of an integral domain.




> write out (an/ad + (bn/bd) + cn/cd)) and ((an/ad + bn/bd) + cn/cd) symbolically, and apply the fact that twos complement arithmetic is associative, commutative and distributive in all possible cases of overflow.

I doubt that raku/perl6 uses these rules for rational arithmetic. It would be highly unintuitive to sum two rational numbers close to 0.5 and obtain a negative number. Do you have any reference for raku arithmetic rules? I cannot seem to find them.


Raku Rat falls through to floats if the denominator overflows, and has arbitrary precision numerators, it does not use twos-complement math for normal operations in any way exposed to the user, AFAIK.

Most other systems (including Scheme) that I've seen that use rational representation by default use arbitrary precision for both numerator and denominator, so don't have this concern.


Your proof fails for the odd man out negative integer value.


It does not. Feel free to justify your claim by writing down an explicit counterexample, though.




Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact

Search: