7 ms·
Those languages which print 0.30000000000000004 are doing something which is silly as a default: they are printing 17 decimal digits, out of a type that only su
by kazinator 11y ago
Those languages which print 0.30000000000000004 are doing something which is silly as a default: they are printing 17 decimal digits, out of a type that only supports 15.
$ txr -p '(+ .1 .2)'
0.3
Why is that? Here, the underlying type is the C double type. The printed representation is obtained according to a default precision. That default is taken from the C constant DBL_DIG. For an IEE754 double, that constant is 15.
It is misleading to print more decimal digits out of a double than DBL_DIG; that is the constant which tells you how many decimal digits of precision double can reliably store. Thus I chose that constant as the default printing precision for floats: to give the programmer/user the maximum realistic precision. That is to say, print the decimal digits which are plausibly there, and not any fictional ones.
17 decimal digits requires a 57 bit mantissa -- ceil(log 10 / log 2) * 17). The 64 bit double type has only 52. So if you print 17, you're "making shit up".
- ant6n 11y agoNitpick: the 64 bit double type has a mantissa of 52 bits, but there's a 1 implied in front, so the precision is 53 bits.
- kazinator 11y agoImplied fixed material doesn't contribute precision. Otherwise we could pretend that there are twenty-four implied 1's and the precision is 76 bits. Because the implied 1 cannot be zero, it isn't a bit; it doesn't carry information. For instance if we define a four bit binary number type which has an implied 1, it can only represent values from 16 to 31. That's four bits of precision; the range is just displaced.
- ant6n 11y agoThe information of the first bit is implied via the exponent. Twenty four implied 1's would give you a precision of 76 bits, but you would restrict yourself to a very small subset of all possible numbers with that precision. EDIT: Let's put it another way. Assume you have 3 decimal digits of precision for a float. The first digit cannot be zero. Would you then claim that you don't have 3 digits of precision, but rather log(10x10x9,10) = 2.95.., because the first digit only carries 0.95.. digits of information?
- kazinator 11y agoStrictly speaking, the 2.95 is right; but there is justification in rounding it up to 3. There is much less justification for rounding up 2.0 to 3.
- deleted 11y ago[deleted]
- mortehu 11y ago> It is misleading to print more decimal digits out of a double than DBL_DIG If you want round-trip conversion for all `double` numbers, i.e. from `double` to text and back, you need 17 digits of precision (%.17g). Likewise, if you want round-trip conversion for all `float` number, you need 9 digits (%.9g). The reason for this is that even though these types can really only reliably give 15 and 6 digits of precision, some of the numbers they can represent are closer to each other than that. In C++, you can get the former limit using std::numeric_limits<double>::digits10, and the latter using std::numeric_limits<double>::max_digits10. http://en.cppreference.com/w/cpp/types/numeric_limits/max_digits10 http://en.cppreference.com/w/cpp/types/numeric_limits/max_di...
- kazinator 11y ago> If you want round-trip conversion for all `double` numbers, i.e. from `double` to text and back I don't. Rather, I want round-trip conversion of all 15-digit-precision decimal numbers to the machine and back. If the machine calculates some numbers which are different from some of these but alias to them textually, I don't care. Someone else might want the decimal text representation to provide bit-exact storage semantics for arbitrary doubles. For that reason it might be a better design choice to obtain the default printing precision from a special variable, which itself defaults to 15, rather than a hard-coded default.
- jordigh 11y ago> that only supports 15. it supports somewhere between 15 and 16, actually. There are 52 bits of precision in a double's mantissa[1]. This gives you log_10(2^52) = 52*log(2)/log(10) ~ 15.654 digits of precision, that is, the 16th decimal place is accurate about 65% of the time. You can also exactly represent in decimal the binary value stored, because as long as you have 52 decimal places, you have enough 2's in the denominator for all of the bits that you can represent (each 10 in the denominator pairs up with each 2 from each bit). -- [1] Plus an implicit bit, but not in the fractional part of the mantissa.
- StefanKarpinski 11y agoThis is a common but fundamentally wrong view of floating-point numbers. You are not "making shit up" by printing more that 15 digits of a floating-point number. Floats, doubles, etc. have precise values. As I said in another comment, the double represented by the literal `0.1` is precisely 0.1000000000000000055511151231257827021181583404541015625; similarly, `0.2` is precisely 0.200000000000000011102230246251565404236316680908203125 and `0.3` is precisely 0.299999999999999988897769753748434595763683319091796875. From these exact values, you can see why `0.1 + 0.2 != 0.3` – the left hand side is greater than 0.3 while the right hand side is smaller than 0.3. Printing only 15 digits is not doing programmers any favors: even though `0.1 + 0.2` will print as "0.3", that just makes the lie even worse – they will be even more confused when `0.1 + 0.2 != 0.3` even though both values print identically.
- crpatino 11y agoOr, for that matter, 1.0 + 2.0 != 3.0
- Stratoscope 11y ago1.0, 2.0, and 3.0 are all represented perfectly and precisely in IEE754 floating point, as are all integers up through 2^24 (for 32-bit floats) or 2^53 (for 64-bit doubles). So 1.0 + 2.0 == 3.0.
- crpatino 11y agoHow is that? I assume the mantissa is chosen to represent the full number, but how is the exponent set so that the granularity is of exactly 1.0? And of course 1x10^7 + 2x10^7 != 3x10^7, right?
- Stratoscope 11y ago2^0 is 1, so if the exponent is 0 then the mantissa times one is your value, so every integer has an exact representation. If the exponent is 1, then the mantissa is multiplied by 2 to get the actual value. Put another way, the mantissa is the value divided by 2 and rounded, i.e. shifted right one bit. > 1x10^7 + 2x10^7 != 3x10^7, right? This particular case is interesting. If we're talking about 64-bit doubles, then you have 53 bits of precision for integers. All those values are well within that range, so this arithmetic and comparison are done with precise integer values, and the expression will compare equal. If we're talking about 32-bit floats, the range of precise integer values is -16,777,216 through 16,777,216, or -2^24 through 2^24. 1x10^7 is 10,000,000, within that range, but the other two values are outside the range. So you might expect that != would be the answer here. But if you test it, that isn't the case: the expression compares equal! The reason: although 20,000,000 and 30,000,000 are outside the range where every integer has a precise representation, they are within the range where every even integer is precise: -33,554,432 to 33,554,432. Values within this range but outside the range of precise integers are rounded to a multiple of 2. Similarly in the range -67,108,864 to 67,108,864, all integers which are a multiple of 4 are represented precisely. Basically, as you go outside the range of precise integers, values get rounded to a multiple of 2, 4, 8, etc. as required. When the mantissa overflows the available precision, it is shifted to the right enough so that it fits without losing the most significant bits. Instead, the least significant bits are discarded, and the exponent is incremented by the number of discarded bits. Of course you could choose other values where the rounding doesn't work out in your favor, and then you'd get the unequal comparison you expect. A simple example: 33333333.0f + 1.0f != 33333334.0f Here, the value 33,333,333 is rounded down to 33,333,332. Add 1 to that and you get 33,333,333, which is again rounded down to 33,333,332. 33,333,334 is represented precisely, so the comparison fails. https://en.wikipedia.org/wiki/Single-precision_floating-point_format#Precision_limitations_on_integer_values https://en.wikipedia.org/wiki/Single-precision_floating-poin...
- kamaal 11y agoYour comment made me look up 'txr'. Looks like a wonderful tool. Do you use that often for data munging tasks? How big is this tool in that area of work?
- kazinator 11y agoSince I'm the author, I absolutely use it as a matter of "eat your own dogfood" and because it is actually convenient. TXR is not "big" in this area because it is very difficult to get people to notice new things like this. It is being rapidly developed. OpenHub stats: https://www.openhub.net/p/txr https://www.openhub.net/p/txr In the last half a year or so, I added major features such as: structs with good OOP support, an interactive REPL (based on "linenoise" with a lot of my hacks applied), and delimited continuations.
- dragonwriter 11y ago> Those languages which print 0.30000000000000004 are doing something which is silly as a default Yes, but the silly thing isn't what you say. The silly thing is having binary floating points as the default representation for literals expressed as precise decimals.
- dnautics 11y agoDefaulting to a Binary Encoded Decimal is also silly because its not as performant and loses compactness. Fundamentally. (Having a composite base comes with problems). What is your suggestion, having a default floating point have a Binary or hexadecimal representation for literals? Let's say I want to input a planet's mass as 6e24 kg - quick what is that in your literals?
- dragonwriter 11y ago> Defaulting to a Binary Encoded Decimal is also silly because its not as performant and loses compactness. Defaulting to correctness makes a lot more sense than often premature time and space optimizations by default. > What is your suggestion, having a default floating point have a Binary or hexadecimal representation for literals? There's lots of good solutions (Scheme's, of having a numeric tower with literals using the minimal-scope exact representation -- e.g., integer if there is no fractional part, decimal if its a decimal literal, rational if it is rational literal not expressed as a decimal -- by default unless they have an modifier specifying intent to use an inexact representation) is the best.
- dnautics 11y agothe point is, there are still unrepresentables (1/3 e.g.) in binary encoded decimal. If it's all about literals, just make the user put the correct literal in, and make them understand that floats are imperfect anyways. If you're doing anything remotely useful with Binary Coded Decimal, probably about two or three operations in, you're going to lose resolution on your exactness. It doesn't buy you much. GIGO.
- 11y ago
- kazinator 11y agoBetter all-round solution: $ ./txr This is the TXR Lisp interactive listener of TXR 123. Use the :quit command or type Ctrl-D on empty line to exit. 1> (tostring (+ 0.1 0.2)) "0.3" 2> (let ((*flo-print-precision* flo-max-dig)) (tostring (+ 0.1 0.2))) "0.30000000000000004" 3> *flo-print-precision* 15 4> flo-dig 15 5> flo-max-dig 17 - 15 by default is reasonable for everyday programming; we don't need results like 0.30000....4 popping up in our faces. - provide the constant which indicates how many decimal digits are needed to preserve the binary value exactly: this is needed for faithful storage and communication --- we wouldn't want a Sexp-based RPC call to behave differently from a local computation. - provide the printing precision default as a special variable which can be overridden over a dynamic scope.
- jordigh 11y ago15 by default is reasonable for everyday programming; we don't need results like 0.30000....4 popping up in our faces. That just changes the problem. You can't get rid of rounding error by representing decimal numbers in binary. 0.3 - 0.2 - 0.1 still won't show up as zero even if you only display 15 decimal digits.
- kazinator 11y agoI'm not about to change the floating-point type to a non-binary support. I think that 15 digits is the "right" value for ordinary display purposes. If you present a literal value of up to 15 digits to the machine, it gets converted to some approximation which, when printed back with 15 digits of precision, gives you the same digits. By using 15 we avoid this: 1> (let ((*flo-print-precision* flo-max-dig)) (tostring 0.3)) "0.29999999999999999" 2> (tostring 0.3) "0.3" I have no "rounding error" because I haven't performed a calculation. I understand that when I enter 0.3 into the machine, the floating-point value isn't exactly 0.3. I have an error, because the representation can only provide a close approximation of 0.3. Yet I know that this approximation is so close to 0.3 that I would like it printed that way. Now of course it will be printed that way if I round to, say, four places. But that will throw away precision for other numbers. If I round to DBL_DIG digits (15), I get the maximum precision, without having my input numbers altered into something else. It is useful and elegant for the default printing precision to be as high as possible, yet such that numbers that I enter into a REPL are echoed back at me pretty as I entered them, modulo choice of notation. No, of course, DBL_DIG doesn't guarantee that I can do any sequence of calculations, like 0.3 - 0.2 - 0.1 and still get back a result which is exact to DBL_DIG digits! There is no lower bound on how inaccurate a floating-point calculation can be, if it is carried through enough iterations!