# Promotion to BigFloat -- no zero padding

**URL:** <https://discourse.julialang.org/t/promotion-to-bigfloat-no-zero-padding/66042>\
**Category:** General Usage\
**Tags:** bigfloat\
**Created:** [August 8, 2021, 8:39pm UTC](https://discourse.julialang.org/t/promotion-to-bigfloat-no-zero-padding/66042 "2021-08-08T20:39:50Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![LolianSh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/loliansh/32/10834_2.png) [@LolianSh](https://discourse.julialang.org/u/LolianSh)\
**Post date:** [August 8, 2021, 8:39pm UTC](https://discourse.julialang.org/t/promotion-to-bigfloat-no-zero-padding/66042/1 "2021-08-08T20:39:50Z")

</div>

Hi,

I am encountering some problems when promoting Float64 to a BigFloat, specifically the fact that I do not get a zero padding beyond the Float64 precision.  
For example I have the following:

```julia
julia> BigFloat(0.01)
0.01000000000000000020816681711721685132943093776702880859375

julia> BigFloat(1)/100
0.009999999999999999999999999999999999999999999999999999999999999999999999999999995

```

Do you guys know of any fix or is this the normal behaviour of BigFloat?

---

<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:** [August 8, 2021, 8:47pm UTC](https://discourse.julialang.org/t/promotion-to-bigfloat-no-zero-padding/66042/2 "2021-08-08T20:47:49Z")

</div>

try:

```julia
julia> big"0.1"
0.1000000000000000000000000000000000000000000000000000000000000000000000000000002

```

oops, it’s 0.01 not 0.1. Looks like big(1)/100 is the closest you can get.

---

<div class="post-metadata">

**Author:** ![LolianSh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/loliansh/32/10834_2.png) [@LolianSh](https://discourse.julialang.org/u/LolianSh)\
**Post date:** [August 8, 2021, 8:56pm UTC](https://discourse.julialang.org/t/promotion-to-bigfloat-no-zero-padding/66042/3 "2021-08-08T20:56:10Z")

</div>

Sure, that works:

```julia
julia> big"0.1"
0.1000000000000000000000000000000000000000000000000000000000000000000000000000002

julia> parse(BigFloat, string(0.01))
0.009999999999999999999999999999999999999999999999999999999999999999999999999999995

julia> parse(BigFloat, string(0.1))
0.1000000000000000000000000000000000000000000000000000000000000000000000000000002

```

but converting to a string and then back to BigFloat seems like a hassle and has overhead:

```julia
julia> @benchmark BigFloat(x) setup=(x=rand())
BechmarkTools.Trial: 10000 samples with 958 evaluations.
 Range (min … max): 89.468 ns … 1.547 μs ┊ GC (min … max): 0.00% … 93.54%
 Time (median): 99.712 ns ┊ GC (median): 0.00%
 Time (mean ± σ): 104.143 ns ± 60.618 ns ┊ GC (mean ± σ): 2.72% ± 4.43%

        ▁▃▅▆▇█▇▅▂                                               
  ▅▄▆▅▆███████████▅▂▂▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▂▂▂▁▂▁ ▃
  89.5 ns Histogram: frequency by time 147 ns <

 Memory estimate: 112 bytes, allocs estimate: 2.

julia> @benchmark BigFloat(string(x)) setup=(x=rand())
BechmarkTools.Trial: 10000 samples with 175 evaluations.
 Range (min … max): 633.166 ns … 7.969 μs ┊ GC (min … max): 0.00% … 90.84%
 Time (median): 697.649 ns ┊ GC (median): 0.00%
 Time (mean ± σ): 721.763 ns ± 281.261 ns ┊ GC (mean ± σ): 1.71% ± 4.09%

            ▄██▆▃                                                
  ▁▁▁▁▂▃▄▅▆███████▆▄▃▃▂▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▂▂▁▁▁▁▁ ▂
  633 ns Histogram: frequency by time 945 ns <

 Memory estimate: 557 bytes, allocs estimate: 5.

```

I thought of asking here is anyone knew of a workaround for now. But I will open an issue on GitHub to report the problem as well.

Anyway, thank you @jling

---

<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:** [August 8, 2021, 8:58pm UTC](https://discourse.julialang.org/t/promotion-to-bigfloat-no-zero-padding/66042/4 "2021-08-08T20:58:05Z")

</div>

You don’t need to go to string, `big(1)/100` gives you the same thing no?

I don’t think it’s a problem, 0.01 is just not representable in Float, what you get is close enough and you can always increase precision.

Also remember you have `1//100` if you need

---

<div class="post-metadata">

**Author:** ![LolianSh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/loliansh/32/10834_2.png) [@LolianSh](https://discourse.julialang.org/u/LolianSh)\
**Post date:** [August 8, 2021, 9:04pm UTC](https://discourse.julialang.org/t/promotion-to-bigfloat-no-zero-padding/66042/5 "2021-08-08T21:04:05Z")

</div>

Yes, `big(1)/100` is correct and no need for strings, but if one wants consistent zero-padding for a random Float64 then the conversion to string and then BigFloat seems to work consistently:

```julia
julia> a = rand()
0.6978961477555439

julia> BigFloat(a)
0.69789614775554387193778893561102449893951416015625

julia> BigFloat(string(a))
0.6978961477555438999999999999999999999999999999999999999999999999999999999999987

```

---

<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 8, 2021, 10:49pm UTC](https://discourse.julialang.org/t/promotion-to-bigfloat-no-zero-padding/66042/6 "2021-08-08T22:49:06Z")

</div>

Float64 is binary, not decimal. The values that are printed aren’t the real value.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [August 8, 2021, 11:09pm UTC](https://discourse.julialang.org/t/promotion-to-bigfloat-no-zero-padding/66042/7 "2021-08-08T23:09:05Z")

</div>

See [https://0.30000000000000004.com/](https://0.30000000000000004.com/)

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [August 8, 2021, 11:24pm UTC](https://discourse.julialang.org/t/promotion-to-bigfloat-no-zero-padding/66042/8 "2021-08-08T23:24:59Z")

</div>

There’s also:

> **[GitHub - JuliaMath/Decimals.jl: Pure Julia decimal arithmetic library.](https://github.com/JuliaMath/Decimals.jl)**
>
> Pure Julia decimal arithmetic library. Contribute to JuliaMath/Decimals.jl development by creating an account on GitHub.

See, here, maybe you don’t want to use it:

> [@Computations with decimals (arbitrary precision)](https://discourse.julialang.org/t/computations-with-decimals-arbitrary-precision/65534):
>
> Hello everybody ! I’m busy now with studying the speed of convergence of sequences of real numbers. I begin to study classical algorithms like Archimedes polygonal approximation of pi, and newton computations of a square root. I already illustrated with some simple python programs and I want to translate into Julia . Here’s an example : from decimal import Decimal, getcontext def u(n): if n == 0: return Decimal(1) return (u(n - 1) + (2 - u(n - 1) \* u(n - 1)) / (2 \* u(n …

---

<div class="post-metadata">

**Author:** ![blackeneth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blackeneth/32/10353_2.png) [@blackeneth](https://discourse.julialang.org/u/blackeneth)\
**Post date:** [August 9, 2021, 12:47am UTC](https://discourse.julialang.org/t/promotion-to-bigfloat-no-zero-padding/66042/9 "2021-08-09T00:47:44Z")

</div>

big() and BigFloat() are converting a Float64 value to 256 bits (default). Float64 is stored in IEEE 754 format.

```julia
julia> x=0.01 # Float64
0.01

julia> y=BigFloat(x)
0.01000000000000000020816681711721685132943093776702880859375

julia> bitstring(x)
"0011111110000100011110101110000101000111101011100001010001111011"

```

If we decode that bitstring according to IEEE 754 format, we can see the exact value it’s encoding:

| **Part** | **Binary** | **Decimal** |
| --- | --- | --- |
| sign | 0 | +1 |
| Exponent | 01111111000 | 1016 |
| Fraction | 0100011110101110000101000111101011100001010001111011 | 0.2800000000000000266453525910037569701671600341796875 |
| Exact | | 0.01000000000000000020816681711721685132943093776702880859375 |

We see that the IEEE 754 binary representation is storing the exact value of 0.01000000000000000020816681711721685132943093776702880859375, which is what was converted to BigFloat.

Float64 has 15.95 digits of precision. The IEEE 754 representation of 0.01 achieves that:

![IEEE001](https://global.discourse-cdn.com/julialang/original/3X/8/d/8d77cf0bd667098388db80a1b3919167b48a9355.png)

However, for your BigFloat values, you probably want the closest value to 0.01.

```julia
julia> zu=BigFloat("0.01",RoundUp)
0.01000000000000000000000000000000000000000000000000000000000000000000000000000013

julia> zd=BigFloat("0.01",RoundDown)
0.009999999999999999999999999999999999999999999999999999999999999999999999999999995

julia> zn=BigFloat("0.01",RoundNearest)
0.009999999999999999999999999999999999999999999999999999999999999999999999999999995

```

The rounded up value of 0.01 has an error of -1.3E-79, whereas the rounded down value has an error of 5E-81. So the rounded down value is nearest to 0.01.
