June 14, 2024, 03:16:29 AM

## News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!

## truncating floating point

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

0 Members and 1 Guest are viewing this topic.

#### Ionic Wind Support Team

##### 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]#endasmreturn 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]#endasmreturn ret;}`
Ionic Wind Support Team

#### GWS

##### April 15, 2006, 01:48:11 AM #1
Just wondering .. is that the equivalent of 'Floor' ..

Graham
Tomorrow may be too late ..

#### Ionic Wind Support Team

##### April 15, 2006, 07:54:10 AM #2
Floor works differently with negative numbers.

Ionic Wind Support Team

#### GWS

##### April 15, 2006, 09:52:13 AM #3
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 ..