-
Notifications
You must be signed in to change notification settings - Fork 323
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow conversion of EnsoBigInteger to double #3865
Allow conversion of EnsoBigInteger to double #3865
Conversation
Marcin has noted: "This breaks even the truffle contract" That's certainly a valid concern, @kustosz. Let's address it in 2bea701 - by checking the code in HostToTypeNode and HostUtil I noticed that Truffle languages may expose any |
engine/runtime/src/main/java/org/enso/interpreter/runtime/callable/function/Function.java
Show resolved
Hide resolved
@@ -19,6 +20,32 @@ public static long add(long a, long b) { | |||
return a + b; | |||
} | |||
|
|||
public static double doubleArrayAverage(double[] arr) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The averageOfMixedArrayOverDouble
test prepares array of power of 2 with 200 elements. Some of them are Long
, some of them are EnsoBigInteger
.
However the Java side can request them to be converted to double
- useful for example for statistic functions where the precision doesn't matter much.
return sum / arr.length; | ||
} | ||
|
||
public static double numberArrayAverage(Number[] arr) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should one need to distinguish between various Enso Number
types, one can accept Number[]
. The array would contain 63 Long
numbers and the rest would be EnsoBigInteger
instances that also extend java.lang.Number
.
var sum = BigInteger.ZERO; | ||
for (int i = 0; i < arr.length; i++) { | ||
var n = arr[i] instanceof Long l ? BigInteger.valueOf(l) : | ||
new BigInteger(arr[i].toString()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If one needs to get the precise value of EnsoBigInteger
one can use its toString()
method and parse the value back.
Alternative would be to make the number implement Callable<java.math.BigInteger>
or something other JDK interface (Supplier
, Function
, etc.) to provide the internal instance of BigInteger
.
I think the functionality is ready for your review, gentlemen! I am awaiting your comments. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Jaroslav convinced me that it is the best we can do for interop but I feel like I may not understand the implications of this change fully
FYI: There is going to be "official" support for |
@@ -16,7 +16,7 @@ | |||
/** Internal wrapper for a {@link BigInteger}. */ | |||
@ExportLibrary(InteropLibrary.class) | |||
@ExportLibrary(TypesLibrary.class) | |||
public final class EnsoBigInteger implements TruffleObject { | |||
public final class EnsoBigInteger extends Number implements TruffleObject { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got a comment from Christian Humer:
Your trick with
EnsoBigInteger extends Number
will not work on Espresso for example.
I guess we'll have to switch to official BigInteger
interop once it is available
Pull Request Description
Make sure any Enso number (including
EnsoBigInteger
) can be passed to Java via Truffle interop and used on the Java side.Notes
The problem is that there is no support for
java.math.BigInteger
in Truffle interop (as of GraalVM 22.3). We need some "approximations". There is a special handling for everything that "looks like number" - e.g. extendsjava.lang.Number
in the Truffle interop. Let's use that! That's the reason whyEnsoBigInteger
now extendsNumber
.That opens three possibilities on Java side:
java.math.BigInteger
viatoString
as the last test shows - slow, but preciseChecklist
Please include the following checklist in your PR:
Java,
style guides.