Bug in prevpow I guess

Hi, what is the right place to report the following?

I think I stumbled onto a simple ‘bug’ (= very unexpected behavior) in prevpow (when solving the latest Project Euler problem). Using Julia 1.7.

prevpow(1234567890123456789)

gives -8446744073709551616 instead of the expected 1000000000000000000, even though the argument is well below typemax(Int). I suspect that the next power of 10 is involved, which causes an overflow (certain looks this way to me having a cursory glance at the source for prevpow.

Not sure whether this is worth fixing, as it is quite an edge case and can be attributed to overflow behavior, but as a Project Euler fan it is slightly irritating to me.

ah, yeah. The problem is the next power of 10 overflows since it uses an integer power internally. Fixing this properly is probably a little tricky. That said, I’ll make a PR with an attempt to fix this.

2 Likes

https://github.com/JuliaLang/julia/pull/43410#issuecomment-991984057

julia> prevpow(2,1)
1

I would expect this to be 0 just looking at documentation:

prevpow(a, x)
The largest a^n not greater than x, where n is a non-negative integer.

Why? 0 isn’t a power of 2, and 1 is a power of 2 not greater than 1.

ah, I thought it returns the power itself because “previous power”… my bad