# C/C++: Floating point exception (core dumped) — SIGFPE, usually integer division by zero

**Code:** `Floating point exception` · **Category:** C/C++ · **Seen on:** C/C++, Linux, Windows

Despite the name, this is almost always an integer division or modulo by zero (or INT_MIN / -1); floating-point division by zero does not raise it by default.

## Meaning

On x86 the CPU traps integer `x / 0` and `x % 0`, and the OS reports SIGFPE as "Floating point exception". Floating-point division by zero normally produces `inf` or `nan` instead. Find the divisor that became zero: an empty input, a count that was never incremented, or a loop that starts at 0.

## Common causes

- Integer division or modulo by zero (`avg = sum / count` with `count == 0`)
- `INT_MIN / -1` overflow
- Empty container used to compute an average
- Divisor read from input or config that was left at 0
- Unchecked `%` in hashing code with table size 0

## Quick fix

1. Check the divisor before dividing
2. Run under gdb and `bt` to see the exact line
3. Compile with `-fsanitize=undefined` to get a clear message

## Detailed fix by platform

### Linux

1. `gdb ./app` → `run` → `bt` shows the dividing line
2. `g++ -g -fsanitize=undefined main.cpp` reports "division by zero" with file:line

### Windows

1. The same bug appears as exception 0xC0000094 (integer divide by zero)

## Code examples

### Guard the divisor

```cpp
#include <vector>
#include <numeric>

double average(const std::vector<int>& v) {
    if (v.empty()) return 0.0;                           // avoids dividing by zero
    return std::accumulate(v.begin(), v.end(), 0.0) / v.size();
}

int bucket(int hash, int buckets) {
    return buckets > 0 ? hash % buckets : 0;            // int % 0 would raise SIGFPE
}
```

## How to diagnose

1. **Line** — Which expression divides?
2. **Value** — Can the divisor be zero or empty?
3. **Type** — Integer or floating-point?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/cpp/floating-point-exception-sigfpe)
