FAQ  •  Register  •  Login

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

Moderator: Inside3D Admins

<<

Baker

User avatar

Posts: 3191

Joined: Tue Mar 14, 2006 5:15 am

Post Mon Mar 12, 2012 6:52 pm

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

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 ..
<<

Spike

Posts: 2084

Joined: Fri Nov 05, 2004 3:12 am

Location: UK

Post Mon Mar 12, 2012 7:35 pm

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

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

mankrip

User avatar

Posts: 509

Joined: Fri Jul 04, 2008 3:02 am

Post Tue Mar 13, 2012 2:15 am

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

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.
Thinking with slipgates.
=-=-=-=-=-=-=-=-=-=
Makaqu engine blog / website.
<<

mh

User avatar

Posts: 2172

Joined: Sat Jan 12, 2008 1:38 am

Post Tue Mar 13, 2012 2:22 am

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

And just for giggles:
  Code:
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...
Like the fifth day of playing 24-hour Scrabble when you don't want to use any letters because each one means a world to you because you're so deranged.
<<

Baker

User avatar

Posts: 3191

Joined: Tue Mar 14, 2006 5:15 am

Post Tue Mar 13, 2012 3:11 am

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

mh wrote:And just for giggles:
  Code:
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 ..

Return to Engine Programming

Who is online

Users browsing this forum: No registered users and 2 guests

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.
Icons provided by Aha Soft