# Should conversion of BigFloat to Float be called rounding instead?

**URL:** https://discourse.julialang.org/t/should-conversion-of-bigfloat-to-float-be-called-rounding-instead/4287
**Category:** New to Julia
**Created:** [June 15, 2017, 10:59am UTC](https://discourse.julialang.org/t/should-conversion-of-bigfloat-to-float-be-called-rounding-instead/4287 "2017-06-15T10:59:20Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![mzaffalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzaffalon/32/214168_2.png) [@mzaffalon](https://discourse.julialang.org/u/mzaffalon)
#### Post date: [June 15, 2017, 10:59am UTC](https://discourse.julialang.org/t/should-conversion-of-bigfloat-to-float-be-called-rounding-instead/4287/1 "2017-06-15T10:59:20Z")

</div>

The conversion of a `Float64` to `Int` is allowed only if it exists an exact representation of that number as `Int`, otherwise the conversion throws an `InexactError`. This seems inconsistent with the conversion of a `BigFloat` to a `Float64`, see third example, which more than a conversion, it sounds like rounding to me.

```julia
julia> convert(Int, 1.0)
1

julia> convert(Int, 0.2)
ERROR: InexactError()
 in convert(::Type{Int64}, ::Float64) at .\int.jl:239

julia> convert(Float64, big"0.21221890538903469203434")
0.21221890538903468

```

---

<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 15, 2017, 11:13am UTC](https://discourse.julialang.org/t/should-conversion-of-bigfloat-to-float-be-called-rounding-instead/4287/2 "2017-06-15T11:13:12Z")

</div>

Although you make a good point, I believe (but am not sure) that conversions like this are required by the IEEE 754 standard on floating-point arithmetic.

Note that you can indeed use rounding with eg

Float64(x, RoundDown)

---

<div class="post-metadata">

### Author: ![mzaffalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzaffalon/32/214168_2.png) [@mzaffalon](https://discourse.julialang.org/u/mzaffalon)
#### Post date: [June 15, 2017, 11:37am UTC](https://discourse.julialang.org/t/should-conversion-of-bigfloat-to-float-be-called-rounding-instead/4287/3 "2017-06-15T11:37:20Z")

</div>

I am not sure what it is meant by `RoundDown`:

```julia
julia> Float64(big"0.123456789012345678", RoundUp)
0.12345678901234569

julia> Float64(big"0.123456789012345678", RoundDown)
0.12345678901234568

```

Unless it is a display issue, the last digit should be 8 and 7 respectively. The [docs](https://docs.julialang.org/en/latest/stdlib/math/#Base.Rounding.RoundDown) seem to imply this.

---

<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 15, 2017, 12:03pm UTC](https://discourse.julialang.org/t/should-conversion-of-bigfloat-to-float-be-called-rounding-instead/4287/4 "2017-06-15T12:03:31Z")

</div>

These round to the nearest Float64 in the given direction.

---

<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 15, 2017, 12:09pm UTC](https://discourse.julialang.org/t/should-conversion-of-bigfloat-to-float-be-called-rounding-instead/4287/5 "2017-06-15T12:09:08Z")

</div>

> [@mzaffalon](#):
>
> Float64(big"0.123456789012345678", RoundUp)

It is partly a display issue. The display of `Float64` uses an algorithm called “Grisu”, that displays the smallest number of digits possible. You can use `big(x)` to see the exact decimal representation of a given `Float64` (provided you use enough precision for the `BigFloat`):

```julia
julia> x = Float64(big"0.123456789012345678", RoundUp)
0.12345678901234569

julia> big(x)
1.234567890123456912476740399142727255821228027343750000000000000000000000000000e-01

julia> y = prevfloat(x)
0.12345678901234568

julia> big(y)
1.234567890123456773698862320998159702867269515991210937500000000000000000000000e-01

```
