http://www.mathworks.com/matlabcentral/newsreader/view_thread/249398
MATLAB Central Newsreader  How do I convert a symbolic number to a double without loosing accuracy
Feed for thread: How do I convert a symbolic number to a double without loosing accuracy
enus
©19942015 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Sun, 19 Apr 2009 14:33:01 +0000
How do I convert a symbolic number to a double without loosing accuracy
http://www.mathworks.com/matlabcentral/newsreader/view_thread/249398#643811
Amz Obi
Hi, <br>
<br>
I've been using the symbolic toolbox in my computations. However, now I need to convert my result to a double. <br>
Here's what my code looks like:<br>
<br>
s = vpa(1.12302745678,12)<br>
S = double(s)<br>
<br>
this is the result I get<br>
S =<br>
<br>
1.1230<br>
but I don't want the number rounded up 5 sig figures. I need the double to be exactly the same as the symbolic number. <br>
Can anyone help with this?<br>
<br>
Kind Regards

Sun, 19 Apr 2009 15:34:01 +0000
Re: How do I convert a symbolic number to a double without loosing accuracy
http://www.mathworks.com/matlabcentral/newsreader/view_thread/249398#643819
Roger Stafford
"Amz Obi" <amz@mathworks.com> wrote in message <gsfcmt$ept$1@fred.mathworks.com>...<br>
> I've been using the symbolic toolbox in my computations. However, now I need to convert my result to a double. <br>
> Here's what my code looks like:<br>
> <br>
> s = vpa(1.12302745678,12)<br>
> S = double(s)<br>
> <br>
> this is the result I get<br>
> S =<br>
> <br>
> 1.1230<br>
> but I don't want the number rounded up 5 sig figures. I need the double to be exactly the same as the symbolic number. <br>
<br>
It cannot be done. The "double" (IEEE754) numbers of matlab are incapable of representing all the numbers from the symbolic toolbox exactly.<br>
<br>
However, I believe the difficulty you are showing here is that you are using format short instead of format long. Double numbers are actually accurate to about 16 decimal places, so you should have been able to display 1.12302745678 accurately. This is only a problem with the display, not with the underlying accuracy of the double numbers within matlab.<br>
<br>
Roger Stafford

Sun, 19 Apr 2009 16:38:01 +0000
Re: How do I convert a symbolic number to a double without loosing accuracy
http://www.mathworks.com/matlabcentral/newsreader/view_thread/249398#643827
James Tursa
"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gsfg99$3i2$1@fred.mathworks.com>...<br>
> "Amz Obi" <amz@mathworks.com> wrote in message <gsfcmt$ept$1@fred.mathworks.com>...<br>
> > I've been using the symbolic toolbox in my computations. However, now I need to convert my result to a double. <br>
> > Here's what my code looks like:<br>
> > <br>
> > s = vpa(1.12302745678,12)<br>
> > S = double(s)<br>
> > <br>
> > this is the result I get<br>
> > S =<br>
> > <br>
> > 1.1230<br>
> > but I don't want the number rounded up 5 sig figures. I need the double to be exactly the same as the symbolic number. <br>
> <br>
> It cannot be done. The "double" (IEEE754) numbers of matlab are incapable of representing all the numbers from the symbolic toolbox exactly.<br>
> <br>
> However, I believe the difficulty you are showing here is that you are using format short instead of format long. Double numbers are actually accurate to about 16 decimal places, so you should have been able to display 1.12302745678 accurately. This is only a problem with the display, not with the underlying accuracy of the double numbers within matlab.<br>
> <br>
> Roger Stafford<br>
<br>
FYI, if you want to see what exact number 1.12302745678 is actually represented as, you can download my num2strexact utility here:<br>
<br>
<a href="http://www.mathworks.com/matlabcentral/fileexchange/22239">http://www.mathworks.com/matlabcentral/fileexchange/22239</a><br>
<br>
e.g.,<br>
<br>
>> num2strexact(1.12302745678)<br>
ans =<br>
1.12302745678000004403429556987248361110687255859375<br>
<br>
But note what Roger says. This value is only significant to about 16 decimal digits. Don't confuse exact conversion with significance.<br>
<br>
James Tursa

Sun, 19 Apr 2009 17:37:01 +0000
Re: How do I convert a symbolic number to a double without loosing accuracy
http://www.mathworks.com/matlabcentral/newsreader/view_thread/249398#643835
Amz Obi
Thanks. <br>
I've figured away around the problem.