How to fix “floating point exception (core dumped)” error

A float type number is any number (positive or negative) with a decimal point in it. A few examples of these are 5.6, 0.25, -1.3, and many more. While dealing with float-type numbers, you may encounter the error with the statement “floating point exception”. The error “floating point exception (core dumped)” occurs when we try to perform invalid operations on a float type number.

In this post, we will state the reasons that invoke this error and also the possible solutions to this error.

How can you fix the error “floating point exception”?

There exist many different factors that can be the reason for “floating point exception error”. This section will provide every possible reason for this error.

Reason 1: Invalid Mathematical operations

Some operations in mathematics are invalid. This indicates are no solutions exist for them. These operations stand to be at the top of the reasons that can invoke the error “floating point exception (core dumped) error”. The example below is an invalid operation:

Solution

The solution to invalid operations is a little more tricky since it will require some knowledge about maths. We will need to check the code for operations that are invalid in maths. A few simple examples of such operations are listed below:

Sr noOperation
1Dividing any number by zero
2log(0)
3Square Root of a negative number

Reason 2: Underflow

In programming, variables are stored as certain data types. Each data type has a limit to the size of data that can be stored inside it. Underflow occurs when the data you want to store inside a certain variable is smaller than the minimum limit of that data type.

Solution

To avoid underflow we need to know the minimum limit of data types and make sure that our variable does not recede below the limit. To do this we can add if statements that will limit the size of the variable or you may know the minimum limit of the various data types. For better understanding, we have created the following table that contains the minimum limit of the well-known data types:

Data TypeMinimum Value
Char-128
Short-32768
Int-2147483648
Float1.175494351 E – 38

Reason 3: Overflow

Its concept is similar to as mentioned above. The overflow occurs when you want to store a value that is higher than the maximum limit of that data type.

Solution

When dealing with overflow we need to make sure that none of our values or answers to any equations exceed the upper limits of our data type. The following tables represent the maximum value of all the possible data types:

Data TypeMinimum Value
Char127
Short32767
Int2147483647
Float3.402823466 E + 38

Conclusion

There are four reasons that invoke the error statement “floating point exception (core dumped)”. The reasons include: using invalid maths operations such as dividing by zero, overflow, and underflow of numbers. This detailed article has described the reasons that cause the error mentioned above. Moreover, we have also demonstrated the solutions to all these reasons.