[futurebasic] Re: [FB] Divide by Zero error

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : June 2009 : Group Archive : Group : All Groups

From: Robert Purves <listrp@...>
Date: Sun, 7 Jun 2009 22:04:52 +1200
Walter Lenk wrote:

> I wrote a little program that tested the 3 various methods of  
> division using the formula A = B / C, where B = 10, C = 0, and STR$ 
> (A) was printed to the screen. I compiled the program the 3  
> different ways, and the PPC code was run on both machines, with the  
> Intel code only being run on the Intel machine.

> The results were:
>      Formula   FB PPC  FB2C PPC  FB2C INTEL
> 1. A% = B%/C%       0         0   >CRASHES<
> 2. A% = B%\C%  2^31-1        -1           0
> 3. A# = B#/C#       0    ' inf'      ' inf'

> Although 10/0 is not defined and therefore best avoided, it would be  
> great if the response to this programming blunder would be  
> consistent - rather tan 4 different answers and a crash.

Line 1. According to the C language specification, the outcome of  
integer division by zero is 'undefined'. PPC processors give 0 as the  
result and nothing else happens. Intel processors generate an  
exception (a software interrupt). There seems no practicable way to  
intercept the exception and return a specific value (such as 0), thus  
a crash is inevitable.

Line 3. For fp division by zero, IEEE754 specifies a unique bit  
pattern as the result. In C and FBtoC, conversion of this pattern to a  
string for display gives 'inf'. For unknown reasons the default  
behaviour of the old FB Compiler was to produce the string '0'.

Calculations with inf values proceed according to defined rules.
inf*10  --> inf
inf/10  --> inf
1/inf   --> 0
inf/inf --> nan  [not-a-number]


Line 2. B and C are converted to fp, and the division produces inf,  
which is then converted to an integer. The result of converting inf to  
an integer value is undefined by the C language spec. The resulting  
value differs between PPC and Intel. (It also depends whether the  
target integer is signed or unsigned).

All very unsatisfactory, I agree.

Robert P.