MathGroup Archive 2003

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Severe ToExpression Fault?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg42913] Re: Severe ToExpression Fault?
  • From: "Eckhard Hennig" <aidev at n-o-s-p-a-m.kaninkolo.de>
  • Date: Sat, 2 Aug 2003 04:12:39 -0400 (EDT)
  • References: <bgcujv$9pj$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

"Nils Theune" <nils at theune.com> schrieb im Newsbeitrag
news:bgcujv$9pj$1 at smc.vnet.net...
> Hi folks,
>
> running Mathematica 4.0.1 I am stuck with the following problem:
>
> Input:
> Print["-1,97E-04"];
> Print[StringReplace["-1,97E-04", {"," -> "."}]];
> Print[ToExpression[StringReplace["-1,97E-04", {"," -> "."}]]];
>
> Output:
> -1,97E-04
> -1.97E-04
> -9.35502
>
> Can somebody explain this to me? Why does the ToExpression Function
> not make the decimal -1.97E-04 out of the String "-1.97E-04". What in
> the hell is going wrong (-9.35502->"Where does this number come
> from?") and what can be done to circumvent the ToExpression function.


Hi Nils,

ToExpression is actually doing exactly what it is meant to do: convert a
Mathematica expression in string form to internal representation. Note that
Mathematica interprets the expression

  -1.97E-04

as

  -1.97*E - 4

where E = 2.71828... denotes the base of the natural logarithm.

If you want to read numbers in engineering notation from a string, use
StringToStream and Read instead of ToExpression:

In[1]:= str = StringToStream["-1.97E-04"];
In[2]:= Read[str, Real]
Out[2]= -0.000197
In[3]:= Close[str];


Best regards,

  Eckhard

--
Dr.-Ing. Eckhard Hennig
www.kaninkolo.de/ai
aidev \at kaninkolo \dot de



  • Prev by Date: RE: Severe ToExpression Fault?
  • Next by Date: Re: Severe ToExpression Fault?
  • Previous by thread: RE: Severe ToExpression Fault?
  • Next by thread: activation function for neural network