Skip to content

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 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
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
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!

@rfourquet rfourquet force-pushed the rf/ndigit0zsnb-unsigned branch from c67ca34 to d2c92b8 Compare September 13, 2018 10:18
@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

bugfix This change fixes an existing bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants