Over a million developers have joined DZone.

Fixing Floating-Point Arithmetics With Kotlin

DZone's Guide to

Fixing Floating-Point Arithmetics With Kotlin

Those of you who learned Java in an academic context probably remember something fishy around FP arithmetics. Then if you never used them, you probably forgot about them.

· Java Zone ·
Free Resource

The CMS developers love. Open Source, API-first and Enterprise-grade. Try BloomReach CMS for free.

This week saw me finally taking time to analyze our code base with Sonar. In particular, I was made aware of plenty of issues regarding floating-point arithmetics.

Fun With Java’s Floating-Point Arithmetics

Those of you who learned Java in an academic context probably remember something fishy around FP arithmetics. Then if you never used them, you probably forgot about them. Here’s a very quick example of interesting it turns out to be:

double a = 5.8d;
double b = 5.6d;
double sub = a - b;

Contrary to common sense, this snippet throws an AssertionError: sub is not equal to 0.2 but to 0.20000000000000018.

BigDecimal as a Crutch

Of course, no language worthy of the name could let that stand. BigDecimal is Java’s answer:

The BigDecimal class provides operations for arithmetic, scale manipulation, rounding, comparison, hashing, and format conversion.

Let’s update the above snippet with BigDecimal:

BigDecimal a = new BigDecimal(5.8d);
BigDecimal b = new BigDecimal(5.6d);
BigDecimal sub = a.subtract(b);
assertThat(sub).isEqualTo(new BigDecimal(0.2d));

And run the test again… Oooops, it still fails:

to be equal to:
but was not.

Using constructors changes nothing, one has to use the static valueOf() method instead.

BigDecimal a = BigDecimal.valueOf(5.8d);
BigDecimal b = BigDecimal.valueOf(5.6d);
BigDecimal sub = a.subtract(b);

Finally it works, but as the cost of a lot of ceremony…

Kotlin to the Rescue

Just porting the code to Kotlin only marginally improves the readability:

val a = BigDecimal.valueOf(5.8)
val b = BigDecimal.valueOf(5.6)
val sub = a.subtract(b)

Note that in Kotlin, floating-point numbers are doubles by default.

In order to make the API more fluent and thus the code more readable, two valuable Kotlin features can be applied.

The first one is extension method (I’ve already showed their use in a former post to improve logging with SLF4J). Let’s use it here to easily create BigDecimal objects from Double:

fun Double.toBigDecimal(): BigDecimal = BigDecimal.valueOf(this)

val a = 5.8.toBigDecimal() // Now a is a BigDecimal

The second feature - coupled with method extension, is operator overloading. Kotlin sits between Java where operator overloading is impossible, and Scala where every operator can be overloaded (I’m wondering why there aren’t already any emoticons library): only some operators can be overloaded, including those from arithmetics - +, -, * and /.

They can be overridden quite easily, as shown here:

operator fun BigDecimal.plus(a: BigDecimal) = this.add(a)
operator fun BigDecimal.minus(a: BigDecimal) = this.subtract(a)
operator fun BigDecimal.times(a: BigDecimal) = this.multiply(a)
operator fun BigDecimal.div(a: BigDecimal) = this.divide(a)

val sub = a - b

Note this is already taken care of in Kotlin’s stdlib.

The original snippet can now be written like this:

val a = 5.8.toBigDecimal()
val b = 5.6.toBigDecimal()
assertThat(a - b).isEqualTo(0.2.toBigDecimal())

The assertion line can probably be improved further. Possible solutions include AssertJ custom assertions or… extension method again, in order for the isEqualTo() method to accept Double parameters.


Any complex API or library can be made easier to read by using Kotlin extension methods. What are you waiting for?

BloomReach CMS: the API-first CMS of the future. Open-source & enterprise-grade. - As a Java developer, you will feel at home using Maven builds and your favorite IDE (e.g. Eclipse or IntelliJ) and continuous integration server (e.g. Jenkins). Manage your Java objects using Spring Framework, write your templates in JSP or Freemarker. Try for free.

api ,kotlin

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}