Comparing two floats

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
Kunutsu
Newbie
Posts: 5
Joined: Fri Oct 19, 2007 7:53 pm

Comparing two floats

Post by Kunutsu » Fri Oct 19, 2007 8:01 pm

i try to compare a (computed) value to a constant value like this:

let>x=5/8
if>%x%>0.5


actually the value of x is 0,625 and the condition should be true. but in the if-condition x seems to be interpreted as an integer value and "if>%x%>0,5" doesn't work.
can anyone tell me how to compare two floats correctly?

User avatar
JRL
Automation Wizard
Posts: 3505
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Fri Oct 19, 2007 8:25 pm

I would have thought that 0.625 would test greater than 0.5 but live and learn. Try multiplying the decimal values by 100 or 1000 or 10000 or whatever makes sense for your application then compare the integer values.

For example:

Let>num_1=0.625
Let>num_2=0.5
Let>num_1a=1000*%num_1%
Let>num_2a=1000*%num_2%
If>{%num_2a%<%num_1a%},LabelName

User avatar
JRL
Automation Wizard
Posts: 3505
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Fri Oct 19, 2007 8:30 pm

Actually I just tested and if the comparison is enclosed in braces 0.625 does test greater than 0.5. the following works for me.

Let>num_1=0.625
Let>num_2=0.5
If>{%num_2%<%num_1%},LabelName

User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Fri Oct 19, 2007 8:30 pm

It works for me:

Code: Select all

let>x=5/8
if>x>0.5
  MessageModal>%x% greater than 0.5
else
  MessageModal>%x% less than 0.5
endif
Now set x to a computation that results in less than 0.5 and it will jump correctly to the else statement
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

Kunutsu
Newbie
Posts: 5
Joined: Fri Oct 19, 2007 7:53 pm

Post by Kunutsu » Fri Oct 19, 2007 8:44 pm

hmm the result for your sourcecode is this:

http://img85.imageshack.us/my.php?image=cmpxo7.jpg

@JRL: of course you could do this, but its not a very good way to compare two floats. i would wonder if there is no better possibility
Last edited by Kunutsu on Fri Oct 19, 2007 8:46 pm, edited 1 time in total.

User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Fri Oct 19, 2007 8:46 pm

The issue is your system uses the comma (,) as the decimal separator, but you are comparing with a value using a period (.). You need to do this:

Code: Select all

let>x=5/8
if>x>0,5
   MessageModal>%x% greater than 0,5
else
  MessageModal>%x% less than 0,5
endif
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

Kunutsu
Newbie
Posts: 5
Joined: Fri Oct 19, 2007 7:53 pm

Post by Kunutsu » Fri Oct 19, 2007 8:50 pm

i already tried this, but it results in an error message:

http://img142.imageshack.us/my.php?imag ... msgna3.png

the message means something like "error - subroutine/label 5 not found!"

User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Fri Oct 19, 2007 8:59 pm

Sorry, my mistake, do this:

Code: Select all

let>x=5/8
Let>compare=0,5
if>x>compare
  MessageModal>%x% greater than 0,5
else
  MessageModal>%x% less than 0,5
endif
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

Kunutsu
Newbie
Posts: 5
Joined: Fri Oct 19, 2007 7:53 pm

Post by Kunutsu » Fri Oct 19, 2007 9:02 pm

yes this is working. :)

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts