Urgent Help, Please

Forum for OpenSees users to post questions, comments, etc. on the use of the OpenSees interpreter, OpenSees.exe

Moderators: silvia, selimgunay, Moderators

Post Reply
Deniz
Posts: 26
Joined: Wed Aug 31, 2005 2:34 am
Location: METU
Contact:

Urgent Help, Please

Post by Deniz »

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.
Deniz Utkutug
METU, Turkey
silvia
Posts: 3909
Joined: Tue Jan 11, 2005 7:44 am
Location: Degenkolb Engineers
Contact:

Post by silvia »

you might want to check with the Tcl people.
still, you should never compare integer equality.
Silvia Mazzoni, PhD
Structural Consultant
Degenkolb Engineers
235 Montgomery Street, Suite 500
San Francisco, CA. 94104
ahmetalperparker
Posts: 90
Joined: Wed Oct 26, 2005 6:31 am
Location: Istanbul Technical University

Post by ahmetalperparker »

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
aneeman
Posts: 90
Joined: Thu Jan 12, 2006 1:13 pm
Contact:

Re: Urgent Help, Please

Post by aneeman »

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
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.
Deniz
Posts: 26
Joined: Wed Aug 31, 2005 2:34 am
Location: METU
Contact:

Post by Deniz »

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.
Deniz Utkutug
METU, Turkey
Deniz
Posts: 26
Joined: Wed Aug 31, 2005 2:34 am
Location: METU
Contact:

Post by Deniz »

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.
Deniz Utkutug
METU, Turkey
silvia
Posts: 3909
Joined: Tue Jan 11, 2005 7:44 am
Location: Degenkolb Engineers
Contact:

Post by silvia »

yes, Aneeman gave the best advice to compare two variables
for example i'd do something like:

epsilon =1e-6 (good estimate)
if (abs(x-y)<epsilon) then ...
Silvia Mazzoni, PhD
Structural Consultant
Degenkolb Engineers
235 Montgomery Street, Suite 500
San Francisco, CA. 94104
Post Reply