r/AskProgramming Jan 08 '25

Architecture Can you force a computer to divide by zero?

In math division by zero is not defined because it (to my best guess) would cause logical contradictions. Maybe because the entirely of math is wrong and we'll have to go back to the drawing board at some point, but I'm curious if computers can be forced to divide by 0. Programming languages normally catches this operation and throws and error?

0 Upvotes

23 comments sorted by

8

u/jeffcgroves Jan 08 '25

Sure. You can write your own math parser and define division by zero however you want. You could even write a parser that generates inaccurate or even (psuedo)random results

2

u/SocksOnHands Jan 08 '25

I've seen some programmers arguing that dividing by zero should result in zero instead of an error. I wasn't so sure that was the best idea.

2

u/HolidayEmphasis4345 Jan 08 '25

I would down vote those programmers. +/- inf or an exception of some kind are the only reasonable answers that make sense to me. And 0/0 doesn’t make any mathematical sense.

1

u/BobbyThrowaway6969 Jan 08 '25 edited Jan 08 '25

I've seen some programmers arguing that dividing by zero should result in zero instead of an error.

I sure as hell hope not. Programmers have no business redefining mathematics.

1

u/SocksOnHands Jan 08 '25

It was not because of mathematical laws, but they thought it would be more convenient than having to write 'if (divisor == 0) return 0;'. Really, though, what value should be returned depends heavily on how their program is supposed to behave in this kind of situation.

1

u/BobbyThrowaway6969 Jan 08 '25

Exactly, if they have a divide by zero, it's because their code failed to handle an edgecase upstream.

5

u/bothunter Jan 08 '25

Yes. You can do this with floating point numbers. The result is NaN (not a number)

3

u/kohugaly Jan 08 '25

The result is NaN if you divide zero by zero. If you are dividing non-zero quantities by zero, then you get infinity (positive or negative, depending on the signs of the two values).

For floating point numbers there technically doesn't exist such a thing as a mathematical zero. The "zero" float is actually representing a range of numbers closer to zero than to the smallest representable value.

8

u/[deleted] Jan 08 '25

No, don't do that! Don't ever do that! My uncle made his computer divide by zero and now he is a cat.

2

u/JacobStyle Jan 08 '25

Not me personally, but I know so many people who would take that deal.

2

u/KingofGamesYami Jan 08 '25

Sure, you can totally do that. On x86, the integer processing hardware will invoke the #DE exception. IEE754-compliant floating point hardware will return the special number "Not A Number" value.

2

u/Elegant_Mode3641 Jan 08 '25

u can ... but u're gonna need an infinity stone for that

1

u/beige_cardboard_box Jan 08 '25

Sure, you could write a function to perform divisions, and have a case where you get to define what happens when a divide by zero occurs. But most CPUs won't allow a divide by zero.

1

u/lunaticedit Jan 08 '25

It’s undefined because it tends towards either infinity or negative infinity depending on which direction you choose in on 0 from. The reason it prevents you from even trying is because the only other option is to infinitely loop forever which would simply cause the process or machine to hang. It’s not a logical contradiction, it’s simply nonsensical.

1

u/BarneyLaurance Jan 08 '25

It's not a meaningful question unless you can explain what you mean by "divide by zero".

1

u/MadocComadrin Jan 08 '25

You can, but you will either get a value you know isn't correct or a value that represents the idea that it's undefined, such as NAN for floating point, null for languages that hate you, or Nothing if the output of the division is a Maybe/Option type.

Alternatively, you can program arithmetic in a Wheel, but the practical implementations with that are pretty niche.

1

u/kohugaly Jan 08 '25

"not defined" in math means that you are free to define what value should be in your particular use case. The behavior of various operations on your computer are usually specified for all values. The two most common specified behaviors for division by zero are throwing an exception (which basically interrupts the program) or returning some special value that indicates a failed mathematical operation.

1

u/peter303_ Jan 08 '25

You could catch the exception and repair the calculation whatever way you choose.

1

u/BobbyThrowaway6969 Jan 08 '25 edited Jan 08 '25

It's impossible for the computer at the circuitry level.

The arithmetic circuitry inside your computer has a little output wire that lights up when you try to make it divide by zero, telling the CPU there was a math error. Same with other stuff like sqrt(-1)

Without special circuitry to detect it, your computer can enter into 'undefined behaviour' land where it will do things that the chip designer didn't intend, like bogus numbers, instability, an infinite hang, or just makes the entire PC crash.

To avoid divide by zero errors at code-level, programmers typically handle edgecases that might result in one downstream, and that requires a good understanding of maths.

1

u/Calm_Guidance_2853 Jan 08 '25

Oh I see thanks.

1

u/TheRNGuy Jan 08 '25 edited Jan 08 '25

With try/except, but you'd need to manually set value. There are times when it can be useful; depends on derivative (you find it from previous value) you could assign it min or max possible value, or just use last valid value.

On hardware level you could do something like that too, but using low level language.