Taymon
Taymon

Reputation: 25656

Why does integer division by zero result in a floating point exception?

Division by zero in a C program results in abnormal termination with the error message Floating point exception (core dumped). This is unsurprising for floating point division, but why does it say this when integer division by zero occurs? Does integer division actually use the FPU under the hood?

(This is all on Linux under x86, by the way.)

Upvotes: 20

Views: 7626

Answers (4)

AnT stands with Russia
AnT stands with Russia

Reputation: 320361

There could be many different implementation-specific reasons for that.

For example, the FPU unit on x86 platform supports both floating point and integer formats for reading arguments and writing results. Back when the platform itself was 16-bit, some compilers used the FPU to perform division with 32-bit integer operands (since there's no precision loss for 32-bit wide data). Under such circumstances there would be nothing unusual in getting a genuine FPU error for invalid 32-bit integer division.

Upvotes: 1

user2404501
user2404501

Reputation:

My guess at a historical explanation for this would be that the original unix hardware didn't generate a trap on integer division by zero, so the name SIGFPE made sense. (PDP assembly programmers, confirm?) Then later when the system was ported (or in the case of Linux, reimplemented) to hardware with an integer division-by-zero trap, it was not considered worthwhile to add a new signal number, so the old one acquired a new meaning and now has a slightly confusing name.

Upvotes: 7

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272447

Does integer division actually use the FPU under the hood?

No, Linux just generates SIGFPE in this case too (it's a legacy name whose usage has now been extended). Indeed, the Single Unix Specification defines SIGFPE as "Erroneous arithmetic operation".

Upvotes: 27

millimoose
millimoose

Reputation: 39950

man signal mentions:

Integer division by zero has undefined result. On some architectures it will generate a SIGFPE signal. (Also dividing the most negative integer by -1 may generate SIGFPE.)

Upvotes: 6

Related Questions