From: <no...@so...> - 2002-07-05 22:36:56
|
Bugs item #576512, was opened at 2002-07-02 19:20 You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=110894&aid=576512&group_id=10894 Category: None Group: None >Status: Pending Resolution: Invalid Priority: 5 Submitted By: Nobody/Anonymous (nobody) Assigned to: miguel sofer (msofer) Summary: Math problem. Initial Comment: Using 8.2.3 (SGI), 8.3.1, or 8.3.3, (both linux) the following behavior is observed: % set x [expr 1.2*3.0] 3.6 % if {3.6 > $x} {puts "3.6 is greater than $x!"} 3.6 is greater than 3.6! % puts [expr 3.6 > $x] 0 So, apparently the expr procedure evaluates differently than the parsing of the if conditional. I would rather not make all of my if statements like: if {[expr 3.6 > $x] == 1} {...} ---------------------------------------------------------------------- Comment By: Don Porter (dgp) Date: 2002-07-02 20:10 Message: Logged In: YES user_id=80530 "So, apparently the expr procedure evaluates differently than the parsing of the if conditional" No, you will find that [expr {3.6 > $x}] always gives the same result as if {3.6 > $x} {...} You did not enclose the expression in braces, so two rounds of substituion occurred, including a translation from double -> string -> double. Such translations often lose precision and change the precise floating point value. Seeking arbitrarily precise answers involving floating point values is a mistake in the first place. ---------------------------------------------------------------------- Comment By: miguel sofer (msofer) Date: 2002-07-02 20:00 Message: Logged In: YES user_id=148712 Oops: first I forget to assign to myself, then I inadvertedly change the status ... ---------------------------------------------------------------------- Comment By: miguel sofer (msofer) Date: 2002-07-02 19:56 Message: Logged In: YES user_id=148712 This is because the internal representation of (1.2*3.0) *is* larger than the internal representation of (3.6) - floats are weird :( Now, when you do puts [expr 3.6 > $x] expr will do double substitution - essentially, it builds the string "3.6 > $x" with '$x' replaced by the string representation of its value, then parses this string and finally evaluates the expression.The roundtrip via the string rep of '$x' causes the equality due to rounding in the float-to-string conversion. If you do instead puts [expr {3.6 > $x}] preventing the conversion to string, you obtain 1. So: it is consistent, the problem that they aren't equal is intrinsic to the binary representation of floats. This is inherited from C. See http://wiki.tcl.tk/879 for more troublesome examples with real numbers. ---------------------------------------------------------------------- You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=110894&aid=576512&group_id=10894 |