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

make cmp with BigInt return in [-1, 0, 1] #28780

Merged
merged 2 commits into from
Aug 23, 2018
Merged

make cmp with BigInt return in [-1, 0, 1] #28780

merged 2 commits into from
Aug 23, 2018

Conversation

rfourquet
Copy link
Member

@rfourquet rfourquet commented Aug 20, 2018

This is to adhere to the documentation.

The added sign call doesn't seem to add overhead to the comparison functions using cmp. Adding this sign call for cmp(::BigInt, ::UInt) seems unnecessary according to the current GMP implementation of mpz_cmp_ui, but it's more maintenable to rely only on the documentation (which doesn't make garranties). A similar treatment could be made for MPFR, but my few tries didn't find a case where the values is not in [-1, 0, 1].

This is to adhere to the documentation.
@rfourquet rfourquet added bugfix This change fixes an existing bug bignums BigInt and BigFloat backport pending 1.0 labels Aug 20, 2018
@KristofferC KristofferC mentioned this pull request Aug 20, 2018
test/bigint.jl Outdated
bigrand() = rand(-big(2)^rand(1:rand(1:1000)):big(2)^rand(1:rand(1:1000)))
for T in (Base.BitInteger_types..., BigInt, Float64, Float32, Float16, BigFloat)
@test cmp(big(2)^130, one(T)) == 1
@test cmp(-big(2)^130, one(T)) == -1
Copy link
Member

@StefanKarpinski StefanKarpinski Aug 20, 2018

Choose a reason for hiding this comment

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

How about testing these with === to ensure that the type matches as well? And throw in some tests where the result is 0 as well...

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok, done. I removed the testing with BigFloat as this is handled in the MPFR module, and can return Int32. I can make a PR for MPFR if you think it's useful.

@KristofferC KristofferC merged commit 472c178 into master Aug 23, 2018
@KristofferC KristofferC deleted the rf/big-cmp branch August 23, 2018 10:02
KristofferC pushed a commit that referenced this pull request Aug 23, 2018
* make cmp with BigInt return in [-1, 0, 1]

(cherry picked from commit 472c178)
staticfloat pushed a commit that referenced this pull request Aug 24, 2018
* make cmp with BigInt return in [-1, 0, 1]
KristofferC pushed a commit that referenced this pull request Sep 8, 2018
* make cmp with BigInt return in [-1, 0, 1]

(cherry picked from commit 472c178)
KristofferC pushed a commit that referenced this pull request Sep 8, 2018
* make cmp with BigInt return in [-1, 0, 1]

(cherry picked from commit 472c178)
KristofferC pushed a commit that referenced this pull request Feb 11, 2019
* make cmp with BigInt return in [-1, 0, 1]

(cherry picked from commit 472c178)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bignums BigInt and BigFloat bugfix This change fixes an existing bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants