Linux SIGFPE — Floating Point Exception (Arithmetic Error)
About Linux SIGFPE
Linux SIGFPE (signal 8) occurs from arithmetic errors including integer division by zero, integer overflow, and floating-point exceptions. This guide covers everything you need to know about this topic, including common causes, step-by-step solutions, and answers to frequently asked questions.
Here are the key things to understand: SIGFPE (signal 8) is misleadingly named — it covers ALL arithmetic errors, not just floating-point. Most common cause: integer division by zero. Integer overflow does not normally raise SIGFPE unless compiled with -ftrapv (GCC). Floating-point operations normally produce Inf/NaN instead of SIGFPE by default. SIGFPE from floating-point only occurs if FP exceptions are explicitly enabled (feenableexcept). Understanding these fundamentals will help you diagnose and resolve this issue more effectively.
The most common reasons this occurs include: Integer division by zero (/ or % operator with divisor = 0). Integer overflow with -ftrapv compiler flag enabled. Floating-point exception explicitly enabled with feenableexcept(). INT_MIN / -1 causing overflow in two's complement arithmetic. Modulo by zero (x % 0). Identifying the root cause is the first step toward finding the right solution.
To resolve this, follow these recommended steps: Use GDB to find the exact line: gdb ./program, run, then 'bt' at SIGFPE. Add division-by-zero guards in code: if (divisor != 0) result = x / divisor;. For C code: compile with -fsanitize=integer for runtime overflow detection. Check for INT_MIN / -1 edge case (yields INT_MIN + 1 which overflows). Review floating-point exception settings: check for feenableexcept() calls. If these steps do not resolve the issue, consider consulting additional resources or a qualified professional.
This article is part of our Linux Error Codes collection on Error Codes Wiki. We provide comprehensive, up-to-date information to help you find solutions quickly.
Quick Answer
Why is it called SIGFPE if it is about integer division?
Historical name from when all arithmetic was done by the floating-point unit. The name stuck even though it covers all arithmetic exceptions now.
Overview
Linux SIGFPE (signal 8) occurs from arithmetic errors including integer division by zero, integer overflow, and floating-point exceptions.
Key Details
- SIGFPE (signal 8) is misleadingly named — it covers ALL arithmetic errors, not just floating-point
- Most common cause: integer division by zero
- Integer overflow does not normally raise SIGFPE unless compiled with -ftrapv (GCC)
- Floating-point operations normally produce Inf/NaN instead of SIGFPE by default
- SIGFPE from floating-point only occurs if FP exceptions are explicitly enabled (feenableexcept)
Common Causes
- Integer division by zero (/ or % operator with divisor = 0)
- Integer overflow with -ftrapv compiler flag enabled
- Floating-point exception explicitly enabled with feenableexcept()
- INT_MIN / -1 causing overflow in two's complement arithmetic
- Modulo by zero (x % 0)
Steps
- 1Use GDB to find the exact line: gdb ./program, run, then 'bt' at SIGFPE
- 2Add division-by-zero guards in code: if (divisor != 0) result = x / divisor;
- 3For C code: compile with -fsanitize=integer for runtime overflow detection
- 4Check for INT_MIN / -1 edge case (yields INT_MIN + 1 which overflows)
- 5Review floating-point exception settings: check for feenableexcept() calls