[?] Change the default number of digits to display in the output?

I feel that the posting that indicates you’re not “100% satisfied” still mixes up a couple of things. As Daniel pointed out, there are two types of approximate real numbers in Mathematica, machine reals and arbitrary-precision numbers, a.k.a. bignums. Each type has different display rules. In the factory defaults, machine reals are displayed in rounded format (rounded to six digits, with trailing zeros truncated). Arbitrary-precision numbers are displayed with as many digits as their precision indicates, and the display includes trailing zeros. In neither case does the displayed digits completely represent the internal value, except in cases where the representation and the value happen to coincide. Also, one might take issue with the assertion that machine precision is “16 decimal digits,” depending on the emphasis on decimal digits. The number 0.1 is not represented exactly in binary floating-point, but has to be rounded (see below). Thus even one decimal digit cannot be represented (exactly). The principal issue, as it seem to me, of the discrepancy between display and internal value is inherent to displaying in decimal and value stored in binary. The only real solutions are getting a decimal computer or printing out in a binary format such as hex. (Excel tries to get around these kind of issues through “cosmetic” rounding, which causes other problems; see Kahan 2006, sect. 2.)

As a user I’ve found the number representation in Mathematica notebooks convenient, once I learned how to understand the output. Hand-held calculators do rounding (at least they used to), so it didn’t seem that unusual to me. Six digits meant I could read a medium-long list of numbers. The bignums are apparent by their digits, unless their precision is between one and six.

The documentation for PrintPrecision

PrintPrecision
is an option for selections that specifies the maximum number of digits used for displaying a machine-precision number.

The “Details” section hints at another workaround:

  • The setting for PrintPrecision is only used when NumberMarks -> False.

One can set these options for the current session or once for all (which writes the setting into your init.m file); or one can use Style[1/3., PrintPrecision -> 12] (or NumberMarks -> True) to override the defaults for a single output.

SetOptions[$FrontEndSession, NumberMarks -> True] (* for current session only *)
SetOptions[$FrontEnd, NumberMarks -> True]        (* for current & future sessions *)

SetOptions[$FrontEndSession, PrintPrecision -> 12]

But note that this won’t relieve one of the impression that computers make arithmetic mistakes. The following will make sense only if you are familiar with the binary64 floating-point standard of IEEE-754:

SetOptions[$FrontEndSession, NumberMarks -> True];
0.1 + 0.2
(*   Out[]=0.30000000000000004`  *)

0.3
(*  Out[]= 0.3`  *)

The discrepancy is due to the way binary floating-point numbers are supposed to work according to the IEEE standard.

There is a decimal IEEE-754 standard that avoids the problem with binary. There are decimal machines (IBM POWER6 and System Z) and decimal math libraries, such as Intel® Decimal Floating-Point Math Library . Unfortunately, commonly available machines are binary.

2 Likes

Doug,

I think the problem is that the underlying assumption of your previous posts is that

N[1.000001, 1000]

means “give me 1.000001 to 1000 digits of precision”. That is not the meaning of “N[]” according to the documentation nor the design of MMA. The N function is for converting exact numbers to floating point numbers with a certain precision. If the number already has a lower precision (such as a machine float) then N does nothing except return the number unchanged. Once the float is returned, it is subject to the same printing rules I discussed in my earlier post.

Another way to look at it is that the printing format is a user preference to help maintain screen clutter. You set your preferences to only display 6 digits and then round numbers off for display. From my perspective I do not think it is fair to set the preference to 6 digits and then use the fact that you do not get 10 digits of display as evidence of a bug.

I hope this helps.

Regards,

Neil

2 Likes

Unless I say otherwise, 1.000001 is an exact number. I am well aware that by convention Mathematica doesn’t treat it as that, so I don’t want to get into that discussion as well. But I just find it strange that for instance:

In[1]:= N[1.00000000000000010, 1000]
Out[1]= 1.0000000000000001

with the remaining 983 0’s on the end just assumed. And yet it doesn’t assume there are any zeros after the number when I write it.

I just can’t understand why nobody seems to see what is staring me in the face. Let’s look at the common example of Pi:

In[2]:= N[Pi, 15]
Out[2]= 3.14159265358979

In[3]:= N[Pi, 16]
Out[3]= 3.141592653589793

In[4]:= N[Pi, 17]
Out[4]= 3.1415926535897932

In[5]:= N[Pi, 18]
Out[5]= 3.14159265358979324

In[6]:= N[Pi, 19]
Out[6]= 3.141592653589793238

In[7]:= N[Pi, 20]
Out[7]= 3.1415926535897932385

Beautiful. Exactly what I would want it to do. Exactly what I would expect it to do. Exactly what I want to know. Here is something else I would like it to do:

N[1.0000000000000001, 15]
1.00000000000000

N[1.0000000000000001, 16]
1.000000000000000

N[1.0000000000000001, 17]
1.0000000000000001

N[1.0000000000000001, 18]
1.00000000000000010

N[1.0000000000000001, 19]
1.000000000000000100

N[1.0000000000000001, 20]
1.0000000000000001000

But what does it actually do?

In[8]:= N[1.0000000000000001, 15]
Out[8]= 1.

In[9]:= N[1.0000000000000001, 16]
Out[9]= 1.

In[10]:= N[1.0000000000000001, 17]
Out[10]= 1.

In[11]:= N[1.0000000000000001, 18]
Out[11]= 1.

In[12]:= N[1.0000000000000001, 19]
Out[12]= 1.

In[13]:= N[1.0000000000000001, 20]
Out[13]= 1.

In[14]:= N[1.0000000000000001, 1000]
Out[14]= 1.

If that’s not the definition of what it does then change the definition.

While I’m at it, what’s wrong with this picture?

In[15]:= SetPrecision[10000.04, 7]
Out[15]= 10000.04

In[16]:= SetPrecision[10000.04, MachinePrecision]
Out[16]= 10000.

In[17]:= SetPrecision[10000.04, 17]
Out[17]= 10000.040000000001

You are setting the precision progressively higher and get different results at each stage. I understand the extra digits on the last one, but that’s not what I’m talking about. I’m talking about the fact that it prints any decimals at all. Ok, I’ve got an explanation of why this happens, but I still don’t agree that it’s right.

Even though it has been acknowledge that there are stylistic choices here, most people seem to be justifying Mathematica’s behavior by implying it’s inevitable. Well I know it’s not because I’ve seen it done differently, and better, in other software. I just think there’s room for improvement here.

Doug, it’s not quite clear to me what your aim is with these posts.

Do you have a practical problem that you need help solving? If yes, can you clarify what you are trying to accomplish that you need help with?

Or are you trying to argue that fundamental behaviours of the Wolfram Language should be changed? When people do this, they often have only a few very narrow use cases in mind, but the Mathematica must be usable for many different tasks, and they do not immediately see that the proposed changes would cause countless inconveniences (or worse), not to mention break most existing programs.

Unless I say otherwise, 1.000001 is an exact number.

Well, then you’re not speaking the Wolfram Language, but another language. The correct way to say that is 1000001/1000000. But you already knew that, so what is your point?

But what does it actually do?

Daniel explained what it does in detail, including that MachinePrecision is treated as the lowest. Do you have any questions regarding that explanation?

If that’s not the definition of what it does then change the definition.

You seem to want SetPrecision, which you are already aware of. Why don’t you just use it?


It seems to me that you may not be realizing what the use of machine precision is, or why it behaves so differently from arbitrary precision arithmetic in Mathematica. Its purpose is performance. “Machine precision” is not about a certain precision but about using the same floating point format that is natively supported by the CPU. It also comes with tradeoffs such as the lack of precision tracking—this is why it necessarily behaves differently.

You are setting the precision progressively higher

This is not the case. As explained in other posts, MachinePrecision is not just a precision of 16 (or rather $MachinePrecision) digits. It is qualitatively different.


Well I know it’s not because I’ve seen it done differently, and better, in other software.

I do not see a proposal for a better system for arbitrary precision arithmetic in your posts, nor a serious comparison with an existing alternative and an analysis of pros and cons.

But let us come back to something concrete and practical. What is it that you are trying to accomplish with Mathematica and cannot? What specific task can be done in another system (which?) that cannot be done in Mathematica reasonably conveniently?

Your original complaint was that machine precision numbers are only displayed with 6 digits of precision by default. Many easy solutions have been offered. Do you have any other practical problems?

4 Likes

Try

   N[Rationalize[1.0000000000000001`20, 0], 1000]`

Out[144]=
1.00000000000000010000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000

The questions of this thread has been answered more than sufficiently. Please keep discussion contained within the bounds of the original questions and Wolfram technologies. Please make sure you know the rules: https://wolfr.am/READ-1ST

1 Like