Skip to content
Tcl Source Code History Of Ticket 50dfde221669097e
Login

History Of Ticket 50dfde221669097e

Artifacts Associated With Ticket 50dfde221669097e

  1. Ticket change [10641a1468] (rid 141283) by gbrmmt1134 on 2015-03-05 07:59:50:

    1. assignee initialized to: "nobody"
    2. closer initialized to: "nobody"
    3. cmimetype initialized to: "text/plain"
    4. comment initialized to:
      Found problem with "expr" while doing calculations 
      For example : 
      % expr (576.64 - 554.176) * 1.0 / 0576
      39.0
      % expr int((576.64 - 554.176) * 1.0 / 0576)
      38 
      
      How come the int for 39.0 will become 38?
      
    5. foundin initialized to: "8.4"
    6. is_private initialized to: "0"
    7. login: "gbrmmt1134"
    8. priority initialized to: "5 Medium"
    9. private_contact initialized to: "1f9dc978126c843d03707c55bd407445464d0f64"
    10. resolution initialized to: "None"
    11. severity initialized to: "Severe"
    12. status initialized to: "Open"
    13. submitter initialized to: "gbrmmt1134"
    14. subsystem initialized to: "16. Commands A-H"
    15. title initialized to: "facing issue with expr"
    16. type initialized to: "Bug"
  2. Add attachment "BugInTcl.png" [514eb0d883] (rid 141284) by gbrmmt1134 on 2015-03-05 08:00:41.

  3. Ticket change [2460ea47a8] (rid 141285) by dkf on 2015-03-05 10:57:36:

    1. assignee changed to: "kbk"
    2. closedate changed to: "2457086.95667612"
    3. closer changed to: "dkf"
    4. 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?
      
    5. 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.
      
    6. login: "dkf"
    7. mimetype: "text/html"
    8. resolution changed to: "Invalid"
    9. severity changed to: "Cosmetic"
    10. status changed to: "Closed"
    11. subsystem changed to: "47. Bytecode Compiler"
    12. username: "dkf"