2

Matlab sets inputs to double precision by default, so if I input a=1/3 then the variable will be converted with double precision

>> a=1/3
    a =0.3333

>> whos('a')
    a         1x1                 8  double  

However, when I input vpa(a,100) afterwards, I get:

ans=0.3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333

My question is that

since a is at double precision and has only 8 Bytes to store its data, why can it have a precision of 100 digits when it comes to vpa(a,100)? In a word, how does Matlab enhance the precision of a from 'double' to 100 digits?

Draken
  • 3,134
  • 13
  • 34
  • 54
llxxee
  • 59
  • 1
  • 6

1 Answers1

2

This is explained in the doc page of vpa, here is the link.

vpa Restores Precision of Common Double-Precision Inputs

Unlike exact symbolic values, double-precision values inherently contain round-off errors. When you call vpa on a double-precision input, vpa cannot restore the lost precision, even though it returns more digits than the double-precision value. However, vpa can recognize and restore the precision of expressions of the form p/q, p΀/q, (p/q)1/2, 2q, and 10q, where p and q are modest-sized integers.

First, demonstrate that vpa cannot restore precision for a double-precision input. Call vpa on a double-precision result and the same symbolic result.

So the answer is, that Matlab is smart enough to recover your expression, as it is of the form p/q.

Thomas
  • 1,199
  • 1
  • 14
  • 29
  • Thanks first, it's really helpful! So Matlab knows the original form of a variable even this variable is stored with double-precision and displayed in decimal fraction ? – llxxee Jul 06 '16 at 08:52
  • 3
    @lixxee It doesn't _know_ that; it just _guesses_ from the double-precision form (which is all it knows). For example, if the input to `vpa` is exactly `3.141592653589793` it guesses you meant the actual value of pi (with its infinitely many decimals) – Luis Mendo Jul 06 '16 at 09:09