3

I have a two-part question regarding NumberForm

First question

In my attempt at answering this i stumbled upon a strange behavior by NumberForm when the final result is exported.

Lets say I have a simple Table output as shown below which uses the command NumberForm

t1 = Table[{i, NumberForm[Range[i], {2, 2}]}, {i, 1, 2}]

Now if I export this it works fine

   Export["try.dat", t1];
   FilePrint["try.dat"]
  (* 1  {1.00} *)
  (* 2  {1.00, 2.00}*)

But if i slightly tweak the Table like this

 t2 = Table[{i, NumberForm[Range[i + #], {2, 2}] & /@ Range[2]}, {i, 1, 2}]

Now on export NumberForm remains unevaluated

  Export["try.dat", t2];
  FilePrint["try.dat"]

(* 1    {NumberForm[{1, 2}, {2, 2}], NumberForm[{1, 2, 3}, {2, 2}]} *)
(* 2    {NumberForm[{1, 2, 3}, {2, 2}], NumberForm[{1, 2, 3, 4}, {2, 2}]} *)

So why is this happening?

Second question

Also while searching I found this where the OP asked and answered his own question regarding NumberForm but when I run his/her solution it's not giving the expected answer in my machine (Windows 8 with mma 9). Below is the code which the OP gave.

time = Table[i, {i, 0, 0.4, 0.01}];
time2 = Table[NumberForm[i, 6], {i, 0, 0.4, 0.01}];
op = Table[time[[i]]^2 + time[[i]], {i, 1, Length@time}];
Export["data.dat", Transpose@{time2, op}, "Table"];
FilePrint["data.dat"]; 

Can someone confirm what they get on running the above piece matches with whats given in that thread. I'm getting some numbers which are not formatted.

Hubble07
  • 3,614
  • 13
  • 23
  • I don't think your question has anything to do with Export. if t2 evaluated correctly, the export would have been fine. And t2 does evaluate correctly on my system, which is V10 on OS X. – m_goldberg Sep 20 '14 at 16:23
  • 1
    but isn't t2 giving the expected result. i see no problem in the output of t2 – Hubble07 Sep 20 '14 at 16:29
  • Re your second question, yes, some numbers on the second column are not formatted as in the linked answer (version 9.0.1.0 Windows 8 6-bit). With a small change (Export["data.dat", Transpose@{time2, NumberForm[#, 6] & /@ op}, "Table"]; you get the same result as in the linked answer. – kglr Sep 20 '14 at 21:51

2 Answers2

4

I confirm the issue in Version 9.0.1.0 (Windows 8 64-bit). A workaround is wrapping NumberForm with OutputForm:

t2b = Table[{i, OutputForm[NumberForm[Range[i + #], {2, 2}]] & /@ Range[2]}, {i, 1, 2}]
Export["tryb.dat", t2b];
FilePrint["tryb.dat" ]

enter image description here

kglr
  • 394,356
  • 18
  • 477
  • 896
2

You have an extra pair of list brackets in t2 which you can remove like so:-

t2 = Table[{i, Apply[Sequence,
    NumberForm[Range[i + #], {2, 2}] & /@ Range[2]]}, {i, 1, 2}]

Then it works like t1.

Export["try.dat", t2];
FilePrint["try.dat"]

(* 1    {1.00, 2.00}    {1.00, 2.00, 3.00} *)
(* 2    {1.00, 2.00, 3.00}  {1.00, 2.00, 3.00, 4.00} *)
Chris Degnen
  • 30,927
  • 2
  • 54
  • 108
  • This is fine but whats wrong with an extra bracket from the point of view of Export. I mean why Export fails to write the result given by t2. – Hubble07 Sep 20 '14 at 16:57
  • The export format, which appears to be "Data", seems to expect only a two-dimensional grid of data. So the nested items in the sublists are not being evaluated like the top level items during the Export process. – Chris Degnen Sep 20 '14 at 17:23
  • Chris, if you copy/paste the output of t2 (OP's version) into the second argument of Export it works ok. – kglr Sep 20 '14 at 18:05
  • @kguler - Yes, copy & paste evaluates the NumberForm statements. FullForm[t2] shows NumberForm unevaluated. So Export does not evaluate like copy & paste. – Chris Degnen Sep 20 '14 at 18:48
  • @ChrisDegnen But FullForm[t1] also shows NumberForm unevaluated and yet Export works in that case. – Hubble07 Sep 21 '14 at 08:51
  • @Hubble07 - Export will evaluate NumberForm but in only the top level terms of a two-dimensional grid of data. That is, unless OutputForm is used, which somehow forces evaluation. Quite interesting. – Chris Degnen Sep 21 '14 at 10:15