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

Custom type conversion to double from large long #4099

Merged
merged 3 commits into from
Jan 31, 2023

Conversation

hubertp
Copy link
Contributor

@hubertp hubertp commented Jan 30, 2023

Pull Request Description

When a large long would be passed to a host call expecting a double, it would crash with a

Cannot convert '<some long>'(language: Java, type: java.lang.Long) to Java type 'double': Invalid or lossy primitive coercion

That is unlikely to be expected by users. It also came up in the Statistics examples during Sum. One could workaround it by forcing the conversion manually with .to_decimal but it is not a permanent solution.

Instead this change adds a custom type mapping from Long to Double that will do it behind the scenes with no user interaction. The mapping kicks in only for really large longs.

Important Notes

Note that the safe range is hardcoded in Truffle and it is not accessible in enso packages. Therefore a simple c&p for that max safe long value was necessary.

Checklist

Please include the following checklist in your PR:

  • The documentation has been updated if necessary.
  • All code conforms to the
    Scala,
    Java,
    and
    Rust
    style guides.
  • All code has been tested:
    • Unit tests have been written where possible.
    • If GUI codebase was changed: Enso GUI was tested when built using BOTH
      ./run ide build and ./run ide watch.

Copy link
Member

@JaroslavTulach JaroslavTulach left a comment

Choose a reason for hiding this comment

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

If we are going the targetTypeMapping route, when we can fix assert problems of EnsoBigInteger discovered in #4074 by using target type mapping as well. There is a pivotal calling for resolving the BigInteger issue by upgrade to (not yet known) version of GraalVM. But targetTypeMapping would address those issues as well.

When a large long would be passed to a host call expecting a double,
it would crash with a
```
Cannot convert '<some long>'(language: Java, type: java.lang.Long) to Java type 'double': Invalid or lossy primitive coercion
```

That is unlikely to be expected by users. It also came up in the
Statistics examples during Sum. One could workaround it by forcing the
conversion manually with `.to_decimal` but it is not a permanent
solution.

Instead this change adds a custom type mapping from Long to Double that
will do it behind the scenes with no user interaction. The mapping kicks
in only for really large longs.
@hubertp hubertp force-pushed the wip/hubert/lossy-coercion-184203977 branch from 53b2caa to 1c7198b Compare January 31, 2023 08:46
@hubertp hubertp added the CI: Ready to merge This PR is eligible for automatic merge label Jan 31, 2023
@mergify mergify bot merged commit d3b350f into develop Jan 31, 2023
@mergify mergify bot deleted the wip/hubert/lossy-coercion-184203977 branch January 31, 2023 15:13
@jdunkerley jdunkerley linked an issue Feb 6, 2023 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CI: Ready to merge This PR is eligible for automatic merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Statistics fails for large Integer values
4 participants