6

For example, this is what I want

In[1]:= N[Table[4^(i/4), {i, 1, 4}]]
Out[1]= {1.41421, 2., 2.82843, 4.}

But if I want a few more digits only from the irrational values, I get this

In[2]:= N[Table[4^(i/4), {i, 1, 4}], 10]
Out[2]= {1.414213562, 2.000000000, 2.828427125, 4.000000000}

When I want

Out[2]= {1.414213562, 2., 2.828427125, 4.}

In the first case, it detects that 4^(1/2) is exactly 2, and intelligently suppresses the trailing zeros, giving 2., but in the second case it doesn't. I'd like to be able to request more digits when necessary, but still suppress trailing zeros.

Michael E2
  • 235,386
  • 17
  • 334
  • 747
goweon
  • 163
  • 3
  • Related/duplicate: https://mathematica.stackexchange.com/questions/3736/annoying-display-truncation-of-numerical-results – Michael E2 Aug 04 '18 at 13:45

4 Answers4

5

Other answers have provided solutions but they have not directly addressed why this happens. The use of N on exact values with a numeric second parameter (e.g. 10) produces arbitrary-precision output, and these values are by default displayed in full without truncation.

Compare:

5.000000   (* machine precision Real *)
5`7        (* arbitrary precision *)
5.

5.000000

The methods in Alan's and Michael's answers get around this by converting to machine precision and then adjusting output. However if you want to print more than $MachinePrecision digits these will not work. Carl's answer comes closer but it also has a problem in that more digits than requested may be printed.

If you specifically want to suppress trailing zeros then you could process to affect that.

For whole numbers simply:

rep = r_Real /; r == Round[r] :> N @ Round[r];

N[Table[4^(i/4), {i, 1, 4}], 25] /. rep
{
 1.414213562373095048801689,
 2.,
 2.828427124746190097603377,
 4.
}

If you need to preserve the arbitrary precision nature of the output when used as input use Interpretation:

rep2 = r_Real /; r == Round[r] :> Interpretation[N @ Round[r], r];

If you want to trim trailing zeros that are not immediately following the decimal point you might turn to string processing:

rep3 = r_Real :> 
   Interpretation[
    RawBoxes @ StringReplace[ToString[r], 
      Shortest[x__] ~~ "0" .. ~~ EndOfString :> x], r];

N[Table[4^(i/4), {i, 1, 4}]/10, 25] /. rep3 // Column
0.1414213562373095048801689
0.2
0.2828427124746190097603377
0.4

If you want to do this globally for a session consider $PrePrint, e.g.

$PrePrint = # /. rep3 &;

Then you can simply write:

N[Table[4^(i/4), {i, 1, 4}]/10, 10]
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
4
NumberForm[N@Table[4^(i/4), {i, 1, 4}], 10]
Alan
  • 13,686
  • 19
  • 38
3

You can create a form that displays numbers the way you want:

MakeBoxes[SuppressedZeroForm[a_], StandardForm] ^:= MakeBoxes[
    Defer[InputForm[a, NumberMarks -> False]]
]

For example:

N[Table[4^(i/4), {i, 1, 4}], 10] //SuppressedZeroForm

{1.4142135624, 2., 2.8284271247, 4.}

Carl Woll
  • 130,679
  • 6
  • 243
  • 355
2

From this answer to Annoying display truncation of numerical results:

Style[N@Table[4^(i/4), {i, 1, 4}], PrintPrecision -> 10]

Mathematica graphics

And adapting the accepted answer, we essentially get Alan's method:

NumberForm[#, 10] &@ N@ Table[4^(i/4), {i, 1, 4}]
Michael E2
  • 235,386
  • 17
  • 334
  • 747