# Sum of float64 vector gives slightly incorrect answer

**URL:** https://discourse.julialang.org/t/sum-of-float64-vector-gives-slightly-incorrect-answer/8577
**Category:** Performance
**Tags:** question
**Created:** [January 24, 2018, 10:38pm UTC](https://discourse.julialang.org/t/sum-of-float64-vector-gives-slightly-incorrect-answer/8577 "2018-01-24T22:38:46Z")
**Posts on this page:** 1
**Showing post:** 20

<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 25, 2018, 5:58pm UTC](https://discourse.julialang.org/t/sum-of-float64-vector-gives-slightly-incorrect-answer/8577/20 "2018-01-25T17:58:36Z")

</div>

> [@ScottPJones](#):
>
> Does that work for decimal floating-point correctly? I remember a problem I had when I started with Julia, I found out that the femto-lisp parser converts floating point literals to `Float64`, and doesn’t keep the original string.

Mostly yes.

ChangePrecision works by taking float literals (which are indeed stored in the AST as `Float64`) and printing them to a string, then re-parsing this string in the desired numeric type. In almost all cases, the grisu algorithm used to print floating-point values will print something equivalent to the original decimal input string, since it prints the shortest string that parses to the same value.

For example, `0.3` parses to a `Float64` value that is not quite `0.3`, but it _prints_ as `"0.3"`, so ChangePrecision will preserve this exact value when converting to `Dec64`, or for `BigFloat` it will give you `BigFloat("0.3")`. However, it is not 100% reliable — in the unlikely event that you enter the literal `0.099999999999999999`, ChangePrecision will treat it as if you had typed `"0.1"` (`== repr(0.099999999999999999)`).

ChangePrecision is convenient for quick hacks, I think — taking an existing script and quickly experimenting with another precision. But in the long run, you should strive to write libraries of code that work for any precision (indeed, any `Number` type), and if you need absolute guarantees of representing human inputs exactly you should probably use DecFP (or similar) directly.

---

_[View the full topic](https://discourse.julialang.org/t/sum-of-float64-vector-gives-slightly-incorrect-answer/8577)._
