Throw UnsupportedMessageException when loosing BigInteger precision (#7817)

This commit is contained in:
Jaroslav Tulach 2023-09-15 17:00:09 +02:00 committed by GitHub
parent 4caee1b00f
commit af8a2c39d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 17 deletions

View File

@ -141,7 +141,7 @@ public abstract class EqualsNode extends Node {
@Specialization
@TruffleBoundary
boolean equalsDoubleBigInt(double self, EnsoBigInteger other) {
return self == other.asDouble();
return self == other.getValue().doubleValue();
}
@Specialization(guards="interop.fitsInDouble(other)")
@ -170,7 +170,7 @@ public abstract class EqualsNode extends Node {
@Specialization
@TruffleBoundary
boolean equalsBitIntDouble(EnsoBigInteger self, double other) {
return self.asDouble() == other;
return self.getValue().doubleValue() == other;
}
@Specialization

View File

@ -52,8 +52,9 @@ public abstract class AddNode extends IntegerNode {
}
@Specialization
@TruffleBoundary
double doBigIntDouble(EnsoBigInteger self, double that) {
return self.asDouble() + that;
return self.getValue().doubleValue() + that;
}
@Specialization(guards = "isForeignNumber(iop, that)")

View File

@ -89,39 +89,33 @@ public final class EnsoBigInteger implements EnsoObject {
}
@ExportMessage
@CompilerDirectives.TruffleBoundary
final byte asByte() throws UnsupportedMessageException {
return value.byteValue();
throw UnsupportedMessageException.create();
}
@ExportMessage
@CompilerDirectives.TruffleBoundary
final short asShort() throws UnsupportedMessageException {
return value.shortValue();
throw UnsupportedMessageException.create();
}
@ExportMessage
@CompilerDirectives.TruffleBoundary
final int asInt() throws UnsupportedMessageException {
return value.intValue();
throw UnsupportedMessageException.create();
}
@ExportMessage
@CompilerDirectives.TruffleBoundary
final long asLong() throws UnsupportedMessageException {
return value.longValue();
throw UnsupportedMessageException.create();
}
@ExportMessage
@CompilerDirectives.TruffleBoundary
final float asFloat() {
return value.floatValue();
final float asFloat() throws UnsupportedMessageException {
throw UnsupportedMessageException.create();
}
@ExportMessage
@CompilerDirectives.TruffleBoundary
public final double asDouble() {
return value.doubleValue();
public final double asDouble() throws UnsupportedMessageException {
throw UnsupportedMessageException.create();
}
@ExportMessage

View File

@ -12,6 +12,7 @@ import Standard.Test.Extensions
import project.Data.Round_Spec
polyglot java import java.math.BigInteger
polyglot java import java.math.BigDecimal
Integer.is_even self = self % 2 == 0
@ -572,6 +573,14 @@ spec =
v -> Test.fail "Expecting Integer, but got: "+(Meta.type_of v).to_text
sum.div two_hundred.length . should_equal expected_value
Test.specify name+" BigInteger to BigDecimal test" <|
h = 2^70
bd1 = BigDecimal.new h 0
bd2 = BigDecimal.new h
bd1.to_text . should_equal bd2.to_text
bd1 . should_equal bd2
bd1.toBigIntegerExact . should_equal h
to_java_bigint n = BigInteger.new n.to_text
java_bigint_mul a b =
big_a = to_java_bigint a