jnr / jnr-ffi

Java Abstracted Foreign Function Layer
Other
1.23k stars 154 forks source link

Add support for native `long double` using Java `BigDecimal` #290

Open basshelal opened 2 years ago

basshelal commented 2 years ago

The largest precision floating point type we support is 64bit, native and Java double.

But there exists an additional precision type built into the language, long double which is supposed to be at least as precise as double. See more on Wikipedia

We should support this native type by allowing users to map native long double to Java's BigDecimal which is the standard way of storing floating point numbers that cannot fit into double. This may need an annotation from users to help JNR-FFI know exactly what is being desired. An example mapping would look like this:

C function:

typedef long double ldouble;
ldouble ret_ldouble(ldouble d);

Java:

public @long_double BigDecimal ret_ldouble(@long_double BigDecimal d);

This may be much more difficult than it initially seems for multiple different reasons:

This might be more work than it's worth, but I think it's worth at least investigating and logging the findings for when one day this may be more worthwhile pursuing.