# Computation precision with Float

**URL:** <https://discourse.julialang.org/t/computation-precision-with-float/91180>\
**Category:** General Usage\
**Tags:** precision\
**Created:** [December 3, 2022, 3:11pm UTC](https://discourse.julialang.org/t/computation-precision-with-float/91180 "2022-12-03T15:11:09Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![dgagnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dgagnon/32/45466_2.png) [@dgagnon](https://discourse.julialang.org/u/dgagnon)\
**Post date:** [December 3, 2022, 3:11pm UTC](https://discourse.julialang.org/t/computation-precision-with-float/91180/1 "2022-12-03T15:11:09Z")

</div>

I see strange behavior for the computation of a very simple formula:

```julia
julia> rounding(Float64)
RoundingMode{:Nearest}()
julia> p=0.8
0.8
julia> 1-p
0.19999999999999996

```

I have the same result with Julia 1.8.0 on Windows10 or with Julia 1.8.1 on Linux.  
Is that the expected behavior?  
Even stranger:

```julia
julia> q=0.2
0.2
julia> 1-q
0.8

```

Is there a well known way to manage these rounding errors or should I systematically make rounding adjustments in my code? Is the “Nearest” method of rounding the most appropriate?  
Rational numbers seem to behave better than Float.

```julia
julia > p = Rational(8,10)
4//5
julia> 1-p
1//5
julia> q = Float64(1-p)
0.2

```

In the specific kind of problem I am working on, I will not use numbers with absolute value larger than 1.  
Any recommendation?

---

<div class="post-metadata">

**Author:** ![ImreSamu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/imresamu/32/20677_2.png) [@ImreSamu](https://discourse.julialang.org/u/ImreSamu)\
**Post date:** [December 3, 2022, 3:40pm UTC](https://discourse.julialang.org/t/computation-precision-with-float/91180/2 "2022-12-03T15:40:22Z")

</div>

> [@dgagnon](#):
>
> Is there a well known way to manage these rounding errors

The `BigFloat` operations can be customized with a `setprecision` and `setrounding` .  
see: → [Arbitrary Precision Arithmetic](https://docs.julialang.org/en/v1/manual/integers-and-floating-point-numbers/#Arbitrary-Precision-Arithmetic)  
Would that solve your problem?

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [December 3, 2022, 4:01pm UTC](https://discourse.julialang.org/t/computation-precision-with-float/91180/3 "2022-12-03T16:01:01Z")

</div>

You might be looking for [https://0.30000000000000004.com](https://0.30000000000000004.com) .

Inserting rounding at intermediate steps of the calculation is likely to reduce accuracy, unless you have reason to believe all intermediate steps will be nice numbers in decimal. In which case you may want something like DecFP.jl or Decimals.jl or FixedPointNumbers.jl.

---

<div class="post-metadata">

**Author:** ![dgagnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dgagnon/32/45466_2.png) [@dgagnon](https://discourse.julialang.org/u/dgagnon)\
**Post date:** [December 3, 2022, 4:05pm UTC](https://discourse.julialang.org/t/computation-precision-with-float/91180/4 "2022-12-03T16:05:05Z")

</div>

Is the speed of calculation for BigFloat the same as for Float64?  
I am not so much looking for many decimals. But I’d like exact results when dealing with simple numbers.

As pointed, Rational numbers seem better for my case but I’m not sure speed is comparable to Float64. Further, since I am doing trigonometric functions on these numbers, I am not sure that these imply a hidden conversion to Float64.

---

<div class="post-metadata">

**Author:** ![dgagnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dgagnon/32/45466_2.png) [@dgagnon](https://discourse.julialang.org/u/dgagnon)\
**Post date:** [December 3, 2022, 4:08pm UTC](https://discourse.julialang.org/t/computation-precision-with-float/91180/5 "2022-12-03T16:08:23Z")

</div>

Probably I should not worry about this and simply perform appropriate rounding when printing numbers.

Thanks for the pointers. I’ll look at that.

---

<div class="post-metadata">

**Author:** ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)\
**Post date:** [December 3, 2022, 4:29pm UTC](https://discourse.julialang.org/t/computation-precision-with-float/91180/6 "2022-12-03T16:29:54Z")

</div>

This is the expected behaviour of floating point representations. The problem being that decimals `0.2` and `0.8` cannot be represented exactly in the binary float format of `Float64` (IEEE 754 standard). The link provided by @mcabbott is a good starting point. For all the glorious details click on " What Every Computer Scientist Should Know About Floating-Point Arithmetic" on that page.

```julia
julia> rationalize(0.2; tol=0) # Convert to rational exactly (must be possible as Float64 precision is finite)
3602879701896397//18014398509481984

julia> rationalize(1 - 0.8; tol=0)
900719925474099//4503599627370496

```

Rational numbers are exact using integers as numerator and denominator. Yet, analytic functions such as `sin` or `exp` are not necessarily rational on rational arguments, i.e., can only be approximated on a computer anyways.

```julia
julia> x = 2//10
1//5

julia> (x.num, x.den)
(1, 5)

julia> rationalize(Float64(x); tol=0) # Float64 cannot represent 0.2 = 2/10 exactly
3602879701896397//18014398509481984

```

In any case, computers are restricted to a countable subset of the real numbers, namely the [computable numbers](https://en.wikipedia.org/wiki/Computable_number). Had once seen a Common Lisp implementation of such where each number was represented by a function that could be run to provide an approximation at the desired precision. Efficiency of such a representation is very low and not many use cases require such fine control about the precision of approximations.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [December 3, 2022, 5:24pm UTC](https://discourse.julialang.org/t/computation-precision-with-float/91180/7 "2022-12-03T17:24:09Z")

</div>

> [@dgagnon](#):
>
> Is the speed of calculation for BigFloat the same as for Float64?

If they were, we would not need Float64 at all 😉

I bet they are at least one to two orders of magnitude slower for most things

---

<div class="post-metadata">

**Author:** ![John\_Gibson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/john_gibson/32/5321_2.png) [@John\_Gibson](https://discourse.julialang.org/u/John_Gibson)\
**Post date:** [December 3, 2022, 7:18pm UTC](https://discourse.julialang.org/t/computation-precision-with-float/91180/8 "2022-12-03T19:18:48Z")

</div>

A brief overview of the issues:

> [@PSA: floating-point arithmetic](https://discourse.julialang.org/t/psa-floating-point-arithmetic/8678):
>
> Sometimes people are surprised by the results of floating-point calculations such as julia\> 5/6 0.8333333333333334 # shouldn't the last digit be 3? julia\> 2.6 - 0.7 - 1.9 2.220446049250313e-16 # shouldn't the answer be 0? These are not bugs in Julia. They’re consequences of the IEEE-standard 64-bit binary representation of floating-point numbers that is burned into computer hardware, which Julia and many other languages use by default. Brief explanation You can t…

---

<div class="post-metadata">

**Author:** ![ImreSamu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/imresamu/32/20677_2.png) [@ImreSamu](https://discourse.julialang.org/u/ImreSamu)\
**Post date:** [December 3, 2022, 9:39pm UTC](https://discourse.julialang.org/t/computation-precision-with-float/91180/9 "2022-12-03T21:39:32Z")

</div>

> [@dgagnon](#):
>
> Is the speed of calculation for BigFloat the same as for Float64?

in the `DoubleFloats.jl` ( [GitHub - JuliaMath/DoubleFloats.jl: math with more good bits](https://github.com/JuliaMath/DoubleFloats.jl#more-performant-than-float128-bigfloat) ) there is a comparison: `Double64` vs. `Float128` vs. BigFloat 128 precision

so the `Double64` is faster than the `BigFloat`

> [@dgagnon](#):
>
> I am not so much looking for many decimals. But I’d like exact results when dealing with simple numbers.

Decimals are an important tool in finance math because they allow for more precise and accurate calculations of monetary values.

- [GitHub - JuliaMath/FixedPointDecimals.jl: Julia fixed-point decimals built from integers](https://github.com/JuliaMath/FixedPointDecimals.jl)

```julia
julia> using FixedPointDecimals

julia> p=FixedDecimal{Int,6}(0.8)
FixedDecimal{Int64,6}(0.800000)

julia> 1-p
FixedDecimal{Int64,6}(0.200000)

julia> print(1-p)
0.200000

```

And see the already mentioned _" DecFP.jl or Decimals.jl or FixedPointNumbers.jl."_

You can find other “float” or “decimal” related packages at the [https://juliahub.com/ui/Packages](https://juliahub.com/ui/Packages)
