Re: how to read real numbers in 1.0000e+00 notation
- To: mathgroup at smc.vnet.net
- Subject: [mg4161] Re: [mg4108] how to read real numbers in 1.0000e+00 notation
- From: support (Tom Zeller)
- Date: Sat, 8 Jun 1996 13:24:45 -0400
- Sender: owner-wri-mathgroup at wolfram.com
>I have to read into mathematica ASCII data files containing floating
>point numbers in the notation
>that uses the character "e" to separate mantissa and exponent. Is there a
>mathematica function that converts this into mathematica's proper
>representation, or do I have to use sed or something similar to
>replace e's by something like "*10^" ?
>
>Thank you.
>--
>--
>Fedor Trintchouk (609) 258-4745 (office/voice)
>Princeton University (609) 258-1089 (lab)
>fedort at princeton.edu (609) 258-1139 (fax)
Fedor,
Here's one of many ways to approach this problem.
Here's the test file:
In[18]:=!!enums
6.40624e+03
3.24578e+05
1.07547e+15
1.29759e+27
4.2454e+28
9.20689e+25
7.47152e+04
4.66307e+04
6.45702e+27
7.65851e+11
Read the numbers in as Word, i.e string data.
In[19]:=wordlist = ReadList["enums", Word]
Out[19]={6.40624e+03, 3.24578e+05, 1.07547e+15, 1.29759e+27,
4.2454e+28, 9.20689e+25, 7.47152e+04, 4.66307e+04,
6.45702e+27, 7.65851e+11}
Make up a function that replaces the e notation.
In[20]:=enomore[x_]:= StringReplace[x, "e+" -> " 10^"]
Map that function onto the list.
In[21]:=wordlist = Map[enomore, wordlist]
Out[21]={6.40624 10^03, 3.24578 10^05, 1.07547 10^15,
1.29759 10^27, 4.2454 10^28, 9.20689 10^25,
7.47152 10^04, 4.66307 10^04, 6.45702 10^27,
7.65851 10^11}
Change the strings to numbers using ToExpression.
In[22]:=numlist = ToExpression[wordlist]
Out[22]=
15 27 28
{6406.24, 324578., 1.07547 10 , 1.29759 10 , 4.2454 10 ,
25 27
9.20689 10 , 74715.2, 46630.7, 6.45702 10 ,
11
7.65851 10 }
The same thing can be done in one command, if you use a pure function inside
string replace.
In[23]:= numlist2 = ToExpression[
Map[ StringReplace[#, "e+" -> " 10^"]&, ReadList["enums", Word]
]
]
Out[23]=
15 27 28
{6406.24, 324578., 1.07547 10 , 1.29759 10 , 4.2454 10 ,
25 27 11
9.20689 10 , 74715.2, 46630.7, 6.45702 10 , 7.65851 10 }
In[24]:= numlist2 == numlist
Out[24]= True
Hope this helps.
Tom Zeller
Wolfram Research Technical Support
==== [MESSAGE SEPARATOR] ====