tools4j / decimal4j

Java library for fast fixed-point arithmetic based on longs with support for up to 18 decimal places.
decimal4j.org
MIT License
155 stars 17 forks source link

Any chance you will be adding some kotlin convenience code? #19

Open xGnoSiSx opened 3 years ago

xGnoSiSx commented 3 years ago

Any chance you will be adding some kotlin convenience code?

terzerm commented 3 years ago

Thanks for your question. We have not currently planned any such activities. Can you elaborate a bit more as to which part of the library you would like to be made available in a more convenient way, e.g. with some pseudo code examples?

xGnoSiSx commented 3 years ago

Hi,

Very simple: Operator overloading. Kotlin allows you to define functions with special names to make it possible to define natural mathematical operatos on custom types: Kotlin reference

This includes, comparison, unary, binary, and augmented assignment operators.

I just picked up this library and will probably be writting those as inline extension helpers for Decimal3f, without changing the library. Extensions

I can send/upload those, for that datatype and you can just copy/paste and replace for the other types.

Example:

inline fun Decimal3f.plus(b : Decimal3f) : Decimal3f { 
    return this.add(b)
}

And after that, you can write:

var result: Decimal3f = a + b + c + d

You could also add helper extensions for Longs, but these could get tricky.

xGnoSiSx commented 3 years ago

There's also increment operators, but may be difficult to decide the increment step. I'd assume by 1 but others might think that's wrong. You could leave that up for users to define.