Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.)

like image 518
Taymon Avatar asked Jun 04 '13 23:06

Taymon


People also ask

What causes a floating point exception?

Why Is There a Floating Point Exception (Core Dumped) Error? Floating point exception occurs because of several reasons such as invalid operation, division by zero, overflow, underflow, or inexact.

What does floating point division by zero mean?

Floating point error means that there is a division by a zero value in your code. It can be a variable, input, or a function in use that returns zero value. You need to identify this variable/input/function and make sure that the returned value is not zero.

What determines if division is integer or floating point?

The division operator / means integer division if there is an integer on both sides of it. If one or two sides has a floating point number, then it means floating point division.

What is the result of dividing one floating point number by zero?

Dividing a floating-point value by zero doesn't throw an exception; it results in positive infinity, negative infinity, or not a number (NaN), according to the rules of IEEE 754 arithmetic.


1 Answers

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".

like image 156
Oliver Charlesworth Avatar answered Sep 28 '22 08:09

Oliver Charlesworth