if comparison perform differently between Lite-c, C-Script.

Posted By: Jethro

if comparison perform differently between Lite-c, C-Script. - 06/10/09 07:12

Just spent 4 weeks converting my 75,000 lines of C-script code base to Lite-c. and discovered the following, among many other things.

var This_Var;

This_Var=0.1;
if (This_Var==0.1){beep();}

The above works in C-script but not in Lite-C

vars are supposed to have an accuracy of 0.001, so why doesn't the above work in Lite-c.
To get it to work you need to make This_Var a float, to up the resolution of the comparison.
or do the comparison like this.
if (This_Var==floatv(0.1)) {beep();}

Is this a bug or a quirk with Lite-c var comparisons, having a much lower resolution than specified.
I put var This_Var in a panel digit and it does indeed keep its value of 0.100 so it does seem to be the comparison at fault.

Jethro.
Posted By: Tobias

Re: if comparison perform differently between Lite-c, C-Script. - 06/10/09 10:37

I think this is related to comparing float numbers with "==".

In programming course I learnt that due to limited computer precision, float numbers can not be compared with == or !=, but always with <, <=, >, >=.

This_Var=0.1;
if (This_Var > 0.0999) printf("Match!");
if (This_Var < 0.1001) printf("Match!");
Posted By: ventilator

Re: if comparison perform differently between Lite-c, C-Script. - 06/10/09 10:44

Quote:
so why doesn't the above work in Lite-c.


because in lite-c the literals are doubles not vars like in c-script.
Posted By: Tobias

Re: if comparison perform differently between Lite-c, C-Script. - 06/10/09 11:03

This would also work:

This_Var=0.1;
That_Var=0.1;

if (This_Var == That_Var) printf("Match!");

Still if a variable is not an integer, you should not compare it with ==.
Posted By: EvilSOB

Re: if comparison perform differently between Lite-c, C-Script. - 06/10/09 14:05

I think ventilator is right.
A regular workaround I use is
if(This_Var==(var)0.1) beep();
Posted By: Jethro

Re: if comparison perform differently between Lite-c, C-Script. - 06/11/09 03:07

Thanks guys for all your responses.
ventilators makes perfect sense, to what I was thinking must be the culprit.

I'll have to be more carefull now with lite-c.

Jethro.
Posted By: jcl

Re: if comparison perform differently between Lite-c, C-Script. - 06/11/09 07:26

We'll mention this in the manual.
© 2024 lite-C Forums