-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Conversation
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)`
What label would fit here? I would say "math" would be the closest, but not quite... |
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. |
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 |
Oh I was too lazy, but your idea is clever! I pushed it here to launch CI, but please re-commit in your name! |
c67ca34
to
d2c92b8
Compare
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)
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)
Because of an unchecked conversion via
signed
(instead ofSigned
),we were giving wrong results, e.g. we had
ndigits(typemax(UInt), base=-2) != ndigits(big(typemax(UInt)), base=-2)