Overview
| Artifact ID: | 2460ea47a838d6cf5ed20b4e1c5c5d70d2970860 |
|---|---|
| Ticket: | 50dfde221669097ea777859a00d47fb1eae03387
facing issue with expr |
| User & Date: | dkf on 2015-03-05 10:57:36 |
Changes
- assignee changed to: "kbk"
- closedate changed to: "2457086.95667612"
- closer changed to: "dkf"
- comment changed to:
Found problem with "expr" while doing calculations For example : % expr (576.64 - 554.176) * 1.0 / 0.576 39.0 % expr int((576.64 - 554.176) * 1.0 / 0.576) 38 How come the int for 39.0 will become 38?
- icomment:
The issue is that the calculation isn't yielding the value that you think it is; when I try with Tcl 8.5 or 8.6 (both of which are <i>much</i> more careful in the rendering of floating point numbers than 8.4 and before were), I get this: <pre>% expr (576.64 - 554.176) * 1.0 / 0.576 38.9999999999999</pre> Binary floating point numbers (such as Tcl uses, along with <i>many</i> other programming languages) are fundamentally not capable of delivering perfectly accurate calculations for every value you can type in, in a way that is precisely analogous to how you can't represent the result of dividing 1 by 3 in a finite number of decimals. For more info see <a href="http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html">this classic paper</a>. <p> The one interesting thing about Tcl is that we now guarantee to use the minimum number of digits to represent a number when converting from the floating point internal representation to the decimal string form, subject to the constraint that the reverse operation (string-to-IEEE-<tt>double</tt>) gives exactly the same thing. That is a non-trivial guarantee; it takes quite a lot of code in Tcl's implementation to achieve it. If you really want to use a smaller number of digits than what is properly necessary, the <tt>format</tt> command is the right approach, though you should be aware that this will do some rounding.
- login: "dkf"
- mimetype: "text/html"
- resolution changed to: "Invalid"
- severity changed to: "Cosmetic"
- status changed to: "Closed"
- subsystem changed to: "47. Bytecode Compiler"
- username: "dkf"