Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How do I convert a symbolic number to a double without loosing accuracy

Subject: How do I convert a symbolic number to a double without loosing accuracy

From: Amz Obi

Date: 19 Apr, 2009 14:33:01

Message: 1 of 4

Hi,

I've been using the symbolic toolbox in my computations. However, now I need to convert my result to a double.
Here's what my code looks like:

s = vpa(1.12302745678,12)
S = double(s)

this is the result I get
S =

    1.1230
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.
Can anyone help with this?

Kind Regards

Subject: How do I convert a symbolic number to a double without loosing accuracy

From: Roger Stafford

Date: 19 Apr, 2009 15:34:01

Message: 2 of 4

"Amz Obi" <amz@mathworks.com> wrote in message <gsfcmt$ept$1@fred.mathworks.com>...
> I've been using the symbolic toolbox in my computations. However, now I need to convert my result to a double.
> Here's what my code looks like:
>
> s = vpa(1.12302745678,12)
> S = double(s)
>
> this is the result I get
> S =
>
> 1.1230
> 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.

  It cannot be done. The "double" (IEEE-754) numbers of matlab are incapable of representing all the numbers from the symbolic toolbox exactly.

  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.

Roger Stafford

Subject: How do I convert a symbolic number to a double without loosing accuracy

From: James Tursa

Date: 19 Apr, 2009 16:38:01

Message: 3 of 4

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gsfg99$3i2$1@fred.mathworks.com>...
> "Amz Obi" <amz@mathworks.com> wrote in message <gsfcmt$ept$1@fred.mathworks.com>...
> > I've been using the symbolic toolbox in my computations. However, now I need to convert my result to a double.
> > Here's what my code looks like:
> >
> > s = vpa(1.12302745678,12)
> > S = double(s)
> >
> > this is the result I get
> > S =
> >
> > 1.1230
> > 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.
>
> It cannot be done. The "double" (IEEE-754) numbers of matlab are incapable of representing all the numbers from the symbolic toolbox exactly.
>
> 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.
>
> Roger Stafford

FYI, if you want to see what exact number 1.12302745678 is actually represented as, you can download my num2strexact utility here:

http://www.mathworks.com/matlabcentral/fileexchange/22239

e.g.,

>> num2strexact(1.12302745678)
ans =
1.12302745678000004403429556987248361110687255859375

But note what Roger says. This value is only significant to about 16 decimal digits. Don't confuse exact conversion with significance.

James Tursa

Subject: How do I convert a symbolic number to a double without loosing accuracy

From: Amz Obi

Date: 19 Apr, 2009 17:37:01

Message: 4 of 4

Thanks.
I've figured away around the problem.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us