开发者

How do division-by-zero exceptions work?

How is division calculated on compil开发者_StackOverflow中文版er/chip level?

And why does C++ always throw these exceptions at run-time instead of compile-time (in case the divisor is known to be zero at compile time)?


  1. It depends. Some processors have a hardware divide instruction. Some processors have to do the calculation is software.
  2. Some C++ compilers don't trap at runtime either. Often because there is no hardware support for trapping on divide by zero.


It totally depend on the compiler. You can if you want write an extension for your compiler to check this kind of problem.

For example visual C++:

  • Division by zero Compiler error


  1. At the chip level, division is of course done with circuits. Here's an overview of binary division circuitry.
  2. Because the C++ compiler just isn't checking for divisors that are guaranteed to equal 0. It could check for this.


  1. Big lookup tables. Remember those multiplication tables from school? Same idea, but division instead of multiplication. Obviously not every single number is in there, but the number is broken up into chunks and then shoved through the table.

  2. The division takes place at runtime, not at compile time. Yes, the compiler could see that the divisor is zero, but most people are not expected to write an invalid statement like that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜