6

I'm puzzled by the output I get from SetAccuracy. According to the documentation, when SetAccuracy is used to increase the accuracy of a number, the number is padded with zeros. But, let's take a look at a couple of examples:

SetAccuracy[1.2, 5]
(* 1.2000 *)
SetAccuracy[1., 5]
(* 1.0000 *)
SetAccuracy[0.2, 5]
(* 0.2000 *)

These examples seem to work properly, so why does it behave differently in this case?

SetAccuracy[0., 5]
(* 0.*10^-5 *)

What should I do to get a zero with four trailing zeros?

Update I'm asking this question, because I need to export data to a txt file and I would like to avoid having 0.*10^-5 sort of numbers.

VLC
  • 9,818
  • 1
  • 31
  • 60
  • 2
    SetAccuracy is not a formatting tool, but intended to set accuracy of numeric value(s). How about something like PaddedForm[0., {4, 4}]? – kirma Oct 29 '12 at 11:50
  • @kirma It might work, but it creates other problems if I try to export the data. – VLC Oct 29 '12 at 12:05
  • What other problems? – Rojo Oct 29 '12 at 12:19
  • 2
    NumberForm[0., {1, 4}] will print 0.0000 – image_doctor Oct 29 '12 at 13:03
  • @Rojo Look at the exported document: Export["test.txt", PaddedForm[RandomReal[], {4, 4}]]. – VLC Oct 29 '12 at 13:22
  • Does Export["test.txt", StandardForm@PaddedForm[RandomReal[], {4, 4}]] work for you ? – image_doctor Oct 29 '12 at 13:36
  • @image_doctor Partially, it inserts an empty space in the exported doc. It is better with StandardForm@NumberForm[0., {4, 4}]. But then again, it is not generic: StandardForm@NumberForm[1200345., {10, 4}]. – VLC Oct 29 '12 at 13:50
  • 2
    @image_doctor Maybe this workaround solves the problem:StandardForm@ NumberForm[1200345., {10, 4}, ExponentFunction -> (Null &)]. I'll check it now extensively. – VLC Oct 29 '12 at 14:02
  • Glad you found a solution. – image_doctor Oct 30 '12 at 00:22

3 Answers3

6

The comments by image_doctor led me to the answer I was looking for:

StandardForm@NumberForm[1.2, {20, 4}, ExponentFunction -> (Null &)]
(* 1.2000 *)
StandardForm@NumberForm[1., {20, 4}, ExponentFunction -> (Null &)]
(* 1.0000 *)
StandardForm@NumberForm[0.2, {20, 4}, ExponentFunction -> (Null &)]
(* 0.2000 *)
StandardForm@NumberForm[0., {20, 4}, ExponentFunction -> (Null &)]
(* 0.0000 *)

The data are then consistent and can be easily exported.

VLC
  • 9,818
  • 1
  • 31
  • 60
3

Might want to have a look at the InputForm of these.

SetAccuracy[0., 5] // InputForm

(*Out[38]//InputForm = 0``5.*)

SetAccuracy[1.2, 5] // InputForm

(*Out[39]//InputForm=1.19999999999999995559107901499373838305`5.079181246047625*)

I doubt either gives the behavior you are after. As was suggested in a comment, maybe NumberForm or PaddedForm will meet your needs.

Dr. belisarius
  • 115,881
  • 13
  • 203
  • 453
Daniel Lichtblau
  • 58,970
  • 2
  • 101
  • 199
2

An example that allows you to accomplish your goal of exporting to a file without needing SetAccuracy is as follows:

Export["test.txt", ToString@NumberForm[#, {5, 4}] & /@ {RandomReal[], 0.0}]]
FilePrint["test.txt"]

Out[40]= "test.txt"

0.6538
0.0000
david
  • 1,290
  • 1
  • 10
  • 15