Forum

Discrete verus Continuous (why int <--> float isn't 1:1)

Discuss programming topics for the various GPL'd game engine sources.

Moderator: InsideQC Admins

Discrete verus Continuous (why int <--> float isn't 1:1)

Postby Baker » Mon Mar 12, 2012 6:52 pm

I've been doing some things lately that have involved converting between ...

1) Integer: screen coordinates in pixels - the desktop or image operations
2) Float: screen coordinates in OpenGL

As I was doing this, I got figuratively slapped in the face with a fish. I quickly solve the issue, but was funny.

Here goes:

Question: What is center of integer range 25 to 225 when converted to float?
Answer: It isn't 125. Its 125.5

An integer is a discrete number. If you have an integer range from 25 to 225, all of 225 including 225.3 and 225.75 fall into that range. Therefore the size of that range is 201 in integer terms, but 200 in floating point terms.

But floats are continuous and a floating point range of 25 to 225 does not include 225.000001.
The night is young. How else can I annoy the world before sunsrise? 8) Inquisitive minds want to know ! And if they don't -- well like that ever has stopped me before ..
User avatar
Baker
 
Posts: 3666
Joined: Tue Mar 14, 2006 5:15 am

Re: Discrete verus Continuous (why int <--> float isn't 1:1)

Postby Spike » Mon Mar 12, 2012 7:35 pm

inclusive or exclusive?
the integer ranges you describe are inclusive
the float ranges you describe are exclusive with +1 to the max.
Spike
 
Posts: 2892
Joined: Fri Nov 05, 2004 3:12 am
Location: UK

Re: Discrete verus Continuous (why int <--> float isn't 1:1)

Postby mankrip » Tue Mar 13, 2012 2:15 am

I remember figuring out that I'd need at least three different ways of converting between both types when working on my own Java-based engine from scratch. I also remember that one of them was impossible to do without recursion.
Ph'nglui mglw'nafh mankrip Hell's end wgah'nagl fhtagn.
==-=-=-=-=-=-=-=-=-=-==
Dev blog / Twitter / YouTube
User avatar
mankrip
 
Posts: 915
Joined: Fri Jul 04, 2008 3:02 am

Re: Discrete verus Continuous (why int <--> float isn't 1:1)

Postby mh » Tue Mar 13, 2012 2:22 am

And just for giggles:
Code: Select all
float f = 225.000001f;

Break on the statement after that in your debugger. What value does f have? Hint: it might not be 225.000001f...
We had the power, we had the space, we had a sense of time and place
We knew the words, we knew the score, we knew what we were fighting for
User avatar
mh
 
Posts: 2292
Joined: Sat Jan 12, 2008 1:38 am

Re: Discrete verus Continuous (why int <--> float isn't 1:1)

Postby Baker » Tue Mar 13, 2012 3:11 am

mh wrote:And just for giggles:
Code: Select all
float f = 225.000001f;

Break on the statement after that in your debugger. What value does f have? Hint: it might not be 225.000001f...


Heh. I haven't tried, but your message is that floating point precision is "scale limited". I might have gotten away with .000001 f but by adding the 225 part to it I have killed the precision because it only spans a certain "number of digits".

Not really what my point was, but yeah I understand your critique. I should have said 225.001 :D

Spike wrote:inclusive or exclusive?
the integer ranges you describe are inclusive
the float ranges you describe are exclusive with +1 to the max.


Inclusive. I was dealing with a range of pixels. In the range/out of the range Yes | No.

mankrip wrote:I also remember that one of them was impossible to do without recursion.


Ok, I'm not sure I want to experience whatever that ^^^ is.
Last edited by Baker on Tue Mar 13, 2012 3:14 am, edited 1 time in total.
The night is young. How else can I annoy the world before sunsrise? 8) Inquisitive minds want to know ! And if they don't -- well like that ever has stopped me before ..
User avatar
Baker
 
Posts: 3666
Joined: Tue Mar 14, 2006 5:15 am


Return to Engine Programming

Who is online

Users browsing this forum: No registered users and 1 guest