Kotlin Complex Numbers

A lightweight mathematical library that allows a comfortable handling of complex numbers in Kotlin


The Maven artifact is available in the Maven Central repository:


The sources are hosted on GitHub.


import org.kotlinmath.*

The complex number 4+3i can be defined in four ways:
val z1 = 4 + 3.I
val z2 = 4 + 3 * I
val z3 = complex(3, 4)
val z4 = "4+3i".toComplex()

You can use the operators +, -, *, and / with any other numeric type in all combinations:
val w1 = 1.5F / z1 * 2 - 4.32 + 10L + (3.toBigDecimal() * I)

There are the common standard functions like exp, sin, cos, ln and sqrt:
val w2 = I * z1 + exp(I * PI/2) + sqrt(-9)

The extension.I creates a pure imaginary number;.R creates a complex number without imaginary part:
val fourI = 4.I // four times i

val three = 3.R // three as number of type Complex

The constants INF (infinity) and NaN (not a number) can be used to represent a pole resp. an essential singularity. Examples:
(3 + 4.I) / 0 == INF
1 / INF == ZERO
ln(0) == NaN
2 * INF == INF
exp(INF) == NaN

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.