> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# C++ Division

The `/` operator is a binary arithmetic operator that computes the quotient of its first operand (the dividend) divided by its second operand (the divisor). It has left-to-right associativity, requires operands of arithmetic or unscoped enumeration types, and is subject to standard C++ usual arithmetic conversions. While the operator associates left-to-right (meaning `a / b / c` parses as `(a / b) / c`), the actual evaluation order of the left and right operands is unsequenced.

```cpp theme={"dark"}
auto result = lhs / rhs;
```

## Evaluation Mechanics

The behavior of the `/` operator is strictly dictated by the data types of its operands:

**1. Integer Division**
If both operands are of integer types, the operator performs integer division. The result is the algebraic quotient with any fractional part discarded. C++11 and later guarantee that this truncation is always directed towards zero.

```cpp theme={"dark"}
int a = 10 / 3;   // Evaluates to 3
int b = -10 / 3;  // Evaluates to -3
```

**2. Floating-Point Division**
If at least one operand is a floating-point type (`float`, `double`, `long double`), the operator performs floating-point division, preserving the fractional component up to the precision limits of the type.

```cpp theme={"dark"}
double c = 10.0 / 3;  // Evaluates to 3.333333...
```

**3. Type Conversion**
When operands are of mixed types, the compiler applies usual arithmetic conversions to yield a common type before the operation. Integral promotions are applied first (e.g., `short` to `int`). If the types still differ, the compiler applies conversions based on type categories (e.g., converting an integer to a floating-point type) and signedness rules.

```cpp theme={"dark"}
int x = 10;
double y = 3.0;
auto z = x / y; // 'x' is converted to double. 'z' is deduced as double (3.333333...)

// Note: Integer division occurs BEFORE assignment if both operands are integers
double w = 10 / 3; // Evaluates to 3.0. (10/3 yields int 3, which is then converted to double)
```

## Undefined Behavior (UB)

The `/` operator introduces specific edge cases that result in Undefined Behavior:

* **Division by Zero (Integer):** If the second operand evaluates to `0` and both operands are integers, the behavior is undefined. It typically results in a hardware exception (e.g., `SIGFPE`).
* **Division by Zero (Floating-Point):** If the environment strictly adheres to IEEE 754, dividing a non-zero float by `0.0` yields `±Infinity`, and `0.0 / 0.0` yields `NaN` (Not a Number). If IEEE 754 is not supported, it may invoke UB.
* **Signed Integer Overflow:** Dividing the minimum representable value of a signed integer type by `-1` results in UB. In a two's complement system, the absolute value of `INT_MIN` is one greater than `INT_MAX`, meaning the positive result cannot be represented in the target type.

```cpp theme={"dark"}
#include <climits>

int zero = 0;
int val1 = 5 / zero;               // UB: Integer division by zero at runtime

int minus_one = -1;
int val2 = INT_MIN / minus_one;    // UB: Signed integer overflow at runtime

double val3 = 5.0 / 0.0;           // Evaluates to +Inf (assuming IEEE 754)
```

## Operator Overloading

The `/` operator can be overloaded for user-defined types (classes, structs, or enumerations). It is conventionally implemented as a non-member function to allow symmetric implicit conversions on both the left-hand and right-hand sides. To achieve this symmetry, the user-defined type must provide an implicit converting constructor, and the operator must accept two objects of that type.

```cpp theme={"dark"}
struct Complex {
    double real, imag;
    
    // Implicit converting constructor enables symmetric conversions
    Complex(double r, double i = 0.0) : real(r), imag(i) {}
};

// Non-member overload taking two Complex objects
Complex operator/(const Complex& lhs, const Complex& rhs) {
    double denom = rhs.real * rhs.real + rhs.imag * rhs.imag;
    return Complex(
        (lhs.real * rhs.real + lhs.imag * rhs.imag) / denom,
        (lhs.imag * rhs.real - lhs.real * rhs.imag) / denom
    );
}

Complex c1(4.0, 8.0);

// Symmetric usage enabled by the implicit constructor and non-member signature:
Complex c2 = c1 / 2.0; // Valid: 2.0 implicitly converts to Complex(2.0, 0.0)
Complex c3 = 2.0 / c1; // Valid: 2.0 implicitly converts to Complex(2.0, 0.0)
```

When overloading `/`, it is standard practice to also overload the compound assignment operator `/=` as a member function, and then implement the binary `/` operator in terms of `/=`.

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C++ Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
