divide by zero exception?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
skandalOS
Posts: 15
Joined: Mon Sep 05, 2011 12:05 pm

divide by zero exception?

Post by skandalOS »

Hello!

I've got a simple question about a problem, where I've not idea why but guess that it could be a problem because of architecture.

Why is there a 'divide by zero exception'? Could this problem not been solved by software manipulation?

Can you help me please?

Thanks a lot
User avatar
Karlosoft
Member
Member
Posts: 277
Joined: Thu Feb 14, 2008 10:46 am
Location: Italy
Contact:

Re: divide by zero exception?

Post by Karlosoft »

What do you mean with "software manipolation"? Dividing by 0 is a big math problem... The processor need to handle this error so there is an exception for this. Try to think what coul happen if your pc need to write the value to an industrial machine. Exceptions not handled could mean lives destroyed... And I'm not kidding
OSwhatever
Member
Member
Posts: 595
Joined: Mon Jul 05, 2010 4:15 pm

Re: divide by zero exception?

Post by OSwhatever »

I think making it an exception, reduces your code so that you don't have to check a CPU flag every time the CPU issue the divide instruction. In some CPU architectures you might shut this exception off if you want or you can make the exception return doing nothing, it's up to you really. I think the exception there to help us rather there to annoy us. In a sense, if there is a divide by zero your program is faulty and it should be closed. Continuing could lead to unexpected results.
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: divide by zero exception?

Post by Brendan »

Hi,
skandalOS wrote:Why is there a 'divide by zero exception'?
There isn't!

There is a "divide exception" for overflow. For example, (for the DIV instruction) dividing the 64-bit number 0x123456789ABCDEF by the 32-bit number 1 causes a "divide exception" because the result is too large to fit in a 32-bit register.

Division by zero causes an overflow (and the overflow causes a "divide exception"). :)


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
OSwhatever
Member
Member
Posts: 595
Joined: Mon Jul 05, 2010 4:15 pm

Re: divide by zero exception?

Post by OSwhatever »

Brendan wrote:Hi,
skandalOS wrote:Why is there a 'divide by zero exception'?
There isn't!

There is a "divide exception" for overflow. For example, (for the DIV instruction) dividing the 64-bit number 0x123456789ABCDEF by the 32-bit number 1 causes a "divide exception" because the result is too large to fit in a 32-bit register.

Division by zero causes an overflow (and the overflow causes a "divide exception"). :)


Cheers,

Brendan
So what what happens if you divide 0x1234 with 0 then?
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: divide by zero exception?

Post by Combuster »

OSwhatever wrote:So what what happens if you divide 0x1234 with 0 then?
The processor repeatedly tries to subtract 0 times 2^bitnumber and then comes to the conclusion you can fit 0xffffffffffffffff times 0 in 0x1234. That's too big for a 32-bit number so a divide error is generated.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
Karlosoft
Member
Member
Posts: 277
Joined: Thu Feb 14, 2008 10:46 am
Location: Italy
Contact:

Re: divide by zero exception?

Post by Karlosoft »

a) It's not completely true, a processor may implement this "function" in a different way.
b) I hope no processor uses the method you said. It's very slow between big numbers divided by small ones.

We can just say that there is this exception not how it works inside because intel could provide a different solution from other clones.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: divide by zero exception?

Post by Combuster »

Karlosoft wrote:a) It's not completely true, a processor may implement this "function" in a different way.
b) I hope no processor uses the method you said. It's very slow between big numbers divided by small ones.
A typical hardware division performs bit_count sequnces of shift-compare-subtractconditional-rotatewithcarry just like you would do long division on paper (the difference is base 2 vs base 10). Integer division is in fact the slowest integer operation a processor supports because of the amount of dependencies between steps compared to multiplication which can just be rewritten as a large sum with the corresponding amount of electrical parallelism. In all cases, the dumb algorithm performs linearly to the number of bits in the operand (assuming the processor can do basic operations on those widths in a single clock which usually is the case).

In the end, all processors will perform some version of long division (where more performance means more transistors). A division by zero however has different effects in different implementations and may have to be special-cased to maintain the backwards compatibility with the "dumb" algorithm.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: divide by zero exception?

Post by bluemoon »

Does the CPU use numerical approximation like Newton-Raphson method? If an initial guess value is required it may be deduced by difference in position of MSB.
OSwhatever
Member
Member
Posts: 595
Joined: Mon Jul 05, 2010 4:15 pm

Re: divide by zero exception?

Post by OSwhatever »

bluemoon wrote:Does the CPU use numerical approximation like Newton-Raphson method? If an initial guess value is required it may be deduced by difference in position of MSB.
I know Intel SSE does this with their fast division instructions. They have a list in the CPU for an initial accuracy that can later be refined with a few Newton-Raphson iterations.
User avatar
Holus
Member
Member
Posts: 51
Joined: Thu Jan 27, 2011 5:57 pm

Re: divide by zero exception?

Post by Holus »

OSwhatever wrote:
bluemoon wrote:Does the CPU use numerical approximation like Newton-Raphson method? If an initial guess value is required it may be deduced by difference in position of MSB.
I know Intel SSE does this with their fast division instructions. They have a list in the CPU for an initial accuracy that can later be refined with a few Newton-Raphson iterations.
Just like 3D graphical do not calculate COS and SIN calculations but look in a table of pre-calculated values.
Computer says NOOOO
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: divide by zero exception?

Post by Combuster »

bluemoon wrote:Does the CPU use numerical approximation like Newton-Raphson method? If an initial guess value is required it may be deduced by difference in position of MSB.
This was about integer divisions, which are specified to have perfect accuracy and therefore can't use iterative approximations by design. For floats it's a slightly different matter because it can not represent perfect accuracy. Even so, fdiv is accurate to the bits available (assuming it's bugfree for which there's a record). 3DNow added approximating reciprocals (of square roots), the concept of which later found it's way into SSE.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: divide by zero exception?

Post by bluemoon »

Did I misunderstood, or integer arithmetic is meant to be inaccurate and round off to the nearest decimal?
StephanvanSchaik
Member
Member
Posts: 127
Joined: Sat Sep 29, 2007 5:43 pm
Location: Amsterdam, The Netherlands

Re: divide by zero exception?

Post by StephanvanSchaik »

bluemoon wrote:Did I misunderstood, or integer arithmetic is meant to be inaccurate and round off to the nearest decimal?
Integer arithmetic doesn't get rounded off, hence the existence of the modulo-operator/remainder (which is stored in EDX, in case of the x86-architecture).


Yours faithfully
S.J.R. van Schaik.
Post Reply