freebasic.net Forum Index
FreeBASIC's Official Forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister   ProfileProfile   Log inLog in

Divison by zero?

 
Post new topic   Reply to topic    freebasic.net Forum Index -> General
View previous topic :: View next topic  
Author Message
vins

PostPosted: Jul 01, 2005 17:25    Post subject: Divison by zero? Reply with quote

Does someone know how to detect a division by zero. Or make this kind of division always return 0.

When I wrote this:

Code:

Declare Function Div(a1 As Integer, b1 As Integer) As Integer
Dim Res As Integer

Res = 1 / 0
Print Res
Print Div(1,0)
Sleep


Function Div(a As Integer, b As Integer) As Integer
    If b = 0 Then
     Print "Division by zero"
    End If
   
    If b <> 0 Then Return a / b
End Function
 


..i've got this as output:

Quote:

-2147483648
Division by zero
0


The first line is a result form the division by FB.
The second line is a result from a function that checks for a division by zero. This function is not good for big formulas.
Isn't it a good idea of adding this to the ON ERROR statement or adding a Warning message in the compiler?

Excuse me if I made any mistakes but I am not very good at basic.
BTW: For example all other languages makes exceptions, error messages or stop the program when a division by zero is found.
 
Back to top
View user's profile Send e-mail Visit poster's website
Florent

PostPosted: Jul 01, 2005 19:59    Post subject: Reply with quote

Compatibility is good and all but what I'd like to see is support for SEH (Structured Exception Handling) - Would make things less unwieldy than ON ERROR RESUME to my way of thinking.
 
Back to top
View user's profile
Antoni
Master
PostPosted: Jul 01, 2005 20:20    Post subject: Reply with quote

The problem vins exposes is not about error handling, it is the error is not detected at all.
I did a couple of tests. A floating point division by zero returns 1.#INF, that's an error result, but integer division returns a valid value. No one sets the ERR variable so no error handling can be used, it just goes unnoticed.
 
Back to top
View user's profile Visit poster's website
mjs
Site Admin
PostPosted: Jul 01, 2005 21:12    Post subject: Reply with quote

This might be a dumb question but ... did you specify "-e" when you called the compiler?

Regards,
Mark
 
Back to top
View user's profile
dumbledore

PostPosted: Jul 01, 2005 23:08    Post subject: Reply with quote

i tried it with "-ex" and "-e" and it didn't have a error...
 
Back to top
View user's profile Visit poster's website
Antoni
Master
PostPosted: Jul 01, 2005 23:59    Post subject: Reply with quote

-e enables QB-like errors, but ERR is supposed to always work.
If you check the table in my Error handling article at the wiki, you will see there is no error number for the Division by 0. I got hte table from the source.

It's simply not implemented yet. The processor may give an error, but it's ignored.
 
Back to top
View user's profile Visit poster's website
v1ctor
Site Admin
PostPosted: Jul 02, 2005 22:15    Post subject: Reply with quote

No kind of exceptions is checked by now, care must be taken to not screw up with the OO exceptions handling when added later.
 
Back to top
View user's profile Visit poster's website MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    freebasic.net Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum



sf.net phatcode