IonicWind Software

Aurora Compiler => Tips and Tricks => Topic started by: Ionic Wind Support Team on April 13, 2006, 10:26:52 PM

Title: truncating floating point
Post by: Ionic Wind Support Team on April 13, 2006, 10:26:52 PM
Just a couple of subs I use when I want to truncate a floating point number instead of rounding.  During an assignment such as:

int  i = 1.77f;

The default FPU mode used by Aurora is a round to the nearest whole number which results in 2.

int i = ftoint(1.77f);

Places the FPU in truncate mode before converting and the result is 1.  This is the equivelent code generated in a cast to int in C

// int i = (int)1.77f;




sub dtoint(double d),int64
{
word cw;
word cw2;
int64 ret;
#asm
lea eax,[ebp]
fstcw [eax-4]
mov word [eax-8],0x0F3F
fldcw [eax-8]
fld qword [eax+8]
fistp qword [eax-16]
fldcw [eax-4]
#endasm
return ret;
}

sub ftoint(float f),int
{
word cw;
word cw2;
int ret;
#asm
lea eax,[ebp]
fstcw [eax-4]
mov word [eax-8],0x0F3F
fldcw [eax-8]
fld dword [eax+8]
fistp dword [eax-12]
fldcw [eax-4]
#endasm
return ret;
}
Title: Re: truncating floating point
Post by: GWS on April 15, 2006, 01:48:11 AM
Just wondering .. is that the equivalent of 'Floor' .. :)

Graham
Title: Re: truncating floating point
Post by: Ionic Wind Support Team on April 15, 2006, 07:54:10 AM
Floor works differently with negative numbers.

Title: Re: truncating floating point
Post by: GWS on April 15, 2006, 09:52:13 AM
Ah yes .. it's like Floor for +ve numbers, and like Ceil for -ve ones ..ÂÃ,  :)

I always have to think a bit about negative numbers.

Graham