6 ms·
It uses exact rational arithmetic for values that it can represent that way, preferring correctness to efficiency. Scheme and some other languages do the same t
by dragonwriter 7y ago
It uses exact rational arithmetic for values that it can represent that way, preferring correctness to efficiency. Scheme and some other languages do the same thing.
Using imprecise but efficient-to-calculate-with-numbers for exact literals by default is probably the most pervasive premature optimization in all of computing.
- enriquto 7y ago> It uses exact rational arithmetic for values that it can represent that way, preferring correctness to efficiency. This is exactly the same situation as with floating point numbers. In both cases you have a predetermined finite set of rational numbers, with exact rational arithmetic when you stay within those numbers, and deterministic rules when your operation exits this finite set. > Using imprecise but efficient-to-calculate-with-numbers for exact literals by default is probably the most pervasive premature optimization in all of computing. I do not see how this is a matter of precision nor efficiency. Floating point arithmetic was deemed more useful for general purpose for good reasons: the representable numbers are mostly scale-free, so that you do not care about the absolute size of your numbers (you can compute in armstrongs or in parsecs and obtain essentially the same results). With rational arithmetic using bounded integers, you cannot represent very large or very small numbers. On the other hand you can represent small fractions like 1/3, which is arguably useful in some cases, but not really a big deal in practice. There's no reason why rational arithmetic with bounded numerator and denominator could not be efficiently implemented in hardware as fast as floating point; I do not understand your point.
- saithound 7y agoIt is not at all the same situation as with floating point numbers. 1. It does not use a predetermined finite set of rational numbers. It uses a bigint numerator and a 64-bit denominator. 2. Even the case of bounded numerator and bounded denominator would preserve more of the nice mathematical properties of rational arithmetic than IEEE 754 floating-point arithmetic does (e.g. the associativity of addition). 3. While the latter could be implemented in hardware, it is not implemented in hardware. The defaults for literals prefer the choice that is implemented in hardware, IEEE 754 floating-point. This is a premature optimization.
- jcranmer 7y ago> 2. Even the case of bounded numerator and bounded denominator would preserve more of the nice mathematical properties of rational arithmetic than IEEE 754 floating-point arithmetic does (e.g. the associativity of addition). It does not preserve associativity of addition. If a = MAX_INT/1, b = 1/1, c = -1/1, then a + b is Infinity, and (a + b) + c is therefore also Infinity, while b + c is 0, so a + (b + c) is MAX_INT/1.
- enriquto 7y agoEven if you allow bigint numerators, addition is still not associative. You can similarly build an example 1/(M-1) - 1/M ...
- saithound 7y ago> It does not preserve associativity of addition. It does, if you use the mathematically natural addition operation (see my reply to enriquto). In the example above, the result is MAX_INT/1 in both cases.
- jcranmer 7y agoAn addition operator where adding two positive numbers results in a negative number can hardly be called "the mathematically natural" variant. Saturation arithmetic (which is the norm for floating point, and even most fixed point) is pretty obviously not associative. Also, it's not clear to me that your proposed wraparound addition operator is actually fully associative in all possible cases of overflow.
- saithound 7y ago> 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.
- dragonwriter 7y ago> In both cases you have a predetermined finite set of rational numbers Neither Scheme nor Raku does this (Scheme uses unbound numerator and denominator in its default rational type, Raku has unbound numerator.) > I do not see how this is a matter of precision It's an issue of precision because exact rationals precisely represent the numbers expressed in decimal literals and provide exact (as opposed to approximate with floating point) arithmetic operations, though, to be fair, Raku’s choice of Rat instead of FatRat as the default has some warts in arithmetic.
- Ultimatt 7y ago"bounded integers" not entirely sure what you mean. But the integers are big integers that can take up the entirety of your computer memory to represent a single rational number if you so wish.
- dnautics 7y agoThat's a really bad choice and it will result in surprisingly unpredictable results because terminating divisibility by 10 is much much more complicated than by a pure power of a prime. I imagine whole classes of unexpected problems like timing side channel attacks, financial types expecting it to work better and missing easily overlooked corner cases... How do I know this? I spent two years investigating exact versus inexact arithmetic in the context of computational science. >It uses exact rational arithmetic for values that it can represent that way, preferring correctness to efficiency Fwiw, floats do exactly this the only difference is that you've changed your base from 2 to 10 and introduced unnecessary computation for what is maybe a 10% increase in exactly computable values.
- dragonwriter 7y ago> Fwiw, floats do exactly this the only difference is that you've changed your base from 2 to 10 No, exact rationals aren't equivalent to decimal floating point, which while not as good as arbitrary precision rational or decimal representation is still better than binary floating point for decimal literals. In arbitrary precision decimal or rational representation, or even Raku's bounded denominator rationals, if p and q are decimal literals that can be exactly represented in the representation, then so are p + q and p - q. For the arbitrary precision versions, subject to available memory, so are p × q and p ÷ q. Fixed-size floating point (binary or decimal) provides none of those precision guarantees (whether for decimal or binary literals).