Function: vpa (x)
Function: vpa (x, n)

Create a variable-precision floating point number.

x can be a string, a sym or a double. Example:

x = vpa('1/3', 32)
  ⇒ x = (sym) 0.33333333333333333333333333333333
a = sym(1)/3;
x = vpa(a, 32)
  ⇒ x = (sym) 0.33333333333333333333333333333333

If n is omitted it defaults to the current value of digits().

Be careful when creating a high-precision float from a double as you will generally only get 15 digits:

vpa(1/3)
  ⇒ (sym) 0.3333333333333333148296162...
vpa(sqrt(2));
ans^2
  ⇒ (sym) 2.0000000000000002734323463...

For the same reason, passing numbers with decimal points may produce undesirable results:

vpa(0.1)
  ⇒ (sym) 0.1000000000000000055511151...

Instead, enclose the decimal number in a string:

vpa('0.1')
  ⇒ (sym) 0.10000000000000000000000000000000

Very simple expressions can also be enclosed in quotes:

vpa('sqrt(2)')
  ⇒ (sym) 1.4142135623730950488016887242097

But be careful as this can lead to unexpected behaviour, such as low-precision results if the string contains decimal points:

vpa('cos(0.1)')
  -| warning: string expression involving decimals is
  -|          dangerous, see "help vpa"...
  ⇒ ans = (sym) 0.99500416527802...

Instead, it is preferable to use sym or vpa on the inner-most parts of your expression:

cos(vpa('0.1'))
  ⇒ (sym) 0.99500416527802576609556198780387
vpa(cos(sym(1)/10))
  ⇒ (sym) 0.99500416527802576609556198780387

See also: sym, vpasolve, digits.

Package: symbolic