Expr for float values in TCL -
calculating float values
tclsh % expr 0.2+0.2 0.4 % expr 0.2+0.1 0.30000000000000004 %
why not 0.3??
am missing thing. in advance.
neither 0.1 or 0.2 have exact representation in ieee double precision binary floating point arithmetic (which tcl uses internally expressions involving fractional values, there's hardware support them). means values computing never exactly think are; instead, they're both more (as happens; have been less in general). when add 0.2+ε1+0.1+ε2, can happen ε1+ε2 can add more threshold 0.3 (another imprecisely represented value) becomes next represented value above it. have observed. it's inherent in way floating point mathematics works in vast array of languages; integer arithmetic (or fractional arithmetic capable of being expressed exact multiples of power of 2, e.g., 0.5, 0.25, 0.125) guaranteed exact.
the interesting thing of note here tcl 8.5 , 8.6 prefer render floating point numbers minimal number of digits required exact value when re-parsed. if want fixed number of digits (e.g., 8) try using format
when converting:
format %.8f [expr 0.2+0.1]
Comments
Post a Comment