Skip to content
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

Reproduce Float-passing problem #9201

Merged
merged 17 commits into from
Mar 14, 2024
Merged

Reproduce Float-passing problem #9201

merged 17 commits into from
Mar 14, 2024

Conversation

GregoryTravis
Copy link
Contributor

@GregoryTravis GregoryTravis commented Feb 27, 2024

This applies @JaroslavTulach's fix in the comment below.

Checklist

Please ensure that the following checklist has been satisfied before submitting the PR:

  • The documentation has been updated, if necessary.
  • Screenshots/screencasts have been attached, if there are any visual changes. For interactive or animated visual changes, a screencast is preferred.
  • All code follows the
    Scala,
    Java,
    and
    Rust
    style guides. In case you are using a language not listed above, follow the Rust style guide.
  • All code has been tested:
    • Unit tests have been written where possible.
    • If GUI codebase was changed, the GUI was tested when built using ./run ide build.


vs.map v->
r = float_id v
IO.println "float_id "+v.to_text+" "+r.to_text
Copy link
Contributor Author

@GregoryTravis GregoryTravis Feb 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JaroslavTulach I am seeing some unusual behavior here. I wonder if you get the same results when running Decimal_Spec.enso locally?

This test simply passes a Float to a Java function that returns its argument. It seems like the argument is changed when it reaches the Java in some cases.

float_id 9.223372036854776E19 9.223372036854776E19
float_id 9.223372036854778E18 9.223372036854778E18
float_id 9.223372036854776E18 9.223372036854776E18
float_id 9.223372036854776E18 9.223372036854776E18
float_id 9.223372036854776E18 9.223372036854776E18
float_id 9.223372036854775E18 NaN # unexpected
float_id 9.223372036854774E18 NaN # unexpected
float_id 9.2233720368547E18 NaN # unexpected
float_id 9.223372036854E18 NaN # unexpected
float_id 9.223372E18 NaN # unexpected
float_id 9.2233720368547E17 5.148800401705924E-247 # unexpected
float_id 9.2233720368547E16 1.7265376942307606E-302 # unexpected
float_id 9.2233720368547E15 4.663754807431019E-308 # unexpected
float_id 9.2233720368547E14 9.2233720368547E14
float_id 9.2233720368547E13 9.2233720368547E13
float_id 9.223372036854E12 9.223372036854E12
float_id 9.22337203685E11 9.22337203685E11
float_id 9.2233720368E10 9.2233720368E10
float_id 9.223372036E9 9.223372036E9
float_id 9.22337203E8 9.22337203E8
float_id 9.223372E7 9.223372E7
float_id 9223372.0 9223372.0
float_id 922337.0 922337.0
float_id 92233.0 92233.0
float_id 9223.0 9223.0
float_id 922.0 922.0
float_id -9.223372036854776E19 -9.223372036854776E19
float_id -9.223372036854778E18 -9.223372036854778E18
float_id -9.223372036854776E18 -0.0 # unexpected
float_id -9.223372036854776E18 -0.0 # unexpected
float_id -9.223372036854776E18 -0.0 # unexpected
float_id -9.223372036854775E18 -5.06E-321 # unexpected
float_id -9.223372036854774E18 -1.012E-320 # unexpected
float_id -9.2233720368547E18 -3.74383E-319 # unexpected
float_id -9.223372036854E18 -3.8349E-318 # unexpected
float_id -9.223372E18 -1.8208678612E-313 # unexpected
float_id -9.2233720368547E17 -8.390303882377555E246 # unexpected
float_id -9.2233720368547E16 -2.6059089326772877E302 # unexpected
float_id -9.2233720368547E15 -8.772742498128177E307 # unexpected
float_id -9.2233720368547E14 -9.2233720368547E14
float_id -9.2233720368547E13 -9.2233720368547E13
float_id -9.223372036854E12 -9.223372036854E12
float_id -9.22337203685E11 -9.22337203685E11
float_id -9.2233720368E10 -9.2233720368E10
float_id -9.223372036E9 -9.223372036E9
float_id -9.22337203E8 -9.22337203E8
float_id -9.223372E7 -9.223372E7
float_id -9223372.0 -9223372.0
float_id -922337.0 -922337.0
float_id -92233.0 -92233.0
float_id -9223.0 -9223.0
float_id -922.0 -922.0

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if you get the same results when running Decimal_Spec.enso locally?

Yes, I do. I am seeing the same results.

@JaroslavTulach
Copy link
Member

JaroslavTulach commented Feb 28, 2024

This change seems to fix the problem:

enso$ git diff engine/polyglot-api/src/main/java/org/enso/polyglot/HostAccessFactory.java
diff --git engine/polyglot-api/src/main/java/org/enso/polyglot/HostAccessFactory.java engine/polyglot-api/src/main/java/org/enso/polyglot/HostAccessFactory.java
index 1f9cbbcf2c..150c43fc2f 100644
--- engine/polyglot-api/src/main/java/org/enso/polyglot/HostAccessFactory.java
+++ engine/polyglot-api/src/main/java/org/enso/polyglot/HostAccessFactory.java
@@ -21,11 +21,6 @@ public class HostAccessFactory {
         .allowIteratorAccess(true)
         .allowMapAccess(true)
         .allowAccessInheritance(true)
-        .targetTypeMapping(
-            Long.class,
-            Double.class,
-            (v) -> v != null && !longInSafeDoubleRange(v, LONG_MAX_SAFE_DOUBLE),
-            (v) -> Double.longBitsToDouble(v))
         .build();
   }
 

This conversion has been added in

by @hubertp. Hubert, looks like @GregoryTravis found edge cases when the conversion isn't working properly.

@enso-bot enso-bot bot mentioned this pull request Feb 29, 2024
@GregoryTravis GregoryTravis marked this pull request as ready for review February 29, 2024 16:58
@GregoryTravis GregoryTravis added the CI: No changelog needed Do not require a changelog entry for this PR. label Feb 29, 2024
@JaroslavTulach
Copy link
Member

We seem to have a failure related to Numeric values: can be passed in host calls without lossy coercion exception as that now ends:

Reason: An unexpected panic was thrown: (Unsupported_Argument_Types.Error [6907338656278321365] 
Cannot convert '6907338656278321365'(language: Java, type: java.lang.Long) to Java type 'double': 
Invalid or lossy primitive coercion.)
 at <enso> <anonymous><arg-1>(test/Base_Tests/src/Semantic/Java_Interop_Spec.enso:53:13-34)
 at <enso> null(Internal)
 at <enso> <anonymous>(Standard/Test/2024.1.1-dev/src/Group.enso:25:61-64)

I assume this is the test added by #4099 - its implementation was achieved by those five deleted lines. Now we have two options:

  • remove the test
  • don't delete the lines, but replace them with better ones

Removing the test can probably be done by anyone (with the blessing by someone from libraries team). Finding better lines is trickier. @hubertp please take a look and propose an alternative that wouldn't have drawbacks demonstrated in this PR and still satisfy the test.

@JaroslavTulach
Copy link
Member

  • remove the test

Removing a test is a bad practice in general, but it may be the right solution in this case. I believe the test has been written before support for

has been put in place. Before the BigInteger interop got fully supported there was no way to get java.math.BigInteger into libraries Java code - as such the conversion to double might have been necessary. However now it is possible to accept java.math.BigInteger along double or long and handle the case differently.

If that's the case the test may be deleted or modified to benefit from the new big integer capabilities.

@radeusgd
Copy link
Member

radeusgd commented Mar 1, 2024

  • remove the test

Removing a test is a bad practice in general, but it may be the right solution in this case. I believe the test has been written before support for

has been put in place. Before the BigInteger interop got fully supported there was no way to get java.math.BigInteger into libraries Java code - as such the conversion to double might have been necessary. However now it is possible to accept java.math.BigInteger along double or long and handle the case differently.

If that's the case the test may be deleted or modified to benefit from the new big integer capabilities.

I agree that once we have proper BigInteger support, we can modify our methods to accept a BigInteger properly.

But I don't think that was the only motivation behind #4099.

  1. One thing is APIs that we control (e.g. Moments in the test there), where we could add the BigInteger overload (and we probably also need a long overload there as well, right?)
  2. Another thing is external APIs that we do not control, e.g. someone calling a Java function from some library that is only accepting double.

The problem is - in Enso we usually accept 2 wherever a Float would be accepted, and so the major question is - are we okay with not allowing that in polyglot calls? Shall we then add some kind of to_float conversion, that allows to convert a 2 into 2.0?

@GregoryTravis
Copy link
Contributor Author

@hubertp What are your thoughts?

@hubertp
Copy link
Collaborator

hubertp commented Mar 4, 2024

@hubertp What are your thoughts?

Sorry, I replied to @JaroslavTulach in DM. I'm fine with ditching the test. The patch has been added prior to BigInteger support. I would also be fine in this case to make conversions explicit, rather than implicit, now.
The additional advantage of removing target mapping is that context initialization will be faster (that fragment actually took a non-negligible amount of time and could not be bypassed with other means).

@GregoryTravis
Copy link
Contributor Author

@hubertp What are your thoughts?

Sorry, I replied to @JaroslavTulach in DM. I'm fine with ditching the test. The patch has been added prior to BigInteger support. I would also be fine in this case to make conversions explicit, rather than implicit, now. The additional advantage of removing target mapping is that context initialization will be faster (that fragment actually took a non-negligible amount of time and could not be bypassed with other means).

Should this PR be merged on its own, or does it need other changes with it?

@JaroslavTulach
Copy link
Member

Should this PR be merged on its own, or does it need other changes with it?

Once the CI is green and code owners are fine with the PR, it can be merged.

Comment on lines 1 to 8
package org.enso.base.numeric;

public class Number_Utils {
// For testing only.
public static Double floatId(Double d) {
return d;
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is only used for tests, it should be moved to test/Base_Tests/polyglot-sources. This is a Java lib specifically compiled for Base_Tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Member

@radeusgd radeusgd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, but I'd like to move the helper to the dedicated test library instead.

btw. @JaroslavTulach curious note for you - as you can see, we have a separate JAR built for Base_Tests that includes some utils which are used for the tests but are not meant to be 'shipped' for users of Base library. Do you think we can retain this structure if we merge the tests to be parts of their libraries? (I guess it would not be a big issue if we cannot, just wondering)

@GregoryTravis GregoryTravis added the CI: Clean build required CI runners will be cleaned before and after this PR is built. label Mar 13, 2024
@GregoryTravis GregoryTravis added the CI: Ready to merge This PR is eligible for automatic merge label Mar 14, 2024
@mergify mergify bot merged commit dc84317 into develop Mar 14, 2024
39 of 41 checks passed
@mergify mergify bot deleted the 8982-reproduce-id-problem branch March 14, 2024 19:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CI: Clean build required CI runners will be cleaned before and after this PR is built. CI: No changelog needed Do not require a changelog entry for this PR. CI: Ready to merge This PR is eligible for automatic merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants