Search code examples
matlabnumbersarbitrary-precision

MatLab - variable precision arithmetic


I have a brief question regarding the vpa command one may use to evaluate symbolic expressions in MatLab.

My textbook says the following:

"You need to be careful when you use functions such as sqrt on numbers, which by default result in a double-precision floating-point number. You need to pass such input to vpa as a symbolic string for correct evaluation: vpa('sqrt(5)/pi')."

I don't quite understand the jargon here. Why is it that for most inputs I get the exact same answer whether I type vpa(input) or vpa('input'), but not for square roots? For instance, if I type vpa(sin(pi/4)) or vpa('sin(pi/4)'), I get the exact same answers, but if I type the give problem above as vpa(sqrt(5)/pi), I do not get the same answer as when I type vpa('sqrt(5)/pi').

If someone could explain this in a little more detail than what my book does above, I would be very grateful!


Solution

  • I'm no MatLab expert, but without quotes, you're passing the result of sqrt(5)/pi into vpa():

      vpa(sqrt(5)/pi)
    = vpa(0.7117625434171772)
    

    With quotes, you're passing the expression sqrt(5)/pi (unevaluated and in exact form) into vpa() and then telling MatLab to compute sqrt(5)/pi with variable precision.