# Bug in prevpow I guess

**URL:** <https://discourse.julialang.org/t/bug-in-prevpow-i-guess/72996>\
**Category:** General Usage\
**Tags:** bug\
**Created:** [December 12, 2021, 10:03pm UTC](https://discourse.julialang.org/t/bug-in-prevpow-i-guess/72996 "2021-12-12T22:03:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![reinermartin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/reinermartin/32/25574_2.png) [@reinermartin](https://discourse.julialang.org/u/reinermartin)\
**Post date:** [December 12, 2021, 10:03pm UTC](https://discourse.julialang.org/t/bug-in-prevpow-i-guess/72996/1 "2021-12-12T22:03:00Z")

</div>

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.

```julia
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.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [December 12, 2021, 10:22pm UTC](https://discourse.julialang.org/t/bug-in-prevpow-i-guess/72996/2 "2021-12-12T22:22:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [December 12, 2021, 10:38pm UTC](https://discourse.julialang.org/t/bug-in-prevpow-i-guess/72996/3 "2021-12-12T22:38:39Z")

</div>

[https://github.com/JuliaLang/julia/pull/43410#issuecomment-991984057](https://github.com/JuliaLang/julia/pull/43410#issuecomment-991984057)

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [December 12, 2021, 10:55pm UTC](https://discourse.julialang.org/t/bug-in-prevpow-i-guess/72996/4 "2021-12-12T22:55:28Z")

</div>

```julia
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.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [December 12, 2021, 10:56pm UTC](https://discourse.julialang.org/t/bug-in-prevpow-i-guess/72996/5 "2021-12-12T22:56:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [December 12, 2021, 10:56pm UTC](https://discourse.julialang.org/t/bug-in-prevpow-i-guess/72996/6 "2021-12-12T22:56:43Z")

</div>

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