October 09, 2024, 04:28:51 PM

News:

IWBasic runs in Windows 11!


truncating floating point

Started by Ionic Wind Support Team, April 13, 2006, 10:26:52 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Ionic Wind Support Team

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;
}
Ionic Wind Support Team

GWS

Just wondering .. is that the equivalent of 'Floor' .. :)

Graham
Tomorrow may be too late ..

Ionic Wind Support Team

Floor works differently with negative numbers.

Ionic Wind Support Team

GWS

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
Tomorrow may be too late ..