An immutable ComplexNumber class in Java, written from scratch: arithmetic, powers and roots, elementary transcendental functions, tolerance-based equality, formatting in four notations and parsing from strings. It comes with full Javadoc and 43 JUnit 5 tests.
- Arithmetic: sum, difference, product, quotient, opposite, conjugate, inverse, with both complex and real operands
- Powers and roots: integer, real and complex exponents (principal branch), all n-th roots
- Transcendental functions:
exp, principal logarithm,sin,cos,tan,sinh,cosh,tanh,asin,acos,atan - Four notations for construction and display: algebraic, trigonometric, exponential and 2×2 real matrix
- Parsing of algebraic and exponential strings, including scientific notation (
"1e3+2.5e-2i","2exp(1.5i)") - Equality at a fixed precision, consistent with
hashCodeandcompareTo, so the class works correctly inHashSet,TreeSetand as a map key
The class keeps three levels of precision separate:
- Raw data – real part, imaginary part, norm and angle store the exact floating-point results; every calculation works on these values, with no implicit rounding.
- Equivalence classes –
equals,hashCodeand the classifiers (isZero,isReal,isImaginary) compare the parts rounded to 9 decimal digits, so that numbers differing only by floating-point noise are equal. Rounding never produces-0.0, so the sign of a negligible part does not matter. - Representation –
toStringrounds to a display precision (3 digits by default) that can never exceed the internal one.
All fields are final and there is no mutable static state, so instances are thread-safe.
import io.github.sebian26.complex.ComplexFormatEnum;
import io.github.sebian26.complex.ComplexNumber;
ComplexNumber z = new ComplexNumber(3, 4);
ComplexNumber w = ComplexNumber.valueOf("1 - 2i");
z.plus(w); // 4.0 + 2.0i
z.times(w); // 11.0 - 2.0i
z.dividedBy(w); // -1.0 + 2.0i
z.getNorm(); // 5.0
z.toString(ComplexFormatEnum.EXP); // 5.0exp(0.927i)
ComplexNumber.exp(new ComplexNumber(0, Math.PI)); // -1.0 (Euler's identity)
new ComplexNumber(-8).rootsOfOrder(3); // [1.0 + 1.732i, -2.0, 1.0 - 1.732i]
ComplexNumber.I.raisedTo(ComplexNumber.I); // 0.208 (i^i = e^(-pi/2))
new ComplexNumber(1, 1e-12).equals(ComplexNumber.ONE); // truez.toString(ComplexFormatEnum.MATR) gives the matrix form:
[ 3.000 -4.000
4.000 3.000]
ComplexNumberTest contains 43 JUnit 5 tests, each tied to a statement of the documentation. They cover every public method, numbers in all four quadrants and on the axes, zero, very small and very large values, and invalid input. They also check these identities on sample points:
| Identity | |
|---|---|
| z · z⁻¹ = 1 | z · z̄ = |z|² |
| e^(iπ) = −1 | e^(Ln z) = z |
| sin²z + cos²z = 1 | cosh²z − sinh²z = 1 |
| sin(asin z) = z, cos(acos z) = z, tan(atan z) = z | asin z + acos z = π/2 |
| i² = −1 | product of the n-th roots of z = (−1)ⁿ⁺¹ z |
raisedTo(int)loses angular precision for huge exponents: for |n| around 10⁹ the error can reach about 1e-7.- Equality at 9 decimal digits becomes less meaningful for magnitudes above about 10⁶, where a
doubleno longer carries 9 decimals. - On the branch cut of real numbers greater than 1,
asinandacosreturn the complex conjugate of the C99 convention. intValue,longValue,floatValueanddoubleValuereturn the norm, not the real part.
The API documentation (Javadoc) is available online at
https://sebian-26.github.io/complex-number-java/ and in the docs folder.
src/io/github/sebian26/complex/
ComplexNumber.java
ComplexFormatEnum.java
package-info.java
test/io/github/sebian26/complex/
ComplexNumberTest.java
docs/
Javadoc HTML pages
-
Java 17 or later (developed with Java 25), JUnit 5 for the tests.
-
To regenerate the documentation:
javadoc -d docs -sourcepath src io.github.sebian26.complexor, in Eclipse, Project → Generate Javadoc with
docsas destination.