# Numerical accuracy/machine accuracy and roundness in Julia

**URL:** <https://discourse.julialang.org/t/numerical-accuracy-machine-accuracy-and-roundness-in-julia/124541>\
**Category:** General Usage\
**Tags:** question, rounding\
**Created:** [January 8, 2025, 3:17pm UTC](https://discourse.julialang.org/t/numerical-accuracy-machine-accuracy-and-roundness-in-julia/124541 "2025-01-08T15:17:19Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ofenblech](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ofenblech/32/214587_2.png) [@Ofenblech](https://discourse.julialang.org/u/Ofenblech)\
**Post date:** [January 8, 2025, 3:17pm UTC](https://discourse.julialang.org/t/numerical-accuracy-machine-accuracy-and-roundness-in-julia/124541/1 "2025-01-08T15:17:19Z")

</div>

Dear community,  
I am new to the forum and I’m looking for a good option for how I can round Julia’s machine accuracy for polynomials or simplify the numbers in the system. I am concerned with the following problems:

I use the package [https://github.com/JuliaAlgebra/DynamicPolynomials.jl](https://github.com/JuliaAlgebra/DynamicPolynomials.jl) and would like to save, for example: DynamicPolynomials.Polynomial[1.0000000000000002 + 1.00000000000000004x₂ + 1.1102230246251563e-16x₁ - 2.0000000000000004x₁x₂ - 2.0000000000000004x₁²] as

‘1.0 + 1.0x\_2 + 0.0x\_1 - 2.0x\_1x\_2 - 2.0x\_1^2.’

Do you have any idea for me how to implement this well?  
Thank you very much for your effort!

---

<div class="post-metadata">

**Author:** ![Ofenblech](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ofenblech/32/214587_2.png) [@Ofenblech](https://discourse.julialang.org/u/Ofenblech)\
**Post date:** [January 8, 2025, 3:24pm UTC](https://discourse.julialang.org/t/numerical-accuracy-machine-accuracy-and-roundness-in-julia/124541/2 "2025-01-08T15:24:01Z")

</div>

Maybe a little clearer, how can I round  
1.0000000000000002 to 1.0 or  
1.1102230246251563e-16 to 0.0

thanks

---

<div class="post-metadata">

**Author:** ![eldee](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@eldee](https://discourse.julialang.org/u/eldee)\
**Post date:** [January 8, 2025, 3:35pm UTC](https://discourse.julialang.org/t/numerical-accuracy-machine-accuracy-and-roundness-in-julia/124541/3 "2025-01-08T15:35:09Z")

</div>

Hi, and welcome to the Julia community!

I’m not saying this is necessarily a ‘good’ solution as there is no clear universal meaning of closeness in this setting (see e.g. discussions about `isapprox`), but you could use `round(..., digits=...)` or `round(..., sigdigits=...)`:

```julia-repl
julia> round(1.0000000000000002, digits=5)
1.0

julia> round(1.1102230246251563e-16, digits=5)
0.0

```

---

<div class="post-metadata">

**Author:** ![mike.ingold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike.ingold/32/203749_2.png) [@mike.ingold](https://discourse.julialang.org/u/mike.ingold)\
**Post date:** [January 8, 2025, 3:37pm UTC](https://discourse.julialang.org/t/numerical-accuracy-machine-accuracy-and-roundness-in-julia/124541/4 "2025-01-08T15:37:12Z")

</div>

Two questions:

- Is your intent that all of the coefficients be integers?
- Is your intent to change the coefficients being used for calculations, or simply to print them without the additional digits?

---

<div class="post-metadata">

**Author:** ![Ofenblech](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ofenblech/32/214587_2.png) [@Ofenblech](https://discourse.julialang.org/u/Ofenblech)\
**Post date:** [January 8, 2025, 3:39pm UTC](https://discourse.julialang.org/t/numerical-accuracy-machine-accuracy-and-roundness-in-julia/124541/5 "2025-01-08T15:39:50Z")

</div>

As an intermediate step, I would like to simplify this polynomial and then continue my calculations with it. Because this not only increases calculation speed but also simplifies the equations to be calculated.

---

<div class="post-metadata">

**Author:** ![mike.ingold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike.ingold/32/203749_2.png) [@mike.ingold](https://discourse.julialang.org/u/mike.ingold)\
**Post date:** [January 8, 2025, 3:58pm UTC](https://discourse.julialang.org/t/numerical-accuracy-machine-accuracy-and-roundness-in-julia/124541/6 "2025-01-08T15:58:13Z")

</div>

The simple command `round(a)` will round a floating-point value `a` to the nearest integer-valued floating-point number, e.g. `round(5.000000000001) == 5.0` but also `round(0.75) == 1.0`. If that’s desirable, you may also benefit from an explicit conversion to integer data types, e.g. `Int(round(a))`

The source for that package is a little tough to follow, but it sounds like maybe you’re looking to perform something like

```julia
using DynamicPolynominals
poly = Polynomial(...)
rounded_poly = Polynomial(Int.(round.(poly.a)), poly.x)

```

where the dots before parenthesis are used for [broadcasting](https://docs.julialang.org/en/v1/manual/arrays/#Broadcasting).

---

<div class="post-metadata">

**Author:** ![eldee](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@eldee](https://discourse.julialang.org/u/eldee)\
**Post date:** [January 8, 2025, 4:07pm UTC](https://discourse.julialang.org/t/numerical-accuracy-machine-accuracy-and-roundness-in-julia/124541/7 "2025-01-08T16:07:30Z")

</div>

> [@Ofenblech](#):
>
> Because this not only increases calculation speed but also simplifies the equations to be calculated.

Rounding (without changing types) should not make a difference in this regard, i.e. the computer is equally happy to perform calculations with `5.` as it is with `5.1234321`. So if this is your main motivation, there is no need to worry about encountering ‘long’ numbers 🙂 .

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 8, 2025, 4:50pm UTC](https://discourse.julialang.org/t/numerical-accuracy-machine-accuracy-and-roundness-in-julia/124541/8 "2025-01-08T16:50:56Z")

</div>

> [@Ofenblech](#):
>
> would like to save

If this is for output purposes, the best thing is _not_ to use `round` (which rounds the internal binary representation), but rather to use `@printf` (which rounds the decimal-output representation) and related functions from the [`Printf` standard library](https://docs.julialang.org/en/v1/stdlib/Printf/).

For computational purposes, however, rounding intermediate steps is probably actively a bad idea (not only is it not faster, but it can cause catastrophic loss of accuracy if you are not careful). If for some reason you need to ensure exact integer or rational answers, you need to use exact computations from the beginning (and it will probably be slower).

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [January 8, 2025, 6:18pm UTC](https://discourse.julialang.org/t/numerical-accuracy-machine-accuracy-and-roundness-in-julia/124541/9 "2025-01-08T18:18:33Z")

</div>

> [@stevengj](#):
>
> If for some reason you need to ensure exact integer or rational answers, you need to use exact computations from the beginning (and it will probably be slower).

Such as, for example, `Rational{BigInt}`.

---

<div class="post-metadata">

**Author:** ![Ofenblech](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ofenblech/32/214587_2.png) [@Ofenblech](https://discourse.julialang.org/u/Ofenblech)\
**Post date:** [January 9, 2025, 6:50am UTC](https://discourse.julialang.org/t/numerical-accuracy-machine-accuracy-and-roundness-in-julia/124541/10 "2025-01-09T06:50:31Z")

</div>

That’s a good option. Unfortunately, there are not only integers. The goal is to calculate the basis functions for the finite element analysis. There are also 0.25 or something else. For example:

```julia
0.25000000001100e10 -> 0.25

```

`round()` is probably not an elegant solution, but I will test whether it serves its purpose.

---

<div class="post-metadata">

**Author:** ![Ofenblech](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ofenblech/32/214587_2.png) [@Ofenblech](https://discourse.julialang.org/u/Ofenblech)\
**Post date:** [January 9, 2025, 7:45am UTC](https://discourse.julialang.org/t/numerical-accuracy-machine-accuracy-and-roundness-in-julia/124541/11 "2025-01-09T07:45:27Z")

</div>

Is there a possibility that we can connect `round(..., digits=...)` with this one?

```julia
using DynamicPolynominals
poly = Polynomial(...)
rounded_poly = Polynomial(Int.(round.(poly.a)), poly.x)

```

So that we are not only able to do integer rounding but also any other possible?

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [January 9, 2025, 8:05am UTC](https://discourse.julialang.org/t/numerical-accuracy-machine-accuracy-and-roundness-in-julia/124541/12 "2025-01-09T08:05:54Z")

</div>

Just use `Rational` instead of `AbstractFloat`, then convert it to `Float64` or whatever when that’s OK to do.

---

<div class="post-metadata">

**Author:** ![eldee](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@eldee](https://discourse.julialang.org/u/eldee)\
**Post date:** [January 9, 2025, 10:26am UTC](https://discourse.julialang.org/t/numerical-accuracy-machine-accuracy-and-roundness-in-julia/124541/13 "2025-01-09T10:26:50Z")

</div>

You mean like

```julia
rounded_poly = Polynomial(round.(poly.a, digits=2), poly.x)

```

?
