Comparing two floats
Moderators: Dorian (MJT support), JRL
Comparing two floats
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?
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?
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
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
- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
It works for me:
Now set x to a computation that results in less than 0.5 and it will jump correctly to the else statement
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?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
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
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.
- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
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?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
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!"
http://img142.imageshack.us/my.php?imag ... msgna3.png
the message means something like "error - subroutine/label 5 not found!"
- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
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?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?