Urgent Help, Please
Moderators: silvia, selimgunay, Moderators
Urgent Help, Please
Dear all,
I have discovered this problem while I was writing a parametric problem in Tcl for an opensees model. There are three scripts listed below, which MUST give the same results??? I simply add and subtract a number and compare with another variable that holds the SAME number.
Code1:
set a [expr 1.2+19.5-19.5]
set c 1.2
if {$a<$c} {puts "1" }
->>> The outputs is "1" (a=1.2 and < c=1.2 !!!!!??????)
if {$a==$c} {puts "1" }
->>> The outputs is "" !!!!!??????
Code2:
set a [expr 1.2+1.5-1.5]
set c 1.2
if {$a>$c} {puts "1" }
->>> The outputs is "1" (a=1.2 and > c=1.2 !!!!!??????)
if {$a==$c} {puts "1" }
->>> The outputs is "" !!!!!??????
Code3:
set a [expr 1.2+0.1-0.1]
set c 1.2
if {$a>$c} {puts "1" }
->>> The outputs is "" (Finally a=c=1.2)
if {$a==$c} {puts "1" }
->>> The outputs is "1" (Finally a=c=1.2)
I am very confused, is there a problem with my code/syntax or is there a bug in the tcl interpreter. I have run this code on several independent computers and get the same results. Also, if this is a bug I believe it is important from opensees point of view.
Thanks in advance.
I have discovered this problem while I was writing a parametric problem in Tcl for an opensees model. There are three scripts listed below, which MUST give the same results??? I simply add and subtract a number and compare with another variable that holds the SAME number.
Code1:
set a [expr 1.2+19.5-19.5]
set c 1.2
if {$a<$c} {puts "1" }
->>> The outputs is "1" (a=1.2 and < c=1.2 !!!!!??????)
if {$a==$c} {puts "1" }
->>> The outputs is "" !!!!!??????
Code2:
set a [expr 1.2+1.5-1.5]
set c 1.2
if {$a>$c} {puts "1" }
->>> The outputs is "1" (a=1.2 and > c=1.2 !!!!!??????)
if {$a==$c} {puts "1" }
->>> The outputs is "" !!!!!??????
Code3:
set a [expr 1.2+0.1-0.1]
set c 1.2
if {$a>$c} {puts "1" }
->>> The outputs is "" (Finally a=c=1.2)
if {$a==$c} {puts "1" }
->>> The outputs is "1" (Finally a=c=1.2)
I am very confused, is there a problem with my code/syntax or is there a bug in the tcl interpreter. I have run this code on several independent computers and get the same results. Also, if this is a bug I believe it is important from opensees point of view.
Thanks in advance.
Deniz Utkutug
METU, Turkey
METU, Turkey
-
- Posts: 90
- Joined: Wed Oct 26, 2005 6:31 am
- Location: Istanbul Technical University
Hello Deniz,
I am reading a book about java and got a similar situation in java.Maybe your problem is related with it. In comparing Strings in java you can not use == since it only checks the memory adresses allocated for both the string variables. So in java you can do it by a handle from the standard classes. In tcl, there is no distinction which kind of data type (in ex. int double string float...) you are using with the variable and thus it may be checking the allocated adresses for the variables and thus gives the unwanted result.
Hope its usefull
Regards
Ahmet Alper Parker
I am reading a book about java and got a similar situation in java.Maybe your problem is related with it. In comparing Strings in java you can not use == since it only checks the memory adresses allocated for both the string variables. So in java you can do it by a handle from the standard classes. In tcl, there is no distinction which kind of data type (in ex. int double string float...) you are using with the variable and thus it may be checking the allocated adresses for the variables and thus gives the unwanted result.
Hope its usefull
Regards
Ahmet Alper Parker
Re: Urgent Help, Please
This is actually a floating point issue; it could happen in C++ as well. It has to do with how floating point numbers are dealt with at the hardware level.
Basically if you do any operation on floating points (+,-,/,*), you will then have an approximate answer. And at some point of the approximation (very small) there will be error.
The best thing is not to use == with floating points. It is better to decide on some tolerance (e.g. +/- .00000001 ).
cheers,
alisa
Basically if you do any operation on floating points (+,-,/,*), you will then have an approximate answer. And at some point of the approximation (very small) there will be error.
The best thing is not to use == with floating points. It is better to decide on some tolerance (e.g. +/- .00000001 ).
cheers,
alisa
Deniz wrote:Dear all,
I have discovered this problem while I was writing a parametric problem in Tcl for an opensees model. There are three scripts listed below, which MUST give the same results??? I simply add and subtract a number and compare with another variable that holds the SAME number.
Code1:
set a [expr 1.2+19.5-19.5]
set c 1.2
if {$a<$c} {puts "1" }
->>> The outputs is "1" (a=1.2 and < c=1.2 !!!!!??????)
if {$a==$c} {puts "1" }
->>> The outputs is "" !!!!!??????
Code2:
set a [expr 1.2+1.5-1.5]
set c 1.2
if {$a>$c} {puts "1" }
->>> The outputs is "1" (a=1.2 and > c=1.2 !!!!!??????)
if {$a==$c} {puts "1" }
->>> The outputs is "" !!!!!??????
Code3:
set a [expr 1.2+0.1-0.1]
set c 1.2
if {$a>$c} {puts "1" }
->>> The outputs is "" (Finally a=c=1.2)
if {$a==$c} {puts "1" }
->>> The outputs is "1" (Finally a=c=1.2)
I am very confused, is there a problem with my code/syntax or is there a bug in the tcl interpreter. I have run this code on several independent computers and get the same results. Also, if this is a bug I believe it is important from opensees point of view.
Thanks in advance.
Dear Silvia,
I have already mailed to "Tcl People". I do know that it is beyond opensees subject but may you please briefly explain what you mean by "you should never compare integer equality". How can I establish a condtitional statement comparing two double/float variables ? All I wanted to express that 1.2 does not equal to 1.2, which is obtained from some expr involved calculation. And, from my point of view this is a real problem when you are programming an incremental dynamic analysis. On the other hand, if I am doing something wrong with the syntax may you please correct this simple code to express the equality.
Thanks in advance.
I have already mailed to "Tcl People". I do know that it is beyond opensees subject but may you please briefly explain what you mean by "you should never compare integer equality". How can I establish a condtitional statement comparing two double/float variables ? All I wanted to express that 1.2 does not equal to 1.2, which is obtained from some expr involved calculation. And, from my point of view this is a real problem when you are programming an incremental dynamic analysis. On the other hand, if I am doing something wrong with the syntax may you please correct this simple code to express the equality.
Thanks in advance.
Deniz Utkutug
METU, Turkey
METU, Turkey
Dear all,
First of all I would like to thank to people who kindly responded quickly to this very subject.
Alisa wrote that "The best thing is not to use == with floating points. It is better to decide on some tolerance (e.g. +/- .00000001 )".
I think this comment is very important and useful, hence should be emphasized to all opensees users, especially for the users programming parametric runs.
Best regards.
First of all I would like to thank to people who kindly responded quickly to this very subject.
Alisa wrote that "The best thing is not to use == with floating points. It is better to decide on some tolerance (e.g. +/- .00000001 )".
I think this comment is very important and useful, hence should be emphasized to all opensees users, especially for the users programming parametric runs.
Best regards.
Deniz Utkutug
METU, Turkey
METU, Turkey