Recent

Author Topic: divide by zero  (Read 32881 times)

mbohn

  • Full Member
  • ***
  • Posts: 120
divide by zero
« on: October 13, 2009, 11:54:02 pm »
fpc 2.2.4
Lazarus 0.9.27
OSX 10.6.1

After two full days of trying to figure out (without the help of a debugger) why my computationally-intensive Delphi app was blowing up  I found that fpc doesn't catch a divide-by-zero error.  If the offending calc is inside a try..except block the exception never gets raised.  It will report +infinity as the result if you try to print out the results but this is less than useful, especially if your Delphi code has try..except blocks all over the place.

I've seen some discussion on the forum and wiki but not enough to understand why this is set up that way and if it is considered a bug.

Any insight or workarounds would be appreciated.

Mark

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: divide by zero
« Reply #1 on: October 14, 2009, 12:06:36 pm »
What's the type of of the expression? 1 div 0 (integer division) is indeed a EDivByZero exception, but 1 / 0 (floating point) is not. It leads to infinity as what you get.

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: divide by zero
« Reply #2 on: October 14, 2009, 01:23:51 pm »
AFAIK an integer div by zero should raise EDivByZero, whereas a floating point division by zero should raise EZeroDivide
See: http://docwiki.embarcadero.com/VCL/en/SysUtils.EZeroDivide
Quote
SysUtils.EZeroDivide exception is raised when an application attempts to divide a floating-point value by zero.
    Note:  Integer divide-by-zero errors raise the SysUtils.EDivByZero exception.

Simple test program:
Code: [Select]
var r,x: extended;
    i,j: integer;

begin
  i := 0;
  r := 0.0;
  try
    writeln('Integer div');
    j := 1 div i;
    writeln('j = ',j);
  except
    on e: exception do writeln(e.classname,' -> ',e.message);
  end;
  try
    writeln('Float divide');
    x := 1.0 / r;
    writeln('x = ',x);
  except
    on e: exception do writeln(e.classname,' -> ',e.message);
  end;
end.
Output using fpc 2.2.4 (Linux i386)
Code: [Select]
bart@simenon:~/svnroot/lazarus> ~/LazarusProjecten/ConsoleProjecten/project1
Integer div
EDivByZero -> Division by zero
Float divide
EDivByZero -> Division by zero

So my fpc in both cases raises an EDivByZero.
(or if you don't use try ..except in both cases a runtime error 200)

Can anyone test with recent Delphi?

Bart
« Last Edit: October 17, 2009, 01:25:01 pm by Bart »

mbohn

  • Full Member
  • ***
  • Posts: 120
Re: divide by zero
« Reply #3 on: October 14, 2009, 02:43:37 pm »
Here is my test project.  I have a form with two edit boxes and a button.  User puts any number into edit1, clicks the button and the reciprocal of that number is put into edit2.

procedure TForm1.Button1Click(Sender: TObject);
var x,y:double;
    s1:string;
begin
   x:=strtofloat(edit1.Text);
   try
   y:=1/x;
   s1:=  floattostrf(y,ffexponent,6,2)
   except
    s1:='can not calc';
   end;
   edit2.text:=s1;
end;       

If edit1 contains 0.0 edit2 displays +.nf.

I haven't tested it with Delphi because I've been trapping divide-by-zero errors with Delphi since V1.0 and it works. :D

Marc

  • Administrator
  • Hero Member
  • *
  • Posts: 2584
Re: divide by zero
« Reply #4 on: October 14, 2009, 03:59:53 pm »
iirc fp detection was disabled for OSX since internally it is used, but not handled by OSX itself. This resulted in a FP exception if you hovered over a menu for instance.

jonas might know more about it.

//--
{$I stdsig.inc}
//-I still can't read someones mind
//-Bugs reported here will be forgotten. Use the bug tracker

mbohn

  • Full Member
  • ***
  • Posts: 120
Re: divide by zero
« Reply #5 on: October 15, 2009, 04:46:20 pm »
Marc:
Should I submit this as a bug?

Marc

  • Administrator
  • Hero Member
  • *
  • Posts: 2584
Re: divide by zero
« Reply #6 on: October 16, 2009, 01:30:04 am »
iirc it is not fixable. What you can do is turn them on (set fpu mask in the Math unit)
//--
{$I stdsig.inc}
//-I still can't read someones mind
//-Bugs reported here will be forgotten. Use the bug tracker

mbohn

  • Full Member
  • ***
  • Posts: 120
Re: divide by zero
« Reply #7 on: October 16, 2009, 03:11:16 pm »
iirc it is not fixable. What you can do is turn them on (set fpu mask in the Math unit)
The math unit is not used by my test program but I went through it line-by-line anyway and found no flags.

With these unfixable OSX issues (divide-by-zero and the debugger) I'm beginning to have a problem with the "compile once run anywhere" concept.  Should I just install Ubuntu on my Mac?

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: divide by zero
« Reply #8 on: October 17, 2009, 11:18:44 am »
The fact that an floating point divide by zero raises an EDivByZero where it should raise an EZeroDivide should be considered a bug and reported then?

Bart
« Last Edit: October 17, 2009, 01:26:17 pm by Bart »

mbohn

  • Full Member
  • ***
  • Posts: 120
Re: divide by zero
« Reply #9 on: October 17, 2009, 02:16:00 pm »
Bart: What I said earlier was that it raises no exception at all. 

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: divide by zero
« Reply #10 on: October 18, 2009, 03:33:41 pm »
Bart: What I said earlier was that it raises no exception at all. 
Yeah, I know, but that seems MAC specific.
In Linux/Win it seems to raise the wrong exception.

Bart

mbohn

  • Full Member
  • ***
  • Posts: 120
Re: divide by zero
« Reply #11 on: October 18, 2009, 03:47:56 pm »
Yes, it is Mac specific.  So I'm wondering if I should switch to Linux.  My main app will eventually be deployed in Linux.  But I'd hate to move to Linux only to find there are fatal problems with that platform.

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: divide by zero
« Reply #12 on: October 19, 2009, 10:41:48 am »
You'll have to test it in Linux anyway.
Write once, compile everywhere is not yet always 100% perfect.

Bart

TurboRascal

  • Hero Member
  • *****
  • Posts: 672
  • "Good sysadmin. Bad programmer."™
Re: divide by zero
« Reply #13 on: October 28, 2009, 05:56:07 pm »
The fact that an floating point divide by zero raises an EDivByZero where it should raise an EZeroDivide should be considered a bug and reported then?

Bart


But should this be considered a bug at all? The only fault here is it doesn't closely follow Delphi behavior. Since Lazarus is declared as "not just a Delphi clone", perhaps this is a feature instead of a bug ;)
Regards, ArNy the Turbo Rascal
-
"The secret is to give them what they need, not what they want." - Scotty, STTNG:Relics

mbohn

  • Full Member
  • ***
  • Posts: 120
Re: divide by zero
« Reply #14 on: October 28, 2009, 06:36:44 pm »
The fact that an floating point divide by zero raises an EDivByZero where it should raise an EZeroDivide should be considered a bug and reported then?

Bart
But should this be considered a bug at all? The only fault here is it doesn't closely follow Delphi behavior. Since Lazarus is declared as "not just a Delphi clone", perhaps this is a feature instead of a bug ;)


If it departed from Delphi for all 3 operating systems, then maybe yes.  But it seems like it departs from Delphi only for OSX.

 

TinyPortal © 2005-2018