Comparing two floats

Non-platform specific questions.

Comparing two floats

Postby lverona » Wed May 06, 2015 2:06 pm

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.
lverona
 
Posts: 221
Joined: Tue Apr 24, 2012 11:54 am
Score: 1 Give a positive score

Re: Comparing two floats

Postby koala » Wed May 06, 2015 3:28 pm

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.
Phascolarctos cinereus
YouTube: Marko Radivojevic
Google+: Marko Radivojevic
User avatar
koala
 
Posts: 301
Joined: Thu Mar 26, 2015 7:03 pm
Location: Serbia
Score: 30 Give a positive score

Re: Comparing two floats

Postby lverona » Wed May 06, 2015 3:45 pm

Hey koala!

Thank you very much! Simple trick and very neat! I used it, solved my problem so far! Thank you very much!
lverona
 
Posts: 221
Joined: Tue Apr 24, 2012 11:54 am
Score: 1 Give a positive score

Re: Comparing two floats

Postby koala » Wed May 06, 2015 4:19 pm

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:
Phascolarctos cinereus
YouTube: Marko Radivojevic
Google+: Marko Radivojevic
User avatar
koala
 
Posts: 301
Joined: Thu Mar 26, 2015 7:03 pm
Location: Serbia
Score: 30 Give a positive score

Re: Comparing two floats

Postby lverona » Wed May 06, 2015 4:30 pm

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!
lverona
 
Posts: 221
Joined: Tue Apr 24, 2012 11:54 am
Score: 1 Give a positive score

Re: Comparing two floats

Postby koala » Wed May 06, 2015 4:50 pm

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? :?: :!: :?
Phascolarctos cinereus
YouTube: Marko Radivojevic
Google+: Marko Radivojevic
User avatar
koala
 
Posts: 301
Joined: Thu Mar 26, 2015 7:03 pm
Location: Serbia
Score: 30 Give a positive score

Re: Comparing two floats

Postby lverona » Wed May 06, 2015 5:25 pm

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.
lverona
 
Posts: 221
Joined: Tue Apr 24, 2012 11:54 am
Score: 1 Give a positive score

Re: Comparing two floats

Postby bat78 » Wed Jul 29, 2015 2:19 am

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.
The future of "Game-Editor" here
User avatar
bat78
 
Posts: 816
Joined: Sun Dec 14, 2008 9:13 pm
Location: Bulgaria, Sofia
Score: 88 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest

cron