14

With a text file with entries formatted as (output from C++):

f[38.67] = -2.5387862698183892298317350539374412777263289550697e-05;
⋮

Is there a way to read this into Mathematica? It seems to be getting confused with the e, thinking it is just a symbol e.

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
fpghost
  • 2,125
  • 2
  • 21
  • 25
  • 4
    Related: http://mathematica.stackexchange.com/questions/1737/how-do-you-convert-a-string-containing-a-number-in-c-scientific-notation-to-a-ma – cormullion Nov 22 '12 at 13:08
  • 1
    When reading Python output in the past I have once or twice done a global replacement of e with *^. That seems like the easiest approach if you have this file already in place. – Oleksandr R. Nov 22 '12 at 14:59
  • Could be, is there an automated way to get Mathematica to do this? I am going to have lots of files in this format..(I was thinking about changing the output of c++ code to non scientific but it's not too easy with the MPFR data type and c++ wrapper I am using) – fpghost Nov 22 '12 at 15:19
  • 1
    Alternatively, you could Get the file via a pipe, preprocessing it with sed or similar. – Oleksandr R. Nov 22 '12 at 15:30
  • How would I go about that? sorry – fpghost Nov 22 '12 at 15:37
  • 4
    Perhaps something like <<"!sed 's/e/*^/gI' < file.m"? Obviously you should be careful when doing this if you have other occurrences of "e" in the file. – Oleksandr R. Nov 22 '12 at 15:47
  • Try Internal`StringToDouble – masterxilo Sep 03 '17 at 19:56

3 Answers3

11

Similar to Rolf's method:

string = "f[38.67]=-2.5387862698183892298317350539374412777263289550697e-05;";

StringReplace[string, {"e+" :> "*^", "e-" :> "*^-"}];

ToExpression@%;

?? f
Global`f

f[38.67] = -0.000025387862698183892298317350539374412777263289550697

Depending on your data you may want a more specific pattern, e.g.:

StringReplace[string,
  {a : NumberString ~~ "e" ~~ b : NumberString :> a <> "*^" <> b}]
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
5

Import usually automatically converts the e format to powers of. You can use ImportString with the "Table" or "List" type:

ImportString["-2.5387862698183892298317350539374412777263289550697e-05", "Table"]

{{-0.000025387862698183892298317350539374412777263289550697}}

or

ImportString["1.002e-26", "Table"]

{{1.002*10^-26}}

Markus Roellig
  • 7,703
  • 2
  • 29
  • 53
4

For those wishing to transform a full data file with numbers in scientific notation that is output from Fortran (or C, et al.) as "1.6E-19" and "3.0E+08" and such, it is incredibly faster to make changes in the Unix sed command line editor than doing a search and replace in the .m file within Mathematica or any word processor. At the Unix prompt $ one types

$ cat FilewE.m | sed 's/E-/*^-/g' | sed 's/E+/*^+/g' > FilewoE.m

For those new to Unix, the cat command "concatenates" a single file (named FilewE.m in this example), which normally would flow the contents onto the terminal screen. Instead the flow is piped | into sed and, to avoid killing off any Es that appear in words, E- is replaced with *^- globally g. The output of that is piped into sed again to catch the positive exponents and the output of that is directed > to the output file named FilewoE.m in this example.

straton
  • 61
  • 3
  • This should be the answer especially if you are dealing with a lot of files. Easily incorporated in a script. – Cain Jul 28 '21 at 11:07