What happens when I divide by zero?

夙愿已清 提交于 2019-12-10 21:02:01

问题


Now I'm not asking about the mathematical answer of dividing by zero, I want to what my computer does when it tries to divide by zero?

I can see different answers depending on what level we look at? Looking at a high-level I expect the language specification may just say "hey you can't do that throw an error"

Looking at an assembly level, will the CPU try to call the divide instruction when we try to divide by 0?

If it does that'll take us to the machine code level. What happens now?

Now if that doesn't happen and we force it to happen, what would the result be?


回答1:


Looking at an assembly level, will the CPU try to call the divide instruction when we try to divide by 0?

Yep instruction gets fetched and decoded. What happens then? The divisor is found to be zero and the process stops doing what it's doing. It throws some kind of an exception, pipeline (if there is one) is flushed and most likely control jumps to some predefined error handling code - often the OS controls a machine level jump table called an interrupt vector (or this could be a separate to the interrupt vector table).

There are many architectures however, and things like error handling vary. Intel x86 follows above procedure at least.

If it does that'll take us to the machine code level. What happens now?

I have no idea what that means. From the CPU's perspective, it is all machine code level.




回答2:


I think what you're asking is what's going to happen if we perform binary division algorithm with 0 in the denominator.

The algorithm will go into an infinite loop, and the quotient will grow larger and larger until it exhausts all available memory.



来源:https://stackoverflow.com/questions/20842515/what-happens-when-i-divide-by-zero

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!