Type conversion

Hello!

I have a 2D vector of floats representing a position. It’s important that the numbers describing the position need to be rounded to integer values when I use them in drawing. Which method would be faster :

  1. convert floats to ints at the level of C code
iX = (int) fX;
iY = (int) fY;
  1. pass floats as arguments to glVertex2i() and hope that OpenGL does the rounding better than pure C does
glVertex2i(fX, fY)

Which one seems more efficient? Maybe there is some other - better - solution?

Thanks in advance! :slight_smile:

Assuming you use iX and iY from 1) and pass them to glVertex2i, then there’s no difference between the two. In the second code, the floats are converted to integers before they are passed to the function. So it’s just a matter of explicitly storing the temporary integer values (as in 1) or let the compiler deal with the temporary copies (as in 2). A decent optimizing compiler will generate the same code for both cases.

Putting it shortly - no difference. :slight_smile: That’s all I wanted to know.

Thanks!

Small nitpick: For negative vertex coordinates this might not be what you want.

From Microsoft’s C++ help:
“When an object of floating type is converted to an integral type, the fractional part is truncated. No rounding takes place in the conversion process. Truncation means that a number like 1.3 is converted to 1, and –1.3 is converted to –1.”