6
$\begingroup$

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.

$\endgroup$
1

4 Answers 4

5
$\begingroup$

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]
$\endgroup$
1
  • $\begingroup$ This is quite comprehensive. And I especially liked the idea of adding this to $PrePrint $\endgroup$
    – goweon
    Aug 5, 2018 at 4:41
4
$\begingroup$
NumberForm[N@Table[4^(i/4), {i, 1, 4}], 10]
$\endgroup$
3
$\begingroup$

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.}

$\endgroup$
2
$\begingroup$

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}]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.