Main Archive Specials Wiki | FAQ Links Submit Forum

Double to Int

Type : pointer cast (round to zero, or 'trunctate')
References : Posted by many people, implementation by Andy M00cho

Notes :
-Platform independant, literally. You have IEEE FP numbers, this will work, as long as your not expecting a signed integer back larger than 16bits :)
-Will only work correctly for FP numbers within the range of [-32768.0,32767.0]
-The FPU must be in Double-Precision mode

Code :
typedef double lreal;
typedef float real;
typedef unsigned long uint32;
typedef long int32;

//2^36 * 1.5, (52-_shiftamt=36) uses limited precision to floor
//16.16 fixed point representation

const lreal _double2fixmagic = 68719476736.0*1.5;
const int32 _shiftamt = 16;

#if BigEndian_
#define iexp_ 0
#define iman_ 1
#define iexp_ 1
#define iman_ 0
#endif //BigEndian_

// Real2Int
inline int32 Real2Int(lreal val)
val= val + _double2fixmagic;
return ((int32*)&val)[iman_] >> _shiftamt;

// Real2Int
inline int32 Real2Int(real val)
return Real2Int ((lreal)val);

For the x86 assembler freaks here's the assembler equivalent:

__double2fixmagic dd 000000000h,042380000h

fld AFloatingPoint Number
fadd QWORD PTR __double2fixmagic
fstp TEMP
movsx eax,TEMP+2


Added on : 28/01/07 by martin[ DOT ]eisenberg[ AT ]udo[ DOT ]edu
Comment : credits one Sree Kotay for this code.

Added on : 11/07/07 by rasmus[ AT ]3kings[ DOT ]dk
Comment :
On PC this may be faster/easier:

int ftoi(float x)
  int res;
    fld x
    fistp res
  return res;

int dtoi(double x)
  return ftoi((float)x);

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


Are you human?

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