Main Archive Specials Wiki | FAQ Links Submit Forum


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


Added on : 11/06/02 by 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.




Added on : 28/05/03 by 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
}              




Added on : 06/04/04 by kusma[ AT ]nowhere[ DOT ]com
Comment :
if you use msvc, just use the /QIfist compile-switch



Add your own comment
Comments are displayed in fixed width, no HTML code allowed!
Email:

Comment:

Are you human?



Site created and maintained by Bram
Graphic design by line.out | Server sponsered by fxpansion