Posts: 66
Threads: 19
Joined: May 2015
I have been coding for many years with QM but I am baffled why the "or" statement (just below the ??????) in the code below does not work when the variable "multiple=5.15" in the code below (excuse the rough coding) Note the "or" statement does work when "totalmoverequired>dailymove"
Note as multiple starts at 0.95 and then increases by 0.35 one each loop it does eventually = 5.15 but the "or" statement does not recognise this.
I am missing something obvious - I have stared at this for days and would appreciate a fresh pair of eyes. Thank you
Posts: 12,073
Threads: 140
Joined: Dec 2002
this simplified code works
Macro
Macro1053
out
double multiple=0.95
double totalmoverequired=1
double dailymove=1.5
rep 100
,out multiple
,if totalmoverequired>dailymove or multiple>=5.15
,,out "yes"; break
,out "no"
,multiple=multiple+0.35
Posts: 66
Threads: 19
Joined: May 2015
Thank you.
The simplified code should out "yes" when multiple=5.15 but it out "no"
It only out "yes" when multiple=5.5 which is wrong as the condition is "or multiple>=5.15" not "or multiple>5.15"
Is this a bug or am I missing something?
Posts: 12,073
Threads: 140
Joined: Dec 2002
It is a problem with floating-point numbers in any programming language. Not all numbers can be stored with exact value. For example, how to store numbers with infinite number of digits after the decimal point, for example the result of 1/3 is 0.3333333333333333333333333333333333333333333333333333333333333333333333333333333333....... Operations with double numbers then produce the nearest number that can be stored in the variable. After one or several such operations the result can become incorrect, therefore the >= operator does not work as expected.
Macro
Macro1053
out
double multiple=0.95
rep 100
,;out multiple
,out "%.20f" multiple ;;shows the number with higher precision than 'out multiple'
,if multiple>=5.15
,,out "yes"; break
,out "no"
,multiple=multiple+0.35
;You see, the number then is 5.1499999999999995 instead of 5.15.
;Even the initial number cannot be stored in the variable. Instead of 0.95 it is 0.94999999999999996.
Posts: 66
Threads: 19
Joined: May 2015
Gintaras you are a genius.
Thank you for your help ........I was gong mad trying to solve this. Now I understand.