Message Boards Message Boards


Explain behavior of N function displayed precision?

Posted 9 months ago
4 Replies
3 Total Likes

I noticed an anomaly when using different ways to calculate square roots. For good or bad over the years I have fallen into a habit of calculating square roots as raising a number to .5. Doing this in Mathematica has caused an unexplained behavior.

If I use Sqrt[5] or (5^(1/2) I get the same results and displayed precision If I use (5^.5) I get the same result but less displayed precision.

See the below example . For phi1 and phi 2 I can see 20 decimal places. For phi3 I can see at most 5 decimal places. However phi1-phi3 is zero so I am getting the same result

Why am I only seeing 5 decimal places for phi3?



phi1 = (1 + Sqrt[5])/2;
N[phi1, 20]                     (* <——— Shows 20 decimal places*)

phi2 = (1 + 5^(1/2))/2;
N[phi2, 20]                    (* <——— Shows 20 decimal places*)

phi3 = (1 + 5^(.5))/2;
N[phi3, 20]                    (* <——— Shows only 5 decimal places*)



"11.3.0 for Mac OS X x86 (64-bit) (March 7, 2018)"




4 Replies

It's not a unexplained behavior it is a normal behavior (not a bug).

phi1 = (1 + Sqrt[5])/2; N[phi1, 20] 
phi2 = (1 + 5^(1/2))/2; N[phi2, 20] 
phi3 = (1 + 5^(SetPrecision[0.5, 20]))/2; N[phi3, 20] 

(* 1.6180339887498948482 *)
(* 1.6180339887498948482 *)
(* 1.6180339887498948482 *)


{Precision[(1 + 5^(.5))/2], Precision[(1 + 5^(1/2))/2]}

(* {MachinePrecision, \[Infinity]} *)

This is very well explained in this book

Posted 9 months ago

wow !! ... Thank you .... so it's the default behavior of Machine Precision vs setting a precision. Based on your answer I changed .5 to .50000000000000000000 and got the same result you did with SetPrecission

Are there best practice guidelines on how to introduce precision. Using .5 vs 1/2 while arithmetically correct may not give me the result I expect.

It seems that I should enter the accuracy I want (.50000000000000000000) or enter a calculation (1/2) allowing Mathematica to use its internal precision


To enter any number to a specific precision you can use a shortcut: entering 0.5`30 will give you 0.5 with 30 digits of precision. The prime is the character above the Tab key.

Unless you specifically specify the precision, as soon as you enter a decimal point you are using machine precision. That is why using 1/2 instead of 0.5 will maintain the expression with infinite precision until you either use N[] to truncate it or mix it with some real numbers with decimal points.



Posted 9 months ago

excellent explnation. Thank you. I am starting to understand Mathematica at a more subtle level.

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract