# BigFloat converted to string returns different number?

**URL:** <https://discourse.julialang.org/t/bigfloat-converted-to-string-returns-different-number/86473>\
**Category:** Numerics\
**Tags:** numbers, bigfloat\
**Created:** [August 28, 2022, 9:40pm UTC](https://discourse.julialang.org/t/bigfloat-converted-to-string-returns-different-number/86473 "2022-08-28T21:40:59Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![HelloThere](https://avatars.discourse-cdn.com/v4/letter/h/9de053/32.png) [@HelloThere](https://discourse.julialang.org/u/HelloThere)\
**Post date:** [August 28, 2022, 9:40pm UTC](https://discourse.julialang.org/t/bigfloat-converted-to-string-returns-different-number/86473/1 "2022-08-28T21:40:59Z")

</div>

Hi,

I am new to Julia and in the documentation I came across this:

```julia
julia> big"1.23456789012345678901"
1.234567890123456789010000000000000000000000000000000000000000000000000000000004

```

At first I thought that this is some sort of displaying error but then I tried this:

```julia
julia> string(big"1.23456789012345678901")
"1.234567890123456789010000000000000000000000000000000000000000000000000000000004"

```

How does “1.23456789012345678901” turn into “1.234567890123456789010000000000000000000000000000000000000000000000000000000004”?

I know that normal floats aren’t precise because they can’t be represented exactly but isn’t BigFloat supposed to fix this?

Can someone please explain what is going on here?

Thanks

---

<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:** [August 28, 2022, 9:54pm UTC](https://discourse.julialang.org/t/bigfloat-converted-to-string-returns-different-number/86473/2 "2022-08-28T21:54:03Z")

</div>

`BigFloat` is still a binary format, just with more bits (256 by default). Most decimal expansions have infinite length in binary. If you want to represent decimal numbers exactly, you need to use a different number type.

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [August 28, 2022, 10:27pm UTC](https://discourse.julialang.org/t/bigfloat-converted-to-string-returns-different-number/86473/3 "2022-08-28T22:27:55Z")

</div>

To slightly elaborate on Oscar’s answer and more directly address the question “what is going on here?”, `1.234567890123456789010000000000000000000000000000000000000000000000000000000004` is the closest arbitrary-precision (well, whatever precision you’re currently using, presumably 256-bit, the default) that can represent the real number `1.23456789012345678901`. You can see that both the previous and the following floating-point numbers are much further away from `1.23456789012345678901` than `1.234567890123456789010000000000000000000000000000000000000000000000000000000004` is:

```julia
julia> x = big"1.23456789012345678901"
1.234567890123456789010000000000000000000000000000000000000000000000000000000004

julia> prevfloat(x)
1.234567890123456789009999999999999999999999999999999999999999999999999999999987

julia> nextfloat(x)
1.234567890123456789010000000000000000000000000000000000000000000000000000000021

```

---

<div class="post-metadata">

**Author:** ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)\
**Post date:** [August 28, 2022, 10:37pm UTC](https://discourse.julialang.org/t/bigfloat-converted-to-string-returns-different-number/86473/4 "2022-08-28T22:37:00Z")

</div>

To add a simple example to the previous answers: The underlying problem is approximating a decimal fraction in binary terms can be infinite, like this one:  
\frac{1}{10} =\frac{1}{16} + \frac{1}{32} + \frac{1}{256} + \frac{1}{512} + \frac{1}{4096} + ...

If you like math, you can even proof that, e.g. if \frac{1}{10} = \frac{n}{2^k} then 2^k = 10n and then 2 | 5.)

---

<div class="post-metadata">

**Author:** ![HelloThere](https://avatars.discourse-cdn.com/v4/letter/h/9de053/32.png) [@HelloThere](https://discourse.julialang.org/u/HelloThere)\
**Post date:** [August 28, 2022, 10:49pm UTC](https://discourse.julialang.org/t/bigfloat-converted-to-string-returns-different-number/86473/5 "2022-08-28T22:49:24Z")

</div>

Thanks. Then I misunderstood.  
I thought it would be an exact decimal representation.  
Do you know a different number type in Julia that implements it exactly or should I use rationals instead for calculations and convert the result back into a 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:** [August 28, 2022, 10:50pm UTC](https://discourse.julialang.org/t/bigfloat-converted-to-string-returns-different-number/86473/6 "2022-08-28T22:50:49Z")

</div>

If you want decimal arithmetic, you should check out [GitHub - JuliaMath/Decimals.jl: Pure Julia decimal arithmetic library.](https://github.com/JuliaMath/Decimals.jl). Rational arithmetic also works, but can overflow in some situations.

---

<div class="post-metadata">

**Author:** ![HelloThere](https://avatars.discourse-cdn.com/v4/letter/h/9de053/32.png) [@HelloThere](https://discourse.julialang.org/u/HelloThere)\
**Post date:** [August 28, 2022, 10:59pm UTC](https://discourse.julialang.org/t/bigfloat-converted-to-string-returns-different-number/86473/7 "2022-08-28T22:59:05Z")

</div>

Thanks.  
I didn’t know that prevfloat and nextfloat existed.

---

<div class="post-metadata">

**Author:** ![HelloThere](https://avatars.discourse-cdn.com/v4/letter/h/9de053/32.png) [@HelloThere](https://discourse.julialang.org/u/HelloThere)\
**Post date:** [August 28, 2022, 11:14pm UTC](https://discourse.julialang.org/t/bigfloat-converted-to-string-returns-different-number/86473/8 "2022-08-28T23:14:08Z")

</div>

Thanks for the example that makes it obvious. But what do you mean with the 2|5?

---

<div class="post-metadata">

**Author:** ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)\
**Post date:** [August 28, 2022, 11:20pm UTC](https://discourse.julialang.org/t/bigfloat-converted-to-string-returns-different-number/86473/9 "2022-08-28T23:20:47Z")

</div>

Ops, “2 | 5” was my notation for “2 divides 5”.
