4 ms·
> You're right, for relatively small integers of 8 digits or so. For doubles (more common than floats) it is up to 2^53, which is quite a usable range. > As i
by haberman 11y ago
> You're right, for relatively small integers of 8 digits or so.
For doubles (more common than floats) it is up to 2^53, which is quite a usable range.
> As in you've written such code in dozens of programs?
As in every JavaScript program ever written that uses a for loop and integer indices does this.
> 0.1 is already "past" that point which is even less impressive than 8 digit integers.
0.1 is not a binary fraction. A binary fraction can be expressed in the form a/2^b for some a and b.
Decimal representations have the same limitation (can only express decimal fractions), it just seems more exact because we write numbers in decimal.
> Are these sorts of thing really worth mentioning when the starting point is just 0.1 + 0.2 == 0.3?
It's worth mentioning when you say untrue things like "Floats are, by definition, not relevant if one wants to store an exact number exactly." Double precision floating point has more exact integer range than int32.
- kbenson 11y agoI think you guys are arguing at cross-purposes. I'm not sure anyone is arguing that an integer is better than a double for integer representation, but that storing a pair of numeric values (whether they be integers or floating point of some type) allows for more natural expression of numeric values in a high level language, with more fidelity. > > Floats are, by definition, not relevant if one wants to store an exact number exactly. > Floats can store integer values exactly up to an upper limit (2^24 IIRC), which is often useful. And they can store binary fractions exactly up to a point. If you have some knowledge about the number you are storing, such as that it is an integer, then you can use the fact that floats can represent integers exactly to large values. If you need a general purpose numeric type of which you can't assume it's an integer, and it needs to be stored exactly, then I think it's valid to say that floating point numbers are not relevant, at least not as the entirely of the underlying storage container. > No number representation can represent all numbers exactly. Each can store some subset of all numbers exactly. Yes. So we are talking about storage mechanisms that allow for more fidelity than plain floats and doubles can achieve.
- raiph 11y agoIf you search back to the comment that started this subthread you'll find it began: "Try 0.1 + 0.2 == 0.3 in some other language, then try it in Perl 6." Do you agree with all of the next sentence or not? Please start with a plain yes or no before getting in to details. The three individual literal decimal numbers shown above are not integers, they're not binary fractions, they're not floats, they are rational numbers, they are exact numbers, they are easily stored exactly on a computer, and whether or not they are stored exactly on a computer is a choice made by a programming language.
- haberman 11y agoNo, because I think that "easily stored on a computer" is too vague and reductive. I apologize if I have been too unclear about my point. My point is that your position is unfair. Your position seems to be that rational arithmetic is an obvious choice that other languages have been too lazy or stupid to adopt as their default. My point is that rational arithmetic is a fine choice for many things, but it is not a strict improvement over other number representations. Yes it can handle 0.1 + 0.2 = 0.3, but your implied conclusion that it is an obvious "default" choice is not accurate. It is not as perfect, and floating point is not as bad, as you seem to think. Yes, doubles have lots of values they can't represent perfectly, but so do rationals. Like sqrt(2), sin(1), pi, or any calculations involving numbers like these. Yes rationals can represent lots of numbers perfectly, but if you're keeping everything exact these can start to take lots of memory. Some slides I found about Perl 6 indicate that Rat values lose precision if the denominator gets too big, to avoid using too much memory. If this is true, the Rat type shouldn't be thought of as an "exact" type any more than double -- it could cease to be exact even if you stay away from inherently non-rational number like I listed above. What I'm disagreeing with overall is the dichotomy you are trying to create of: double=approximate, Perl 6 number=exact. Neither of those are really very true. There are cases where double is exact and cases where Perl 6 is not exact.
- raiph 11y ago> "easily stored on a computer" is too vague and reductive. Fwiw, from the compiler source code: my role Rational[::NuT, ::DeT] does Real { has NuT $.numerator; has DeT $.denominator; > Your position seems to be that rational arithmetic is an obvious choice Yes. Rational storage and rational arithmetic is an obvious choice for storing and processing rational numbers. (Alongside the other obvious choice -- using floats.) > other languages have been too lazy or stupid I'm extolling what I consider a minor Perl 6 virtue. Where have I been negative toward other languages? > rational arithmetic is a fine choice for ... ... doing rational arithmetic. > it is not a strict improvement over other number representations. For representing Integers? Sure. Irrationals? Agreed. Complex numbers? Gotchya. Number crunching where absolute exactness is not necessary? Use floats. But if exactness is required and a formula only involves rational numbers (and integers) then rational storage and processing is a strict improvement over use of floats. > [rationals are] not as perfect, and floating point is not as bad, as you seem to think. When did I suggest floats are bad? The general number type in Perl 6 is a float. Floats are awesome! But floats are by design not exact for arbitrary rational numbers. Their awesomeness derives directly from them being inexact in this way. When did I suggest storing rationals is perfect? They use up more memory than floats and processing can become pathologically slow if the denominators get big enough. This problem derives directly from them being exact. > doubles have lots of values they can't represent perfectly, but so do rationals. Like sqrt(2), sin(1), pi Those are explicitly defined in mathematics as numbers that are not rational. Rational storage and arithmetic is for representing and processing numbers that are rational! One can't exactly store an irrational number. The Perl 6 compiler defines a constant for the irrational number `pi`: my constant pi = 3.14159_26535_89793_238e0; The 'e0' on the end means that `pi` is stored as a float. > Some slides I found about Perl 6 indicate that Rat values lose precision if the denominator gets too big, to avoid using too much memory. The numerator of a default rational (a non-parameterized Rat) can be arbitrarily large but if the denominator exceeds 64 bits (20 decimal digits) then the compiler converts the number to a float. If you don't like that then you can use a rational with a larger denominator, with the extreme being a FatRat that has an arbitrary sized denominator as well as arbitrary sized numerator. > If this is true, the Rat type shouldn't be thought of as an "exact" type any more than double I think that view misses something very important. This is indeed where the discussion could get interesting. We shall see. It seems we needed to get past a lot of misunderstanding first. > What I'm disagreeing with overall is the dichotomy you are trying to create of: double=approximate, Perl 6 number=exact. I've never said that. What I have said is that, for storing and processing rational numbers, doubles are approximate (and they are); that it's possible to be exact instead (and it is); and that Perl 6 returns True for the expression `0.1 + 0.2 == 0.3` because by default it processes rationals as exact rationals. > There are cases where double is exact Not for rational numbers (I'm ignoring silly examples like 1/1 and others that happen to work out because the denominator and numerators are powers of two or whatever). > and cases where Perl 6 is not exact. Not for rational numbers, if you choose to have them be exact.