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

BigInt.equals(Number) and BigDecimal.equals(Number) should implement equality in mathematical sense #5627

Closed
scabug opened this issue Mar 29, 2012 · 4 comments
Milestone

Comments

@scabug
Copy link

scabug commented Mar 29, 2012

The BigInt.equals(that: Any) and BigDecimal.equals(that: Any) should implement
equality in mathematical sense (at least when "that" is primitive.
I mean that BigInt.equals(that: Any) iff "this" and "that" represent the same rational number.

However, this is not always so.
The value 2^64 can be represented exactly both by BigInt, Float and Double, but the assertions below fails.

def testBigIntEquals {
 val bi = BigInt(1) << 64
 assert(bi == bi.toFloat)
 assert(bi == bi.toDouble)
}

The value 2^53 + 1 doesn't equal to 2^53 but the assertions below fails too

def testBigIntEquals2 {
 val l = 1L << 53
 val f = l.toFloat
 val d = l.toDouble
 val bi = BigInt(l + 1)
 assert(bi != f)
 assert(bi != d)
}

I would like the same for equality between primitives too, but I understand
the burden of compatibility with fuzzy Java definition of operators
float == int
double == long

@scabug
Copy link
Author

scabug commented Mar 29, 2012

Imported From: https://issues.scala-lang.org/browse/SI-5627?orig=1
Reporter: @nadezhin
Assignee: @nadezhin
Affected Versions: 2.9.1, 2.10.0
Other Milestones: 2.10.0

@scabug
Copy link
Author

scabug commented Mar 30, 2012

@nadezhin said:
Sorry, I found now that this bug is duplicate of #4540 and it has been fixed already by overriding isValidChar/isValidByte/isValidShort in BigInt/BigDecimal .

Nevertheless, there is still an issue with comparison of BigInt/BigDecimal with Float/Double .

def testBigIntEquals {
val bi = BigInt(1) << 64;
assert(bi == bi.toFloat);
assert(bi == bi.toDouble)
}

This test fails, though 2^64 is represented exactly in both Float and Double.
If you agree that this is a bug, then I can try to prepare a pull request that fixes comparison of BigInt/BigDecimal with Float/Double .

@scabug
Copy link
Author

scabug commented Apr 8, 2012

@nadezhin said:
Fix is in the commit 883ca63361a039f27e412eb0cb1bee7e9742a0bb
that was merged to master on Apr 01, 2012 .

@scabug
Copy link
Author

scabug commented Apr 8, 2012

@paulp said:
Thanks.

@scabug scabug closed this as completed Apr 8, 2012
@scabug scabug added this to the 2.10.0-M2 milestone Apr 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant