Main Content

Increase Precision of Numeric Calculations

By default, MATLAB®uses 16 digits of precision. For higher precision, use thevpafunction in Symbolic Math Toolbox™.vpaprovides variable precision which can be increased without limit.

When you choose variable-precision arithmetic, by default,vpauses 32 significant decimal digits of precision. For details, seeChoose Numeric or Symbolic Arithmetic. You can set a higher precision by using thedigitsfunction.

Approximate a sum using the default precision of 32 digits. If at least one input is wrapped withvpa, all other inputs are converted to variable precision automatically.

vpa(1/3) + 1/2
ans = 0.83333333333333333333333333333333

You must wrap all inner inputs withvpa, such asexp(vpa(200)). Otherwise, the inputs are automatically converted to double by MATLAB.

Increase the precision to50digits by usingdigitsand save the old value ofdigitsindigitsOld. Repeat the sum.

digitsOld = digits(50); sum50 = vpa(1/3) + 1/2
sum50 = 0.83333333333333333333333333333333333333333333333333

恢复旧值的位数进行进一步的计算ations.

digits(digitsOld)

Note

vpaoutput is symbolic. To use symbolic output with a MATLAB function that does not accept symbolic values, convert symbolic values to double precision by usingdouble.

Check the currentdigitssetting by callingdigits.

digits
Digits = 32

Change the precision for a singlevpacall by specifying the precision as the second input tovpa. This call does not affectdigits. For example, approximatepiwith100digits.

vpa(pi,100)
ans = 3.14159265358979323846264338327950288419716939937510582097494 4592307816406286208998628034825342117068
digits % digits remains 32
Digits = 32

Variable precision can be increased arbitrarily. Findpito500digits.

digitsOld = digits(500); vpa(pi) digits(digitsOld)
ans = 3.1415926535897932384626433832795028841971693993751058209749 445923078164062862089986280348253421170679821480865132823066 470938446095505822317253594081284811174502841027019385211055 596446229489549303819644288109756659334461284756482337867831 652712019091456485669234603486104543266482133936072602491412 737245870066063155881748815209209628292540917153643678925903 600113305305488204665213841469519415116094330572703657595919 530921861173819326117931051185480744623799627495673518857527 248912279381830119491

digitsandvpacontrol the number ofsignificantdecimal digits. For example, approximating1/111with four-digit accuracy returns six digits after the decimal point because the first two digits are zeros.

vpa(1/111,4)
ans = 0.009009

Note

If you want to increase performance bydecreasingprecision, seeIncrease Speed by Reducing Precision.