|
2001-04-20
| ||
| 02:41 | • Ticket [218443ffff] an if expression comparing a computed floating point value w... artifact: 9bb8923d96 user: dgp | |
|
2000-10-26
| ||
| 08:22 | • Closed ticket [218443ffff]. artifact: 400b04fe04 user: welch | |
| 00:57 | • New ticket [218443ffff]. artifact: 33841e7bf8 user: nobody | |
an if expression comparing a computed floating point value w
Closed / Invalid218443ffffffffffffffffffffffffffffffffffCreated 26 years agoUpdated 26 years ago
Descriptionplain2000-10-26 00:57:39
OriginalBugID: 6295 Bug
Version: 8.3.2
SubmitDate: '2000-09-21'
LastModified: '2000-10-18'
Severity: MED
Status: Closed
Submitter: techsupp
ChangedBy: ericm
OS: All Windows
OSVersion: Windows NT 4.0
Machine: Dell Dimension P200
FixedDate: '2000-10-25'
ClosedDate: '2000-10-18'
Name:
Jon Hilbert
ReproducibleScript:
for {set i 0} {$i < 10} {incr i} {set a($i) "0.10"}
set total 0
for {set i 0} {$i < 10} {incr i} {set total [expr {$total + $a($i)}]}
if {$total != 1.0} {puts "what? why doesn't $total == 1.0 ?"}
if {[expr $total] == 1.0} {puts "but \[expr $total] == 1.0 ?"}
info patchlevel
ObservedBehavior:
as the script shows, the results of "if {$total != 1.0}" are not the same as "if {[expr $total] != 1.0}". I think they should be.
Floating-point comparisons are notoriously unreliable. Likely what you are hitting here is an artifact of the limitations of floating-point arithmetic inherent in all computer platforms.
If you must perform floating-point equality comparisons, you are better off with something like:
set tolerance 0.001
if { 1.0 - $tolerance <= $total && $total <= 1.0 + $tolerance } {
...
}
That is, allow for a "slush factor" in the comparison by testing that the value is "tolerably close" to the expected value.
Eric Melski The Other Tcl Guy
ericm at ajubasolutions.com Ajuba Solutions
-- 10/18/2000 ericm
welch Closed ticket [218443ffff].
[History diff]2000-10-26 08:22:16
dgp Ticket [218443ffff]: 4 changes
[History diff]2001-04-20 02:41:06
Log in (anonymous is fine) to add a comment.