Division by zero

General FreeBASIC programming questions.
SotSvart
Posts: 262
Joined: May 27, 2005 9:03
Location: Norway
Contact:

Division by zero

Postby SotSvart » May 22, 2006 23:15

This gives an runtime error when compiled with -exx

Code: Select all

Dim As Double test, test2

test2 = 0
test  = 30 \ test2


But this just continiues

Code: Select all

Dim As Double test, test2

test2 = 0
test  = 30 / test2


Should'nt they both give an runtime error?
v1ctor
Site Admin
Posts: 3799
Joined: May 27, 2005 8:08
Location: SP / Bra[s]il
Contact:

Postby v1ctor » May 23, 2006 0:49

Floating-point operations are masked to not generate exceptions, while that can be enabled, if i remember, a degrade in the fpu performance can happen in some (old) CPU's.
Thrawn89
Posts: 477
Joined: Oct 08, 2005 13:12

Postby Thrawn89 » May 23, 2006 1:14

On that note:

Code: Select all

/ = floating point divide
\ = integer divide


Just in case ;-)

Thrawn
SotSvart
Posts: 262
Joined: May 27, 2005 9:03
Location: Norway
Contact:

Postby SotSvart » May 26, 2006 0:38

Thrawn: Yeah I have picked this up after about 10 years with QB(and later FB) ;)

V1c: If it would slowdown normaly compiled exe's then just forgett it, but it could be usefull if it only slowed down the code when compiled with -exx?
MichaelW
Posts: 3500
Joined: May 16, 2006 22:34
Location: USA

Postby MichaelW » May 26, 2006 2:05

Or you could just temporarily enable the relevant exception in your code. Quick and dirty example:

Code: Select all

option explicit

dim as double d1,d2
dim as ushort oldfpucw, fpucw, fpustatus

asm fstsw [fpustatus]
print "fpustatus = ";hex(fpustatus);"h"
asm fstcw [oldfpucw]
print "fpucw = ";hex(oldfpucw);"h"

print "zero divide..."
sleep
d1 = 1 / d2

asm fstsw [fpustatus]
print "fpustatus = ";hex(fpustatus);"h"

print "clear exceptions..."
sleep
asm fclex
asm fstsw [fpustatus]
print "fpustatus = ";hex(fpustatus);"h"

asm fstcw [fpucw]
print "fpucw = ";hex(fpucw);"h"

print "clear zero divide mask..."
sleep

' Clear Zero Divide Mask (bit2 of control word).
asm
    fstcw [fpucw]
    fwait
    mov ax, [fpucw]
    and ax, not 4
    push eax
    fldcw [esp]
    pop eax
end asm

asm fstcw [fpucw]
print "fpucw = ";hex(fpucw);"h"

print "Zero divide exception next..."
sleep

d1 = 1 / d2

' Restore old control word (if we get this far).
asm fldcw [oldfpucw]

sleep
Dirk_Fist
Posts: 82
Joined: Mar 07, 2006 11:28

Postby Dirk_Fist » May 27, 2006 8:30

Code: Select all

' whats wrong with ?

dim as double INF= 1/0.0
dim as double x,y

if y<>0 then x=1536/y else ?"divide by zero"

'or

x=1536/y : if x=INF then ?"divide by zero"
SotSvart
Posts: 262
Joined: May 27, 2005 9:03
Location: Norway
Contact:

Postby SotSvart » May 28, 2006 23:08

Dirk_Fist: Your code works fine, but it would be easier to have the error checking do this automaticaly when the code is compiled with error checking(-exx), then you wont have to add a division by zero check after all divisions in the code. The division by zero error in my code was coused by a bug in my code, if I know some code sometimes can cause a divison by zero, then I offcourse would add a check to prevent this.

Though this is not a big problem, its only my 2 NKR.

Edit:
MichaelW: Your hack would also do the job =)

Return to “General”

Who is online

Users browsing this forum: No registered users and 6 guests