Skip to main content

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.

The / operator in Kotlin is the arithmetic division operator, used to evaluate the quotient of two operands. At compile time, Kotlin translates the / operator into a method invocation of the div() member or extension function via operator overloading conventions.
val result = a / b
// Translates internally to:
val result = a.div(b)

Primitive Type Behavior

Signed and Unsigned Integer Division When both the dividend and the divisor are integer types (signed: Byte, Short, Int, Long; unsigned: UByte, UShort, UInt, ULong), the / operator performs integer division. Arithmetic operations on smaller types (Byte, Short, UByte, UShort) implicitly promote the operands to Int or UInt respectively. The return type resolves to the 64-bit variant (Long or ULong) only if at least one operand is 64-bit; otherwise, it resolves to the 32-bit variant (Int or UInt). Evaluation truncates towards zero, strictly discarding any fractional component. Unsigned types utilize unsigned arithmetic instructions, yielding different results than signed division for bit-equivalent values.
val a: Byte = 5
val b: Byte = 2
val c = a / b // Evaluates to 2, type is Int

val u1: UInt = 5u
val u2: UInt = 2u
val u3 = u1 / u2 // Evaluates to 2u, type is UInt
Floating-Point Division If at least one operand is a floating-point type (Float or Double), the / operator performs floating-point division. The return type is implicitly promoted to the widest floating-point type present in the expression.
val a: Int = 5
val b: Double = 2.0
val c = a / b // Evaluates to 2.5, type is Double

Division by Zero Mechanics

The behavior of the / operator when the divisor evaluates to zero depends strictly on the operand types and the compilation target:
  • Integer Types (JVM): Throws an ArithmeticException at runtime for both signed and unsigned integer division.
  • Integer Types (Kotlin/Native): Triggers a hardware trap or crash (e.g., SIGFPE / EXC_ARITHMETIC). For performance reasons, Kotlin/Native does not inject zero-checks into integer division operations, meaning it does not throw a catchable Kotlin exception.
  • 32-bit and Smaller Integer Types (Kotlin/JS): Does not throw an exception. Because Kotlin/JS maps 32-bit and smaller integers (Int, Short, Byte) to JavaScript’s Number type (IEEE 754 floats) and emulates integer division using bitwise operations (e.g., (a / b) | 0), dividing a non-zero Int by 0 evaluates to 0 (since Infinity | 0 evaluates to 0).
  • 64-bit Integer Types (Kotlin/JS): Throws an ArithmeticException. In Kotlin/JS, 64-bit integers (Long and ULong) are not mapped to standard JavaScript numbers. They are emulated (or utilize BigInt), and their division implementation explicitly checks for zero.
  • Floating-Point Types (All Targets): Adheres to the IEEE 754 standard and does not throw an exception. Instead, it evaluates to Infinity (e.g., 1.0 / 0.0), -Infinity (e.g., -1.0 / 0.0), or NaN (Not a Number, e.g., 0.0 / 0.0).

Operator Overloading

The / operator can be implemented for user-defined types by declaring a function named div with the operator modifier. The function signature is highly flexible; the parameter type (divisor) and the return type (quotient) do not need to match the receiver type (dividend), allowing for asymmetric division operations.
class Vector(val x: Double, val y: Double) {
    // Overloading the '/' operator for Vector / Double
    operator fun div(scalar: Double): Vector {
        return Vector(x / scalar, y / scalar)
    }
}

val v1 = Vector(4.0, 8.0)
val v2 = v1 / 2.0 // Resolves to v1.div(2.0)
Master Kotlin with Deep Grasping Methodology!Learn More