In an effort to become more familiar with Kotlin, I've decided to implement a class that represents rational numbers, ie. numbers that can be expressed as one integer divided by another. Numbers represented this way have the advantage that they can be manipulated without any loss of precision (unlike doubles and floats which are imprecise). I've found that rationals are useful, in particular with my work on LastCalc, a web-based calculator/programming language implemented in Java.
It’s still a work in progress, but since I’m new to Kotlin I wanted to get some early feedback in case I’m doing anything dumb - the important class can be found here:
As you can see, there is much to be done, including comments and unit tests. It certainly isn’t professional quality yet, not even close. With this in mind I’ve been looking for examples of how to use kunit, but haven’t had much luck. Ultimately my intention is to release this under a sufficiently liberal open source license that it could be integrated into the Kotlin standard library, assuming that it is considered sufficiently useful.
For now I’d be interested in feedback, and am also happy to accept patches. In particular I’m interested in constructive criticism of how Rationals are currently constructed. I’ve made the constructor private so that I can enforce “simplified” being true only if the rational is in its simplest form.