3 ms·
Better 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>
by kazinator 11y ago
Better 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!