# Why is there a discrepancy in the value of sqrt(2) from Wolfram Alpha and Julia?

**URL:** <https://discourse.julialang.org/t/why-is-there-a-discrepancy-in-the-value-of-sqrt-2-from-wolfram-alpha-and-julia/107461>\
**Category:** New to Julia\
**Tags:** numbers, precision, display\
**Created:** [December 11, 2023, 6:45pm UTC](https://discourse.julialang.org/t/why-is-there-a-discrepancy-in-the-value-of-sqrt-2-from-wolfram-alpha-and-julia/107461 "2023-12-11T18:45:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![chandra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chandra/32/207807_2.png) [@chandra](https://discourse.julialang.org/u/chandra)\
**Post date:** [December 11, 2023, 6:45pm UTC](https://discourse.julialang.org/t/why-is-there-a-discrepancy-in-the-value-of-sqrt-2-from-wolfram-alpha-and-julia/107461/1 "2023-12-11T18:45:16Z")

</div>

On the [Wolfram Alpha website](https://www.wolframalpha.com/input?i=sqrt%282%29), I get this approximation for `sqrt(2)`

`1.4142135623730950488016887242096980785696718753769480731766797379...`

With the Julia REPL, I get

```julia
sqrt(2)
1.4142135623730951

```

and

```julia
convert(BigFloat, sqrt(2))
1.4142135623730951454746218587388284504413604736328125

```

There is a difference after the fifteenth decimal place.

Is this due to differences in how sqrt(2) is computed, or due to display precision, or something else entirely?

I would have expected to be able to check one reputable source against another for a never-ending decimal expansion.

Thanks.

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [December 11, 2023, 6:50pm UTC](https://discourse.julialang.org/t/why-is-there-a-discrepancy-in-the-value-of-sqrt-2-from-wolfram-alpha-and-julia/107461/2 "2023-12-11T18:50:12Z")

</div>

```julia
julia> sqrt(big(2))
1.414213562373095048801688724209698078569671875376948073176679737990732478462102

```

When you convert matters.

---

<div class="post-metadata">

**Author:** ![chandra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chandra/32/207807_2.png) [@chandra](https://discourse.julialang.org/u/chandra)\
**Post date:** [December 11, 2023, 7:00pm UTC](https://discourse.julialang.org/t/why-is-there-a-discrepancy-in-the-value-of-sqrt-2-from-wolfram-alpha-and-julia/107461/3 "2023-12-11T19:00:42Z")

</div>

Wow! I am reassured. Thanks.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [December 11, 2023, 7:24pm UTC](https://discourse.julialang.org/t/why-is-there-a-discrepancy-in-the-value-of-sqrt-2-from-wolfram-alpha-and-julia/107461/4 "2023-12-11T19:24:58Z")

</div>

Specifically, `1.4142135623730951` is the _shortest decimal representation_ that rounds to the _closest possible value_ to \sqrt{2} using standard 64-bit floating point.

Converting that 64-bit floating point number to a `BigFloat` doesn’t actually change its numerical value, but it does change how many digits are printed in the decimal representation you see. It’s the same as if you asked for a bunch of decimal digits from `@printf`:

```julia-repl
julia> using Printf

julia> @printf("%.52f", sqrt(2))
1.4142135623730951454746218587388284504413604736328125

```

64-bit floating point only has about ~15 decimal digits worth of precision, which is why the shortest decimal needed to round to that is the length it is. You can see that this is the closest possible value by comparing it (and its nearest representable neighbors) to that higher-precision reference:

```julia-repl
julia> nextfloat.(sqrt(2), -1:1)
3-element Vector{Float64}:
 1.414213562373095
 1.4142135623730951
 1.4142135623730954

julia> Float64.(nextfloat.(sqrt(2), -1:1) .- sqrt(big(2)))
3-element Vector{Float64}:
 -1.2537167179050217e-16
  9.667293313452913e-17
  3.187175380595604e-16

```

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [December 11, 2023, 9:58pm UTC](https://discourse.julialang.org/t/why-is-there-a-discrepancy-in-the-value-of-sqrt-2-from-wolfram-alpha-and-julia/107461/5 "2023-12-11T21:58:34Z")

</div>

> [@chandra](#):
>
> There is a difference after the fifteenth decimal place.

You can _expect_ that the last digit is not correct and it’s **not** Julia-specific (max. 15-17 is correct for Float64, and fewer for Float32, or rather, exactly 53 bits _after_ rounding, but any previous digit in binary or decimal can look off), even that the last few, and it’s not a bug per se. You can expect arbitrary many digits to be wrong for _some_ numbers.

> **[Six nines in pi](https://en.wikipedia.org/wiki/Six_nines_in_pi)**
>
> A sequence of six consecutive nines occurs in the decimal representation of the number pi (π), starting at the 762nd decimal place. It has become famous because of the mathematical coincidence, and because of the idea that one could memorize the digits of π up to that point, and then suggest that π is rational. The earliest known mention of this idea occurs in Douglas Hofstadter's 1985 book Metamagical Themas, where Hofstadter states
> I myself once learned 380 digits of π, when I was a crazy hi...

> I myself once learned 380 digits of π, when I was a crazy high-school kid. My never-attained ambition was to reach the spot, 762 digits out in the decimal expansion, where it goes “999999”, so that I could recite it out loud, come to those six 9’s, and then impishly say, “and so on!”
> 
> This sequence of six nines is sometimes called the “ **Feynman point** ”, after physicist [Richard Feynman](https://en.wikipedia.org/wiki/Richard_Feynman)

From correct value of pi at Wikipedia:  
… 4 **999999** 837

```julia
julia> setprecision(640*ceil(Int, log(10)/log(2))); string(big(pi))[762:772] # some miscalculation in finding the cut-off point-in binary for the decimal, but showing correct
"34999999837"

```

You can see that if you end at 98 (correct) and then cut off that (8) decimal and round the previous up from that decimal, then you would get … 35000000 …

Nothing fundamentally different applies in binary. You could get arbitrary long series of 1s repeating in pi or sqrt(2) or other irrationals.\* Or in decimal, arbitrary long series of 9s as shown.

I’m trying to hit that right spot, but BigFloat is _binary_ big-floating point:

```julia
julia> setprecision(639*ceil(Int, log(10)/log(2))); string(big(pi))[762:772]
"34999999833"

```

> `*` π is [conjectured](https://en.wikipedia.org/wiki/Conjecture), but not known, to be a [normal number](https://en.wikipedia.org/wiki/Normal_number).

For _any_ normal number you will get arbitrary long sequences, and even all know literature ever written encoded in pi. But only if pi is proved normal. I don’t know if it’s know for the square root of 2, for all I know it’s proven to NOT be normal. Still you could get very long series.

We do have Dec64, and Dec128, but no arbitrary-precision decimal floating point, that I know of, but it would neither help…

---

<div class="post-metadata">

**Author:** ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)\
**Post date:** [December 11, 2023, 11:55pm UTC](https://discourse.julialang.org/t/why-is-there-a-discrepancy-in-the-value-of-sqrt-2-from-wolfram-alpha-and-julia/107461/6 "2023-12-11T23:55:14Z")

</div>

![Screenshot 2023-12-12 at 10.53.35 am](https://global.discourse-cdn.com/julialang/original/3X/f/d/fd869dd2fcb0d64aa03071cac96cb1f46ef5685d.png)  
 ![Screenshot 2023-12-12 at 10.54.54 am](https://global.discourse-cdn.com/julialang/original/3X/3/6/36526d7e2974413404066b343a47e85207a5b425.png)

So don’t go memorizing more than 64 decimal digits of Pi
