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 * "

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

Regards

D.

Reedbeta
—
2010-04-02T02:49:54Z —
#2

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.

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

Kenneth_Gorking
—
2010-04-02T08:12:48Z —
#4

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.

renton79
—
2010-04-02T13:33:10Z —
#5

Thanks so much for your help guys

renton79
—
2010-04-02T20:35:49Z —
#6

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

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

Reedbeta
—
2010-04-02T21:06:47Z —
#7

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.