PDA

View Full Version : assigning an int to a float



geohoffman49431
07-20-2004, 09:44 PM
I have heard that it is a bad idea to repeatedly assign a float to an int like this:

float a = 1.0002;
int b = a;

The compiler will make an additional function call to _ftol(). This can really slow a program down. My source is Andre Lamothes "Tricks of the 3D Game Programming Gurus" chapter 16, page 1595. Is it ok to cast an integer to a float like this or will it slow a program down (this type of thing will get called many times each time I render the scene):

int a = 1;
float b = a;

another source: http://www.devx.com/Intel/Article/11541

Relic
07-21-2004, 02:31 AM
Loading an int as a float needs no special handling. The value range of int is included in the float range. The floating point processor has a special instruction to load an integer.
Your compiler will likely translate the
float b = a;
to two floating point assembly instructions doing the conversion:
FILD [a]
FSTP [B]
in the general case. Your example will not even need that.

geohoffman49431
07-21-2004, 03:28 AM
thanks