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

ndigits with negative base: error out instead of giving incorrect result #29148

Merged
merged 2 commits into from
Sep 13, 2018

Conversation

rfourquet
Copy link
Member

Because of an unchecked conversion via signed (instead of Signed),
we were giving wrong results, e.g. we had
ndigits(typemax(UInt), base=-2) != ndigits(big(typemax(UInt)), base=-2)

Because of an unchecked conversion via `signed` (instead of `Signed`),
we were giving wrong results, e.g. we had
`ndigits(typemax(UInt), base=-2) != ndigits(big(typemax(UInt)), base=-2)`
@rfourquet rfourquet added the kind:bugfix This change fixes an existing bug label Sep 12, 2018
@rfourquet
Copy link
Member Author

What label would fit here? I would say "math" would be the closest, but not quite...

@StefanKarpinski
Copy link
Sponsor Member

Could we just give the correct answer here? Maybe by widening to a larger type? It may also be possible to tweak this computation for negative bases somehow instead.

@StefanKarpinski
Copy link
Sponsor Member

StefanKarpinski commented Sep 12, 2018

So, I think this definition might work:

ndigits0znb(x::Unsigned, b::Integer) = ndigits0znb(-signed(fld(x, -b)), b) + (x != 0)

It relies on the fact that cld(x, b) == -fld(x, -b) but does the conversion from unsigned to signed before negating the unsigned quotient; since -b ≥ 2 the quotient always fits in the signed type.

@rfourquet
Copy link
Member Author

rfourquet commented Sep 13, 2018

Oh I was too lazy, but your idea is clever! I pushed it here to launch CI, but please re-commit in your name!

@StefanKarpinski StefanKarpinski merged commit 77ec1ec into master Sep 13, 2018
@StefanKarpinski StefanKarpinski deleted the rf/ndigit0zsnb-unsigned branch September 13, 2018 19:32
rfourquet added a commit that referenced this pull request Sep 14, 2018
KristofferC pushed a commit that referenced this pull request Sep 17, 2018
It relies on the fact that `cld(x, b) == -fld(x, -b)` but does the conversion from unsigned to signed before negating the unsigned quotient; since `-b ≥ 2` the quotient always fits in the signed type.

(cherry picked from commit 77ec1ec)
@KristofferC KristofferC mentioned this pull request Sep 17, 2018
KristofferC pushed a commit that referenced this pull request Feb 11, 2019
It relies on the fact that `cld(x, b) == -fld(x, -b)` but does the conversion from unsigned to signed before negating the unsigned quotient; since `-b ≥ 2` the quotient always fits in the signed type.

(cherry picked from commit 77ec1ec)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bugfix This change fixes an existing bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants