Board index » delphi » Floating point error

Floating point error

Hi!

Just wondered if anyone know how to trap floating point errors (overflow etc.) under
BTP7.0 without hooking interrupts.

/P?r

 

Re:Floating point error


On Sun, 14 Jun 1998 16:04:47 +0200, "P?r Spjuth"

Quote
<per.spj...@karlskrona.mail.telia.com> wrote:
>Just wondered if anyone know how to trap floating point errors (overflow etc.) under
>BTP7.0 without hooking interrupts.

The only way is to mask floating point exceptions and to check for an
exception after every suspicious operation. To achieve this you have
to load a control word into the FPU which will mask every exception.

Here is a example which checks "by hand" for the relevant FPU
exceptions:

Quote
>---------------------------------------------<

{$N+}
const
  FP_INVALID    = 1;
  FP_ZERODIVIDE = 4;
  FP_OVERFLOW   = 8;
  FP_ERROR =  FP_INVALID or FP_ZERODIVIDE or FP_OVERFLOW;
  DEFCW:word=$037F;

function getfperror:word;
{ get relevant FPU errors and clear exceptions }
var sw:word;
begin
  asm fstsw sw end;
  getfperror:=sw and FP_ERROR;
  asm fclex end
end;

procedure disperror;
var
  error:word;
begin
  error:=getfperror;
  if error = 0
    then writeln('OK')
  else begin
    if error and FP_INVALID <> 0
      then writeln('Invalid operation');
    if error and FP_ZERODIVIDE <> 0
      then writeln('Division by 0');
    if error and FP_OVERFLOW <> 0
      then writeln('Division by 0');
  end
end;

var
  d,e:double;
begin
{ load default control word }
  asm fldcw DEFCW end;
  d:=0; e:=1/d;
  disperror;
  d:=1; e:=1/d;
  disperror;
  d:=ln(0);
  disperror;
  readln
end.

Quote
>---------------------------------------------<

Regards
Horst

Other Threads