0
$\begingroup$

I need to export full form of a number i.e. the actual form with which Mathematica works. For example, full form of a number is

num=1.1694845109427001990727289009947411075373409487402352705713`40.

Only way I found to export this is to write the following

InputForm[num];
ToString[%]
StringDrop[%, -4]
SetPrecision[%, StringLength[%]]

Now, the problem is that I want to export the whole table where each entry of the table has several inputs e.g.

tab={{1.190235930262107762653724766752294083855141423958071302364`40., 1.1702261571473762099324066606101339526955374437267904124129`40., 
  0.110264970245810080432049941184470965994019161461656340948`39.473393763182095, 4.7241320323359913274841788887731931621875469331755267802594`40., 
  46.0798885483327363686455352243385184903599023670671495822868`40., 26.2513307092631370285885892895307998289467912235415183987001`39.449222376362826, 
  8.6300009861437150005578527045608343782846751293724603199829`40., 0.3287452770193665821301068252318425815750667629216152942724`39.34155999202286, 0.2`40.}, 
 {1.1694845109427001990727289009947411075373409487402352705713`40., 1.073614784384483777398377895103168199725419102443138752294`40., 
  0.1082754664224803165523824236845656255153277170011990855383`39.48226217386681, 4.5882158842465506017870156331053374906509175343481833706127`40., 
  45.2295919617716654010906521282642146248623730589718327732484`40., 26.7820355268597979269470606841468266499226811521239027848748`39.444225614883074, 
  8.8840442521351719691506133437270445215927621701056185202081`40., 0.3317165434727816951197136242723464016871926565295576138788`39.33765542157627, 0.2`40.}}

and some of these entries are strings with variable lengths.

So, is there a way to circumvent this problem or a smarter way to extract full form of a number to a CSV table, that can later be imported in Mathematica and used?

$\endgroup$
6
  • $\begingroup$ It does not make much sense to give more than 40 digits and then truncate it to Precision==40 (your backtick 40). If you export a number with Precision==40, 40 digits will be exported, what makes sense. $\endgroup$ Feb 11, 2021 at 17:19
  • $\begingroup$ Am I understanding correctly that you are exporting from Mathematica to CSV and then re-import into Mathematica? If yes, then there are much simpler ways. $\endgroup$
    – Roman
    Feb 11, 2021 at 17:32
  • $\begingroup$ @Roman: yes, but I need to export full form of the number. I tried to export in the form e.g. num100=SetPrecision[num,100] but this doesn't work satisfactorily - when I use num100 as an input to the numerical code I don't get the same output as with num. Code is some stiff differential integrator so I need to keep track of a lot of digits $\endgroup$ Feb 11, 2021 at 17:56
  • $\begingroup$ Some more questions: Are you aware that you can export binary representations (full forms) of everything? Why do you need to export anyway instead of just caching internally? The question looks very much like an XY problem. $\endgroup$
    – Roman
    Feb 11, 2021 at 18:02
  • $\begingroup$ It is irrelevant why I need to export the data. I need to store them for some reason. I think that question is formulated in a precise way - Is there a way to export full form of the number that can later be imported in that same form i.e. from 1.1694845109427001990727289009947411075373409487402352705713`40. I want 1.1694845109427001990727289009947411075373409487402352705713 and the method must work for other number that can have different precisions as indicated in the tab example. $\endgroup$ Feb 11, 2021 at 18:11

2 Answers 2

3
$\begingroup$

Export uses CForm under the hood for formatting of real numbers:

TracePrint[ExportString[.2`40, "CSV"], _ToString, TraceAction->Print@*FullForm]

HoldForm[ToString[CForm[0.2`40.],InputForm]]

"0.2"

So, one idea is to temporarily modify the CForm formatting of reals:

Internal`InheritedBlock[{CForm},
    Unprotect[CForm];
    CForm /: ToString[CForm[r_Real], InputForm] := ToString[r, InputForm];
    ExportString[tab, "CSV"]
]

(*
"1.190235930262107762653724766752294083855141423958071302364`40.,1.\
1702261571473762099324066606101339526955374437267904124129`40.,0.\
110264970245810080432049941184470965994019161461656340948`39.473393763182095,\
4.7241320323359913274841788887731931621875469331755267802594`40.,46.\
0798885483327363686455352243385184903599023670671495822868`40.,26.\
2513307092631370285885892895307998289467912235415183987001`39.449222376362826,\
8.6300009861437150005578527045608343782846751293724603199829`40.,0.\
3287452770193665821301068252318425815750667629216152942724`39.34155999202286,\
0.2`40.
1.1694845109427001990727289009947411075373409487402352705713`40.,1.\
073614784384483777398377895103168199725419102443138752294`40.,0.\
1082754664224803165523824236845656255153277170011990855383`39.48226217386681,\
4.5882158842465506017870156331053374906509175343481833706127`40.,45.\
2295919617716654010906521282642146248623730589718327732484`40.,26.\
7820355268597979269470606841468266499226811521239027848748`39.444225614883074,\
8.8840442521351719691506133437270445215927621701056185202081`40.,0.\
3317165434727816951197136242723464016871926565295576138788`39.33765542157627,\
0.2`40."
*)
$\endgroup$
1
  • $\begingroup$ this works, thanks! $\endgroup$ Feb 11, 2021 at 19:21
0
$\begingroup$

Why CSV? Why not mx?

Export["tab.mx", tab]
tabImport = Import["tab.mx"];

InputForm@tab == InputForm@tabImport
(* True *)
$\endgroup$

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.