Page 1 of 2

Division by 0.0 allowed

Posted: Sun May 27, 2007 7:38 pm
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

Posted: Sun May 27, 2007 8:57 pm
by michaeled314
You're crazy if you think you will get a result for a divide by zero!

Posted: Sun May 27, 2007 9:00 pm
by Trond
1/0 is undefined. So allowing it makes no sense.

Edit: Rrrrrr too late for the second time today.

Posted: Sun May 27, 2007 9:27 pm
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

Posted: Sun May 27, 2007 9:40 pm
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

Re: Division by 0.0 allowed

Posted: Sun May 27, 2007 9:53 pm
by Derek
Dr. Dri wrote: is it possible to remove this check ?
I have to ask, why would you want to?

Posted: Sun May 27, 2007 10:38 pm
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

Posted: Mon May 28, 2007 8:11 am
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.

Posted: Mon May 28, 2007 8:46 am
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))

Posted: Mon May 28, 2007 9:38 am
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.

Posted: Mon May 28, 2007 10:37 am
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

Posted: Mon May 28, 2007 11:09 am
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..

Posted: Mon May 28, 2007 11:31 am
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.

Posted: Mon May 28, 2007 2:39 pm
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.

Posted: Mon May 28, 2007 4:00 pm
by DarkDragon