Division by 0.0 allowed

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Dr. Dri
Enthusiast
Enthusiast
Posts: 243
Joined: Sat Aug 23, 2003 6:45 pm

Division by 0.0 allowed

Post by Dr. Dri »

with this code i get an error at run time

Code: Select all

l.l = 0
m.l = 1 / l
but not with this one

Code: Select all

f.f = 0.0
g.f = 1.0 / f
but still i can't do that because the compiler says that division by zero is forbidden even for floats

Code: Select all

g.f = 1.0 / 0.0
is it possible to remove this check ?

Dri
michaeled314
Enthusiast
Enthusiast
Posts: 340
Joined: Tue Apr 24, 2007 11:14 pm

Post by michaeled314 »

You're crazy if you think you will get a result for a divide by zero!
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

1/0 is undefined. So allowing it makes no sense.

Edit: Rrrrrr too late for the second time today.
User avatar
Guimauve
Enthusiast
Enthusiast
Posts: 742
Joined: Wed Oct 22, 2003 2:51 am
Location: Canada

Post by Guimauve »

:shock: Divide by zero ...

It's a joke or ...

The divide by zero without error message it's a MAJOR bug. The best way to built your code is this case is :

Code: Select all

Alpha.f = 0.0

If Alpha <> 0.0
   Gamma.f = 1.0 / Alpha
EndIf
Regards
Guimauve
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

but the result holds the correct return-value of the FPU:

Code: Select all

f.f = 0.0
g.f = 1.0 / f
Debug g
debugs
1.#INF
oh... and have a nice day.
Derek
Addict
Addict
Posts: 2354
Joined: Wed Apr 07, 2004 12:51 am
Location: England

Re: Division by 0.0 allowed

Post by Derek »

Dr. Dri wrote: is it possible to remove this check ?
I have to ask, why would you want to?
User avatar
Deeem2031
Enthusiast
Enthusiast
Posts: 216
Joined: Sat Sep 20, 2003 3:57 pm
Location: Germany
Contact:

Post by Deeem2031 »

Guimauve wrote:The divide by zero without error message it's a MAJOR bug.
Its no bug, but if u really want to have an exception when divide by zero with float-numbers try this:

Code: Select all

FPU_ControlWord.w
!FSTCW [v_FPU_ControlWord]
FPU_ControlWord ! 4
!FLDCW [v_FPU_ControlWord]
So you get your "error message":

Code: Select all

zero.f = 0.0

Debug 1.0 / zero

FPU_ControlWord.w
!FSTCW [v_FPU_ControlWord]
FPU_ControlWord ! 4
!FLDCW [v_FPU_ControlWord]

Debug 1.0 / zero
irc://irc.freenode.org/#purebasic
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Guimauve wrote:The divide by zero without error message it's a MAJOR bug.
It's not really major in my opinion, but I reported it a few days ago anyways.
Kaeru Gaman wrote:but the result holds the correct return-value of the FPU:

Code: Select all

f.f = 0.0
g.f = 1.0 / f
Debug g
debugs
1.#INF
There is no "correct" return value. 1/0 is not infinity.
Division by zero is an operation for which you cannot find an answer, so it is disallowed. You can understand why if you think about how division and multiplication are related.

Code: Select all

   12 divided by 6 is 2   because
    6 times 2 is 12

   12 divided by 0 is x   would mean that
    0 times x = 12
But no value would work for x because 0 times any number is 0. So division by zero doesn't work.
User avatar
Deeem2031
Enthusiast
Enthusiast
Posts: 216
Joined: Sat Sep 20, 2003 3:57 pm
Location: Germany
Contact:

Post by Deeem2031 »

Trond wrote:There is no "correct" return value. 1/0 is not infinity.
We are not talking about what u've learned in school - you are writting programs which uses the FPU, and if you ask the FPU 1/0 is infinity! (or an exception if you set the ZeroFlag of the FPU ControlWord (see code above))
irc://irc.freenode.org/#purebasic
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Deeem2031 wrote:
Trond wrote:There is no "correct" return value. 1/0 is not infinity.
We are not talking about what u've learned in school - you are writting programs which uses the FPU, and if you ask the FPU 1/0 is infinity! (or an exception if you set the ZeroFlag of the FPU ControlWord (see code above))
You are writing programs in PB and if you ask PB it's not allowed.
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

PB does not allow it hardcoded, thats why

Code: Select all

g.f = 1.0 / 0.0
fails: the compiler doesn't accept the expression.
sure he couldn't because he tries to evaluate constant expressions.

but when you use variables, PB passes the responsability to the FPU,
that will react according to it's ControlWord, like Deeem described.

to me this behaviour is absolutely correct.
I only wish we had a native command to set the FPU ControlWord,
and a check to compare a result with 1.#INF
oh... and have a nice day.
thefool
Always Here
Always Here
Posts: 5875
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Post by thefool »

//some theory based on my actual thinking//

For sure a/0 is NOT infinite, thats bullshit. The problem is that floats are rather unprecise, and 0/VERYSMALLNUMBER = a very very large number.

So if the FPU finds a/0 to be infinite its because its not precise.
We are not talking about what u've learned in school
We certainly are! All of this is about that. Now i don't know what your education is but this is actually very very low level math..
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Kaeru Gaman wrote:PB does not allow it hardcoded, thats why

Code: Select all

g.f = 1.0 / 0.0
fails: the compiler doesn't accept the expression.
sure he couldn't because he tries to evaluate constant expressions.

but when you use variables, PB passes the responsability to the FPU,
that will react according to it's ControlWord, like Deeem described.
Not true. PB uses doubles and quads for evaluating constant expressions.

See how there's a rounding error? That's because a double was used.

Code: Select all

a.q = 23372036854775807.0
Debug a
Edit: No, it's not a double, I think it's a ten-byte float. But it's a float rounding error.
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

it seems to be a full FPU register. thats cool to know.
so if I use a decimal point in a literal, it will be evaluated by the FPU during the compile.
oh... and have a nice day.
Post Reply