# Why isn't the "setrounding" function working the way I expect?

**URL:** <https://discourse.julialang.org/t/why-isnt-the-setrounding-function-working-the-way-i-expect/40891>\
**Category:** General Usage\
**Created:** [June 6, 2020, 8:48pm UTC](https://discourse.julialang.org/t/why-isnt-the-setrounding-function-working-the-way-i-expect/40891 "2020-06-06T20:48:06Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lucas\_Queiroz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucas_queiroz/32/12086_2.png) [@Lucas\_Queiroz](https://discourse.julialang.org/u/Lucas_Queiroz)\
**Post date:** [June 6, 2020, 8:48pm UTC](https://discourse.julialang.org/t/why-isnt-the-setrounding-function-working-the-way-i-expect/40891/1 "2020-06-06T20:48:06Z")

</div>

Why does Julia rounds 0.3 down in my code?

> julia\> setrounding(BigFloat, RoundUp)  
> MPFRRoundUp::MPFRRoundingMode = 2
> 
> julia\> BigFloat(0.3)  
> 0.299999999999999988897769753748434595763683319091796875

---

<div class="post-metadata">

**Author:** ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)\
**Post date:** [June 6, 2020, 8:53pm UTC](https://discourse.julialang.org/t/why-isnt-the-setrounding-function-working-the-way-i-expect/40891/2 "2020-06-06T20:53:58Z")

</div>

The rounding happens to 0.3, which is of type Float64, no?

---

<div class="post-metadata">

**Author:** ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)\
**Post date:** [June 6, 2020, 9:00pm UTC](https://discourse.julialang.org/t/why-isnt-the-setrounding-function-working-the-way-i-expect/40891/3 "2020-06-06T21:00:36Z")

</div>

Try `BigFloat("0.3")` and see if that’s better.

---

<div class="post-metadata">

**Author:** ![Lucas\_Queiroz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucas_queiroz/32/12086_2.png) [@Lucas\_Queiroz](https://discourse.julialang.org/u/Lucas_Queiroz)\
**Post date:** [June 6, 2020, 9:10pm UTC](https://discourse.julialang.org/t/why-isnt-the-setrounding-function-working-the-way-i-expect/40891/4 "2020-06-06T21:10:29Z")

</div>

Thank you, now it is working the way I expect.

> julia\> setrounding(BigFloat, RoundUp)  
> MPFRRoundUp::MPFRRoundingMode = 2
> 
> julia\> BigFloat(“0.3”)  
> 0.3000000000000000000000000000000000000000000000000000000000000000000000000000017

---

<div class="post-metadata">

**Author:** ![Lucas\_Queiroz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucas_queiroz/32/12086_2.png) [@Lucas\_Queiroz](https://discourse.julialang.org/u/Lucas_Queiroz)\
**Post date:** [June 6, 2020, 9:40pm UTC](https://discourse.julialang.org/t/why-isnt-the-setrounding-function-working-the-way-i-expect/40891/6 "2020-06-06T21:40:25Z")

</div>

Here Julia is also rounding down, can you help me again?

> julia\> setrounding(BigFloat, RoundUp)  
> MPFRRoundUp::MPFRRoundingMode = 2
> 
> julia\> BigFloat(3/10)  
> 0.299999999999999988897769753748434595763683319091796875

---

<div class="post-metadata">

**Author:** ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)\
**Post date:** [June 7, 2020, 12:45am UTC](https://discourse.julialang.org/t/why-isnt-the-setrounding-function-working-the-way-i-expect/40891/7 "2020-06-07T00:45:20Z")

</div>

I think your mental model of the order of operations is still a bit off. When you run `BigFloat(3 / 10)`, the first thing that happens is computing `3 / 10`. That operation is not affected in any way by the context of being inside a call to `BigFloat`. But once `3 / 10` has run, the approximation error that confuses you has already been introduced and can’t ever be undone by calling `BigFloat`. In fact, none of your examples so far have anything to do with calling `setrounding(BigFloat, RoundUp)`: you’re just seeing the effects of [applicative order evaluation](https://en.wikipedia.org/wiki/Evaluation_strategy). To see it more clearly, spend time understanding what happens here:

```julia
function f(x)
    println("f!")
    x
end

function g(x)
    println("g!")
    x
end

f(g(1))

```

You should do something like `BigInt(3) / BigInt(10)` or `big"3" / big"10"`.

---

<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:** [June 7, 2020, 12:52am UTC](https://discourse.julialang.org/t/why-isnt-the-setrounding-function-working-the-way-i-expect/40891/8 "2020-06-07T00:52:47Z")

</div>

`BigFloat(3//10)` would also work as `3//10` is a `Fraction` which is exact.

---

<div class="post-metadata">

**Author:** ![jlapeyre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlapeyre/32/4514_2.png) [@jlapeyre](https://discourse.julialang.org/u/jlapeyre)\
**Post date:** [June 7, 2020, 1:19am UTC](https://discourse.julialang.org/t/why-isnt-the-setrounding-function-working-the-way-i-expect/40891/9 "2020-06-07T01:19:48Z")

</div>

`Float64` uses a binary representation, and it represents a discrete, not continuous set of numbers. `3/10` cannot be represented by a binary number with a finite number of bits. So when you type `0.3` or `3/10`, you get the binary number that can be represented as a `Float64` and is closest to three tenths. Then you are converting this approximation to a `BigFloat`.

You don’t see the problem here

```julia
julia> BigFloat(1/8)
0.125

```

because one eighth can be represented exactly as a `Float64`.

---

<div class="post-metadata">

**Author:** ![Lucas\_Queiroz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucas_queiroz/32/12086_2.png) [@Lucas\_Queiroz](https://discourse.julialang.org/u/Lucas_Queiroz)\
**Post date:** [June 7, 2020, 1:49am UTC](https://discourse.julialang.org/t/why-isnt-the-setrounding-function-working-the-way-i-expect/40891/10 "2020-06-07T01:49:52Z")

</div>

Thank you for explaining, now I understand better how evaluation works in Julia.

---

<div class="post-metadata">

**Author:** ![Lucas\_Queiroz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucas_queiroz/32/12086_2.png) [@Lucas\_Queiroz](https://discourse.julialang.org/u/Lucas_Queiroz)\
**Post date:** [June 7, 2020, 1:52am UTC](https://discourse.julialang.org/t/why-isnt-the-setrounding-function-working-the-way-i-expect/40891/11 "2020-06-07T01:52:07Z")

</div>

Yes, BigFloat(3//10) works, I got the output I wanted

> julia\> setrounding(BigFloat, RoundUp)  
> MPFRRoundUp::MPFRRoundingMode = 2
> 
> julia\> BigFloat(3//10)  
> 0.30000000000000000000000000000000000015

---

<div class="post-metadata">

**Author:** ![Lucas\_Queiroz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucas_queiroz/32/12086_2.png) [@Lucas\_Queiroz](https://discourse.julialang.org/u/Lucas_Queiroz)\
**Post date:** [June 7, 2020, 2:08am UTC](https://discourse.julialang.org/t/why-isnt-the-setrounding-function-working-the-way-i-expect/40891/12 "2020-06-07T02:08:19Z")

</div>

When I type any of the following expressions: ‘4.1’, ‘8.1’, ‘129.0’, Julia evaluates then as Float64. Is there any macros that makes Julia evaluate those expressions as BigFloat? Because not only is writing ‘4.1’ more comfortable than writing 'BigFloat(“4.1”), but also makes my code more readable.

---

<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:** [June 7, 2020, 2:18am UTC](https://discourse.julialang.org/t/why-isnt-the-setrounding-function-working-the-way-i-expect/40891/13 "2020-06-07T02:18:08Z")

</div>

You don’t need a macro, just `big"4.1"`

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [June 7, 2020, 2:45am UTC](https://discourse.julialang.org/t/why-isnt-the-setrounding-function-working-the-way-i-expect/40891/14 "2020-06-07T02:45:03Z")

</div>

That is actually implemented as the `@big_str` macro: [Metaprogramming · The Julia Language](https://docs.julialang.org/en/v1/manual/metaprogramming/#Non-Standard-String-Literals-1)

---

<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:** [June 7, 2020, 3:07am UTC](https://discourse.julialang.org/t/why-isnt-the-setrounding-function-working-the-way-i-expect/40891/15 "2020-06-07T03:07:06Z")

</div>

Oh, that’s awesome! Thanks for sharing!
