2
$\begingroup$

I have a table of pairs of numbers, each with 300 decimal places, which are all important. If saved using Export["data.dat", data] and then imported back via Import["data.dat"] - then the 300 decimal places will become about 8 decimal places. What is the easiest way to overcome this? I don't really need the data file to be compatible with anything other than Mathematica.

This question is similar to this one.

$\endgroup$
  • $\begingroup$ If you only need to save data for re-import back to mathematica use .m or .mb format. $\endgroup$ – george2079 Oct 9 '15 at 12:55
  • $\begingroup$ @george2079 What difference does specifying the file extension explicitly as .m or .mb make? Looks like if I use Export with the third argument being "List", then the output file will be basically a text file, be its extension .m or .mb. Or am I missing something here? $\endgroup$ – Alex Oct 9 '15 at 13:21
  • $\begingroup$ correcting myself that should be .mx not .mb $\endgroup$ – george2079 Aug 26 '16 at 15:24
2
$\begingroup$

Elaborating on my comment, the native mathematica package format precisely preserves the definition of symbols:

 lst = {1.3566789543235679098765432344564335, N[Pi, 50]};
 FullForm[lst[[1]]]

1.3566789543235679098765432344564335`34.132477087997515

 Export["test.m", lst];
 newlist = Import["test.m"];
 FullForm[newlist[[1]]]

1.3566789543235679098765432344564335`34.132477087997515

The "List" format chops to the nearest ascii representation and converts back on import,

Export["testfile", lst, "List"];
newlst = Import["testfile", "List"];
FullForm[newlst[[1]]]

1.3566789543235679098765432344564340000000000000000000000001`33.132477087997515

The difference is very subtle and perhaps inconsequential (here actually one bit difference in a 100+ bit binary representation ). I haven't tested it but I expect .m is faster too if you have large files to save. Of course the downside is .m is a unique format that would be difficult for anything else to parse.

Note the third argument to Export overrides whatever extension the file name has. The third argument corresponding to .m is "Package".

Also ".mx" is binary, which will be faster and smaller than ".m", but may not be portable across systems and versions. (Anybody know the "third argument" name for mx? )

$\endgroup$
  • $\begingroup$ Great, thanks a lot. I also noticed the following difference between .m and "List": suppose that data is constructed as data=Table[{RandomReal[],RandomReal[]},{10}]. Then if it is saved via Export into an .m file, and then imported back in via Import, then data[[All,2]] does what it's supposed to do, i.e., extracts all of the second components of data. However, when the data export is done using "List" as the third option of Export, then data[[All,2]] no longer works after data is imported via Import. Why is that? $\endgroup$ – Alex Oct 9 '15 at 15:32
2
$\begingroup$

The Export List format may be what your need. For instance, with

lst = {1.3566789543235679098765432344564335, N[Pi, 50]}

(not 300 digits, but a lot.)

Export["C:/Temp/tst", lst, "List"];
newlst = Import["C:/Temp/tst", "List"];
newlst
(* {1.35667895432356790987654323445643, 
    3.1415926535897932384626433832795028841971693993751} *)
$\endgroup$
  • $\begingroup$ Wonderful. Thank you. $\endgroup$ – Alex Oct 9 '15 at 12:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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