FromDigits only works for integer strings. What's its real-number analogue?
2 Answers
This is a good question, but unfortunately there doesn't seem to be a perfect solution.
You can use
ToExpression, e.g.ToExpression["1.23"]. But: (1) this gives no error checking (2) it's a serious security risk if you obtain the string from users (and it can go things go haywire in general if the string comes from an unknown source)Internal`StringToDoublecan parse floating point numbers in C format. It accepts both"1.24"and"12.4e-1". But: (1) is is undocumented so there are no guarantees of compatibility or that it won't crash your kernel (2) it still doesn't offer error checkingIn version 10, there's
Interpreter. For example,Interpreter["Number"]["1.24"]. It is flexible, supports both1.23e4and1.23*10^4. It provides error checking. It's probably the best choice. But: It is very slow, and unsuitable for parsing a long list of numbers. Parsing only 1000 numbers takes a full second on my i7 machine. It doesn't make it possible to implement e.g. a CSV parser in Mathematica.
So none of these is a perfect solution. There's always the choice to implement your own in C if you need all of speed, reliability and error checking. But it takes a lot of work to do this.
Thanks to @chuy in the chatroom, here's a way to make ToExpression safer:
toNum[e_String] := Replace[
Quiet@ToExpression[e, InputForm, HoldComplete],
{HoldComplete[n : (_Integer | _Real)] :> n, _ -> $Failed}
]
This is safer than using ToExpression alone and much faster than Interpreter["Number"]. It handles numbers that follow the Mathematica syntax, so keep in mind that strange looking things such as toNum["16^^abc"] will work.
-
I concur on the slowness of Interpreter. It was probably developed to parse WolframAlpha input, i.e., just a single line. It's totally and utterly useless for any serious big data project. – Sjoerd C. de Vries Apr 01 '15 at 07:44
Here’s first code line of Stephen Wolfram’s Pi or Pie?! Celebrating Pi Day of the Century blog post:
PiString = StringDrop[ToString[N[Pi, 10^2]], {2}];
Stephen converted the first 10 million digits of π to a string without a decimal point, but I’ve taken just the first 100 digits. The real number analogue of FromDigits would be:
PiApproximate = FromDigits[PiString]/10^(10^2 - 1);
Convert this approximate π into a string:
PiStringApproximate = StringDrop[ToString[N[PiApproximate, 10^2]], {2}];
and yes, it exactly matches the real thing:
PiStringApproximate === PiString
- 1,215
- 8
- 12
-
I believe the question is: How can we parse floating point numbers? This doesn't answer that. – Szabolcs Mar 31 '15 at 20:25
-
@Szabolcs Where do I find this chat that you mention in your answer? – Christopher Haydock Apr 01 '15 at 00:52
-
Click the StackExchange logo at the top of the page, and click chat. Or click here: [chat]. Unlike the main site, the chat is free, any topic goes. – Szabolcs Apr 01 '15 at 03:53
-
For the interested reader's benefit, here's the @chuy chat link. – Christopher Haydock Apr 01 '15 at 15:35
RealDigits[ToExpression["1234.234532"]]gives{{1, 2, 3, 4, 2, 3, 4, 5, 3, 2, 0, 0, 0, 0, 0, 0}, 4}– Nasser Jan 26 '15 at 04:53ToExpressionor someInterpreter, orInternal`StringToDouble– Rojo Jan 26 '15 at 06:06ImportString["1.234 5.678e2", "Table"]might do the sort of thing that you want. – Stephen Luttrell Jan 26 '15 at 10:21