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

# Kotlin Remainder

The `%` operator in Kotlin is the arithmetic remainder operator. It calculates the remainder of the truncated division of the left operand (dividend) by the right operand (divisor).

Under the hood, Kotlin implements the `%` operator via operator overloading. When you use `%`, the Kotlin compiler translates it into a call to the `rem()` member function.

```kotlin theme={"dark"}
val dividend = 10
val divisor = 3

// Standard operator syntax
val result1 = dividend % divisor 

// Equivalent explicit function call
val result2 = dividend.rem(divisor) 
```

## Sign Mechanics

Unlike a strict mathematical modulo operation, Kotlin's remainder operator preserves the sign of the **dividend** (the left operand). The sign of the divisor (the right operand) has no effect on the sign of the result.

```kotlin theme={"dark"}
 5 %  3  // Returns  2
-5 %  3  // Returns -2
 5 % -3  // Returns  2
-5 % -3  // Returns -2
```

## Floating-Point Operands

The `%` operator is fully supported for `Float` and `Double` types. It adheres to the IEEE 754 standard for floating-point arithmetic, calculating the exact remainder without implicit rounding.

```kotlin theme={"dark"}
5.5 % 2.0  // Returns 1.5
5.0 % 1.5  // Returns 0.5
```

## Edge Cases and Exceptions

The behavior of the `%` operator when encountering a zero divisor depends strictly on the operand types:

* **Integer Types (`Int`, `Long`, `Short`, `Byte`):** Attempting to calculate the remainder with a divisor of `0` throws a `java.lang.ArithmeticException`.
* **Floating-Point Types (`Float`, `Double`):** Attempting to calculate the remainder with a divisor of `0.0` does not throw an exception; instead, it evaluates to `NaN` (Not a Number).

```kotlin theme={"dark"}
10 % 0     // Throws ArithmeticException: / by zero
10.0 % 0.0 // Returns NaN
```

## Custom Implementation

Because `%` maps to the `rem()` function, you can define the remainder operator for custom classes by utilizing the `operator` modifier.

```kotlin theme={"dark"}
class Vector(val x: Int, val y: Int) {
    operator fun rem(divisor: Int): Vector {
        return Vector(x % divisor, y % divisor)
    }
}

val v1 = Vector(10, 15)
val v2 = v1 % 4 // Evaluates to Vector(2, 3)
```

*(Note: In Kotlin versions prior to 1.1, the `%` operator mapped to the `mod()` function. This was deprecated and replaced by `rem()` to accurately reflect that the operation performs a truncated division remainder rather than a Euclidean modulo).*

<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 Kotlin 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>
