Round float to int

32f6d05912ce22b4a6ae9ad85a7e45af
0
renton79 101 Apr 02, 2010 at 02:13

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.

8 Replies

Please log in or register to post a reply.

A8433b04cb41dd57113740b779f61acb
0
Reedbeta 167 Apr 02, 2010 at 02:49

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.

32f6d05912ce22b4a6ae9ad85a7e45af
0
renton79 101 Apr 02, 2010 at 03:19

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.net/archives/The_Art_of_Demomaking-Issue_03_Timer_Related_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 ???

46407cc1bdfbd2db4f6e8876d74f990a
0
Kenneth_Gorking 101 Apr 02, 2010 at 08:12

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.

32f6d05912ce22b4a6ae9ad85a7e45af
0
renton79 101 Apr 02, 2010 at 13:33

Thanks so much for your help guys

32f6d05912ce22b4a6ae9ad85a7e45af
0
renton79 101 Apr 02, 2010 at 20:35

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

A8433b04cb41dd57113740b779f61acb
0
Reedbeta 167 Apr 02, 2010 at 21:06

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.

32f6d05912ce22b4a6ae9ad85a7e45af
0
renton79 101 Apr 02, 2010 at 21:25

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

E068667c631d2d1ee2b02bc95bdb4e4d
0
flordeliza 101 Jun 05, 2010 at 10:43

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