# Machine Precision ... and 0 in Julia

**URL:** <https://discourse.julialang.org/t/machine-precision-and-0-in-julia/42767>\
**Category:** General Usage\
**Tags:** question\
**Created:** [July 9, 2020, 4:04am UTC](https://discourse.julialang.org/t/machine-precision-and-0-in-julia/42767 "2020-07-09T04:04:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![danicaratelli](https://avatars.discourse-cdn.com/v4/letter/d/90db22/32.png) [@danicaratelli](https://discourse.julialang.org/u/danicaratelli)\
**Post date:** [July 9, 2020, 4:04am UTC](https://discourse.julialang.org/t/machine-precision-and-0-in-julia/42767/1 "2020-07-09T04:04:49Z")

</div>

Hello,

I have a question about whether there is a way around the following problem. If I take different linear combinations of the _same_ vector I should get the _same_ vector as a result. However, applying the linear combination adds some almost-0 error which then implies that the two are not the same. Specifically, in the code below v1 and v2 should be exactly the same but they are no _exactly_ the same.

```julia
x = rand(100);
v1 = 0.1*x + (1-0.1)*x;
v2 = 0.8*x + (1-0.8)*x;
println(maximum(abs.(v1 .- v2)))

```

---

<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:** [July 9, 2020, 4:55am UTC](https://discourse.julialang.org/t/machine-precision-and-0-in-julia/42767/2 "2020-07-09T04:55:13Z")

</div>

it’s expected, it’s Float number precision, you should use `\approx`

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [July 9, 2020, 4:59am UTC](https://discourse.julialang.org/t/machine-precision-and-0-in-julia/42767/3 "2020-07-09T04:59:10Z")

</div>

For a nice write-up of this, see [PSA: floating-point arithmetic](https://discourse.julialang.org/t/psa-floating-point-arithmetic/8678)

---

<div class="post-metadata">

**Author:** ![klaff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klaff/32/7637_2.png) [@klaff](https://discourse.julialang.org/u/klaff)\
**Post date:** [July 9, 2020, 5:03am UTC](https://discourse.julialang.org/t/machine-precision-and-0-in-julia/42767/4 "2020-07-09T05:03:40Z")

</div>

0.1, 0.2, 0.8, and 0.9 cannot be expressed exactly in floating point. If you change to

```julia
x = rand(100);
v1 = 0.25*x + (1-0.25)*x;
v2 = 0.5*x + (1-0.5)*x;
println(maximum(abs.(v1 .- v2)))

```

you’ll get what you expect because 0.25 and 0.5 can be expressed exactly.

This will be true in any language using the floating point hardware in your computer.

You can see the repeating form of the mantissa by using `bitstring(0.1)` or `bitstring(0.25)`.
