# Round float to int

8 replies to this topic

### #1renton79

New Member

• Members
• 8 posts

Posted 02 April 2010 - 02:13 AM

this is such a basic question I know and I am sorry, but I don't know C at all and there I found this piece of code with rounding float to the nearest integer with sub pixel accuracy which is this " 16.0f * "

------------- Cited Code -------------
// 28.4 fixed-point coordinates
const int Y1 = iround(16.0f * v1.y);
const int Y2 = iround(16.0f * v2.y);
const int Y3 = iround(16.0f * v3.y);
--------------------------------------

Could someone explain to me what exactly means "16.0f" I tried to Google it but without any luck.

Regards
D.

### #2Reedbeta

DevMaster Staff

• 5307 posts
• LocationBellevue, WA

Posted 02 April 2010 - 02:49 AM

Like the comment says it's converting to 28.4 fixed-point. This means the 32-bit integer is considered to have 28 bits left of the radix point and 4 bits to the right. Another way to say it is that it's counting the coordinates in units equal to 1/16th of a pixel (2^4 = 16). So the float pixel values are multiplied by 16 first. You can google for fixed point if you're not familiar with it.
reedbeta.com - developer blog, OpenGL demos, and other projects

### #3renton79

New Member

• Members
• 8 posts

Posted 02 April 2010 - 03:19 AM

I have asked incorrect question, what I want to know is:

is 16.0f equal to 16?

as in language I code it doesn't recognize 16.0f

I have found here: http://www.flipcode....ed_Issues.shtml
that "Note that 256.0f = 2^8, because we have a 8 bit fractional part"

So I assume that 16.0f == 2^4 which is simply 16, so if I make
const int Y1 = iround(16 * v1.y);

should be correct, am I right ???

### #4Kenneth Gorking

Senior Member

• Members
• 939 posts

Posted 02 April 2010 - 08:12 AM

16.0f is just a floating-point constant (hence the 'f' at the end), where 16 is an integer constant. You can change the 16.0f to just 16, but the compiler will convert it to a floating-point number internally when the code is compiled, assuming that v1.y is a fp number.
"Stupid bug! You go squish now!!" - Homer Simpson

### #5renton79

New Member

• Members
• 8 posts

Posted 02 April 2010 - 01:33 PM

Thanks so much for your help guys

### #6renton79

New Member

• Members
• 8 posts

Posted 02 April 2010 - 08:35 PM

I know what it is the f at the end of 16.0f but it just doesn't give me a rest as

16.0f == 16.00 == 16

16 * 1.1 = 17.6 and 16.00 * 1.1 = 17.6 this is still the same
or
16 * 1 = 16 and 16.00 * 1 = 16

So what is the whole point of telling the compiler that number 16 is a float as it doesn't make any difference???

### #7Reedbeta

DevMaster Staff

• 5307 posts
• LocationBellevue, WA

Posted 02 April 2010 - 09:06 PM

Well, assuming v1.y and so forth are floats, it doesn't make a difference in this particular case because the compiler will promote ints to floats anytime an int and a float are added/multiplied/whatever.

But before any promotion is done, in C/C++ 16 is an int, 16.0f is a float, and 16.0 is a double.

So, 3 / 2 == 1, because 3 and 2 are ints so the compiler does integer division (truncates the fractional part). But 3.0f / 2.0f == 1.5f. Also 3.0f / 2 or 3 / 2.0f give 1.5f, because the compiler promotes the int to a float.
reedbeta.com - developer blog, OpenGL demos, and other projects

### #8renton79

New Member

• Members
• 8 posts

Posted 02 April 2010 - 09:25 PM

I've got the whole idea now, Reedbeta.
Thanks again

### #9flordeliza

New Member

• Members
• 2 posts

Posted 05 June 2010 - 10:43 AM

well this is a big help thank u so much guys !!

#### 1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users