Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I searched and did not find similar answers which can resolve the 'no precision loss' issue. A sample string is:

"-5.100686209408900133332e+02 -1.294005398404007344443e+01 -2.59376479781563728887e+02 -1.3043629998334040122222e+02"

If I immediately convert them into real variable by ImportString or StringToDouble, then there will be precision loss, the resulted precision is only $MachinePrecision.

How to convert them into real number which should have higher precision than $MachinePrecision.

share|improve this question
    
what is StringToDouble ? –  Nasser Dec 17 '13 at 7:19
3  
@Nasser : mathematica.stackexchange.com/a/1744/121 –  Mr.Wizard Dec 17 '13 at 8:22
add comment

2 Answers

up vote 5 down vote accepted

This question feels familiar but I could not find a true duplicate.

You can use the string replacement that Oleksandr proposed here and then use ToExpression to convert the numbers:

string = "-5.100686209408900133332e+02 -1.294005398404007344443e+01 \
-2.59376479781563728887e-02 -1.3043629998334040122222e+02"

ToExpression /@ StringReplace[StringSplit@string, "e" | "E" :> "*^"]
{-510.068620940890013333, -12.94005398404007344443, -0.025937647978156372889,
 -130.43629998334040122222}

Edit: a bug in verison 9 prevents the following solution from working.

Better I think, avoiding accidential code evaluation from ToExpression, you can use Read or ReadList with type Number, as I proposed for How do you convert a string containing a number in C scientific notation to a Mathematica number?:

# &[ReadList[#, Number], Close@#]& @ StringToStream @ string
{-510.068620940890013333, -12.94005398404007344443, -0.025937647978156372889,
 -130.43629998334040122222}

Note that # &[body, cleanup] is simply a way to evaluate cleanup after body, then return body.

share|improve this answer
    
However, the further command causes: error message: `General::digit: "Digit at position 1 in \!(\"-5.100686209408900133332\") is too large to be used in base 10. "' –  LCFactorization Dec 17 '13 at 10:01
    
@LCFactorization Please, what code causes that error? –  Mr.Wizard Dec 17 '13 at 10:07
    
# &[ReadList[#, Number], Close@#]& @ StringToStream @ string will casue such error; others are OK. –  LCFactorization Dec 17 '13 at 11:25
    
@LCF With what input? (What is assigned to string?) Which version are you using? I have not yet seen the error you describe. –  Mr.Wizard Dec 17 '13 at 11:49
1  
Yes, this is a version difference. It stops working in version 9. I suspect a bug since the errors do not make sense and a very strange output is produced ({100*Removed["$$Failure"], 10*Removed["$$Failure"], Removed["$$Failure"]/100, 100*Removed["$$Failure"]}). –  Oleksandr R. Dec 19 '13 at 11:48
show 3 more comments

The following is a place to start: Use StringToStream to convert the string data to an input stream:

num = "-5.100686209408900133332e+02 -1.294005398404007344443e+01
-2.59376479781563728887e+02 -1.3043629998334040122222e+02"

Now

str = StringToStream[num];

Use ReadList to read the InputStream

data = ReadList[str, Table[Record, {4}], RecordSeparators -> {" "}]; Close[str];

Here's the main trick to get your high precision Real

ReleaseHold[ToExpression[data, InputForm, Hold] /. {Plus[Times[x_, E | e], y_] :> x*10^y}]

Which gives:

{{-510.068620940890013333, -12.94005398404007344443, -259.37647978156372889,
 -130.43629998334040122222}}
share|improve this answer
1  
I believe that there are simpler alternatives to this, as shown in my answer. Can you give any reason to use this more elaborate method? –  Mr.Wizard Dec 17 '13 at 8:49
    
Though this answer works, I am curious why there is so such command as close the Stream... –  LCFactorization Dec 17 '13 at 8:49
1  
@LCFactorization closing the stream is important; see my answer for simple code to do this. –  Mr.Wizard Dec 17 '13 at 8:50
    
And there is such error messsage : ToExpression::notstr: The format type InputForm is valid only for string input –  LCFactorization Dec 17 '13 at 9:02
1  
@LCFactorization, your example shows a string input, am I missing something? –  RunnyKine Dec 17 '13 at 9:10
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.