Page 1 of 1

Comparing two floats

PostPosted: Wed May 06, 2015 2:06 pm
by lverona
As you guys know, comparing floats can be very difficult. In my program I need to compare two floats. I could've used things like floorf, but no such functions are recognized by the game editor. Is there a way to go around that and compare two floats?

I need to compare numbers which have two decimal places. Like 2.5 and 2.5. If those are floats, the == almost never will work.

So far I have no idea how to get over that in gE.

Re: Comparing two floats

PostPosted: Wed May 06, 2015 3:28 pm
by koala
lverona wrote:As you guys know, comparing floats can be very difficult. In my program I need to compare two floats. I could've used things like floorf, but no such functions are recognized by the game editor. Is there a way to go around that and compare two floats?

I need to compare numbers which have two decimal places. Like 2.5 and 2.5. If those are floats, the == almost never will work.

So far I have no idea how to get over that in gE.
Here, try this:
Code: Select all
int cmpfloat(float alfa, float beta) {
    int alfa_int, beta_int;

    alfa_int  = alfa * 1000;
    beta_int  = beta * 1000;

    if(alfa_int < beta_int) return -1;
    else if (alfa_int == beta_int) return 0;
    else return 1;
}
It is for floats with 4 places.

Re: Comparing two floats

PostPosted: Wed May 06, 2015 3:45 pm
by lverona
Hey koala!

Thank you very much! Simple trick and very neat! I used it, solved my problem so far! Thank you very much!

Re: Comparing two floats

PostPosted: Wed May 06, 2015 4:19 pm
by koala
lverona wrote:Hey koala!

Thank you very much! Simple trick and very neat! I used it, solved my problem so far! Thank you very much!
No problem! :mrgreen:

Re: Comparing two floats

PostPosted: Wed May 06, 2015 4:30 pm
by lverona
It does show weird float behavior. Like, the int will be 2900, then 2800, then suddenly 2699 %)

I did not know there was such a problem with floats!

Re: Comparing two floats

PostPosted: Wed May 06, 2015 4:50 pm
by koala
lverona wrote:It does show weird float behavior. Like, the int will be 2900, then 2800, then suddenly 2699 %)

I did not know there was such a problem with floats!
Float changes value? Why? :?: :!: :?

Re: Comparing two floats

PostPosted: Wed May 06, 2015 5:25 pm
by lverona
Because it is float. It is imprecise. At least, this is my understanding. And when I convert it to int by multiplying it by 1000, it looses accuracy even more, I think.

I have solved this problem by using round()
In my app I do not need that level of precision. So 2699 turns into 2700.

Re: Comparing two floats

PostPosted: Wed Jul 29, 2015 2:19 am
by bat78
Well that's absolutely not true.
== works for floating-point numbers just fine.
Note that both the rvalue and the lvalue must be of the same type, float not double.

The other way is to implement the fabs function, since gE's math library doesn't provide it for some reason.
Code: Select all
unsigned double fabs(signed double n) { return((n >= 0.0) ? (n) : (0.0 - n)); }

And a function to take advantage of it, something like:
Code: Select all
int fequal (float a, float b, float epsilon) { return fabs(a-b) < epsilon; }

will return 1 if a equals b with epsilon of 1/1048576
____________________________________________________________________________________________________________________________________________
Ehh, round just rounds it up as if you take the integer part with modf or type-casting to an integral type
and it looses the point of being a floating-point number..
____________________________________________________________________________________________________________________________________________
The other way is to compare it with a number with 0s equal to the number of signs your float consists of.
And this is a lot like Koala's approach, however his won't work with doubles and thus with higher precision.