Float to int

References : Posted by Ross Bencina
Notes :
intel only
Code :
int truncate(float flt)
{
  int i;
  static const double half = 0.5f;
  _asm
  {
     fld flt
     fsub half
     fistp i
  }
  return i
}

Comments
from : jaha[AT]smartelectronix[DOT]com
comment : Note: Round nearest doesn't work, because the Intel FPU uses Even-Odd rounding in order to conform to the IEEE floating-point standard: when the FPU is set to use the round-nearest rule, values whose fractional part is exactly 0.5 round toward the nearest *even* integer. Thus, 1.5 rounds to 2, 2.5 rounds to 2, 3.5 rounds to 4. This is quite disastrous for the FLOOR/CEIL functions if you use the strategy you describe.

from : sugonaut[AT]yahoo[DOT]com
comment : This version below seems to be more accurate on my Win32/P4. Doesn't deal with the Intel FPU issue. A faster solution than c-style casts though. But you're not always going to get the most accurate conversion. Like the previous comment; 2.5 will convert to 2, but 2.501 will convert to 3. int truncate(float flt) { int i; _asm { fld flt fistp i } return i }

from : kusma[AT]nowhere[DOT]com
comment : if you use msvc, just use the /QIfist compile-switch