# floor(BigFloat(27)^(1/3)) = 2.0; Bug in BigFloat?

**URL:** <https://discourse.julialang.org/t/floor-bigfloat-27-1-3-2-0-bug-in-bigfloat/37123>\
**Category:** Numerics\
**Tags:** question\
**Created:** [April 6, 2020, 6:27pm UTC](https://discourse.julialang.org/t/floor-bigfloat-27-1-3-2-0-bug-in-bigfloat/37123 "2020-04-06T18:27:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Syam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/syam/32/10820_2.png) [@Syam](https://discourse.julialang.org/u/Syam)\
**Post date:** [April 6, 2020, 6:27pm UTC](https://discourse.julialang.org/t/floor-bigfloat-27-1-3-2-0-bug-in-bigfloat/37123/1 "2020-04-06T18:27:07Z")

</div>

I encountered a bug in my code while using `floor`. With further debugging, I realized that the issue is with `BigFloat`. See below.

```julia
julia> floor(BigFloat(27)^(1/3))
2.0

julia> floor(Float64(27)^(1/3))
3.0

julia> ceil(BigFloat(32)^(1/5))
3.0

julia> ceil(Float64(32)^(1/5))
2.0

```

In fact, if you define following functions

```julia
julia> f(s, n) = floor((s^n)^(1/n)); c(s, n) = ceil((s^n)^(1/n));

```

then both `f(s, n) = c(s, n) = s` for all `n`. But this is not true when `s` is `BigFloat` as shown below.

```julia
julia> println(map(n -> f(BigFloat(2), n), 1:100))
BigFloat[2.0, 2.0, 1.0, 2.0, 2.0, 1.0, 1.0, 2.0, 1.0, 2.0, 2.0, 1.0, 2.0, 1.0, 1.0, 2.0, 1.0, 1.0, 1.0, 2.0, 1.0, 2.0, 1.0, 1.0, 2.0, 2.0, 1.0, 1.0, 1.0, 1.0, 1.0, 2.0, 2.0, 1.0, 1.0, 1.0, 2.0, 1.0, 1.0, 2.0, 2.0, 1.0, 1.0, 2.0, 2.0, 1.0, 1.0, 1.0, 1.0, 2.0, 1.0, 2.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 2.0, 1.0, 1.0, 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0, 2.0, 2.0, 2.0, 2.0, 3.0, 2.0, 2.0, 2.0, 3.0, 3.0, 2.0, 2.0, 3.0, 2.0, 3.0, 2.0, 2.0, 3.0, 3.0, 2.0, 3.0, 2.0, 2.0, 3.0, 2.0, 3.0, 3.0, 2.0, 2.0, 2.0, 2.0, 3.0, 2.0, 3.0, 3.0, 2.0, 3.0, 2.0, 2.0, 2.0, 2.0, 2.0, 3.0, 3.0]

julia> println(map(n -> c(Float64(2), n), 1:100))
[2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 
2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0]

```

---

<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:** [April 6, 2020, 6:35pm UTC](https://discourse.julialang.org/t/floor-bigfloat-27-1-3-2-0-bug-in-bigfloat/37123/2 "2020-04-06T18:35:01Z")

</div>

This is caused by `1/3` and `1/5` being inexact (since they are `Float64`s and slightly less than the true values. You probably want something like `ceil((32)^(BigFloat(1)/5))` which will make sure your fractions are computed as `BigFloats`. Note, you could also write `BigFloat(1)` as `big"1"` which imo is more readable.

---

<div class="post-metadata">

**Author:** ![Syam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/syam/32/10820_2.png) [@Syam](https://discourse.julialang.org/u/Syam)\
**Post date:** [April 6, 2020, 7:03pm UTC](https://discourse.julialang.org/t/floor-bigfloat-27-1-3-2-0-bug-in-bigfloat/37123/3 "2020-04-06T19:03:25Z")

</div>

Thanks @Oscar_Smith. I understand now.

Since fractions `1/n` are by default promoted to `Float64`, `s` of type `Float64` or below work fine with `s^(1/n)` except `s` of type `BigFloat`. So, I need fractions `1/n` be of `BigFloat` to be able to use `s` of type `BigFloat`.

---

<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:** [April 6, 2020, 7:06pm UTC](https://discourse.julialang.org/t/floor-bigfloat-27-1-3-2-0-bug-in-bigfloat/37123/4 "2020-04-06T19:06:09Z")

</div>

It’s a little more subtle than that. your code promotes the float `1/n` to a bigfloat before taking the power. The problem is that the float `1/n` is not equal to the mathematical quantity `1/n`, and to make it be closer, you need the division to use more accurate arithmetic.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [April 6, 2020, 8:42pm UTC](https://discourse.julialang.org/t/floor-bigfloat-27-1-3-2-0-bug-in-bigfloat/37123/5 "2020-04-06T20:42:49Z")

</div>

> [@Oscar\_Smith](#):
>
> since they are `Float64` s and slightly less than the true values

Making them `BigFloat` won’t make them exact either.
