Quantcast

Documentation Center

  • Trial Software
  • Product Updates

digits

Variable-precision accuracy

Syntax

Description

example

digits shows the number of significant decimal digits that MuPAD® software uses to do variable-precision arithmetic (VPA). The default value is 32 digits.

example

digits(d) sets the current VPA accuracy to d significant decimal digits. The value d must be a positive integer greater than 1 and less than 229 + 1.

example

d1 = digits assigns the current setting of digits to variable d1.

example

d1 = digits(d) assigns the current setting of digits to variable d1 and sets VPA accuracy to d.

Examples

Default Accuracy of Variable-Precision Computations

By default, the minimum number of significant (nonzero) decimal digits is 32.

To obtain the current number of digits, use digits without input arguments:

digits
Digits = 32

To save the current setting, assign the result returned by digits to a variable:

CurrentDigits = digits
CurrentDigits =
    32

Control Accuracy of Variable-Precision Computations

digits lets you specify any number of significant decimal digits from 1 to 229 + 1.

Compute the ratio 1/3 and the ratio 1/3000 with four-digit accuracy:

old = digits(4);
vpa(1/3)
vpa(1/3000)
ans =
0.3333
 
ans =
0.0003333

Restore the default accuracy setting for further computations:

digits(old)

"Guard" Digits

The number of digits that you specify using the vpa function or the digits function is the guaranteed number of digits. Internally, the toolbox can use a few more digits than you specify. These additional digits are called guard digits. For example, set the number of digits to 4, and then display the floating-point approximation of 1/3 using four digits:

old = digits(4);
a = vpa(1/3)
a =
0.3333

Now, display a using 20 digits. The result shows that the toolbox internally used more than four digits when computing a. The last digits in the following result are incorrect because of the round-off error:

digits(20)
vpa(a)
digits(old)
ans =
0.33333333333303016843

Hidden Round-Off Errors

Hidden round-off errors can cause unexpected results. For example, compute the number 1/10 with the default 32-digit accuracy and with 10-digit accuracy:

a = vpa(1/10)
old = digits(10);
b = vpa(1/10)
digits(old)
a =
0.1
 
b =
0.1

Now, compute the difference a - b. The result is not 0:

a - b
ans =
0.000000000000000000086736173798840354720600815844403

The difference a - b is not equal to zero because the toolbox internally boosts the 10-digit number b = 0.1 to 32-digit accuracy. This process implies round-off errors. The toolbox actually computes the difference a - b as follows:

b = vpa(b)
a - b
b =
0.09999999999999999991326382620116
 
ans =
0.000000000000000000086736173798840354720600815844403

Techniques Used to Convert Floating-Point Numbers to Symbolic Objects

Suppose you convert a double number to a symbolic object, and then perform VPA operations on that object. The results can depend on the conversion technique that you used to convert a floating-point number to a symbolic object. The sym function lets you choose the conversion technique by specifying the optional second argument, which can be 'r', 'f', 'd', or 'e'. The default is 'r'. For example, convert the constant π = 3.141592653589793... to a symbolic object:

r = sym(pi)
f = sym(pi,'f')
d = sym(pi,'d')
e = sym(pi,'e')
r =
pi
 
f =
884279719003555/281474976710656
 
d =
3.1415926535897931159979634685442
 
e =
pi - (198*eps)/359

Although the toolbox displays these numbers differently on the screen, they are rational approximations of pi. Use vpa to convert these rational approximations of pi back to floating-point values.

Set the number of digits to 4. Three of the four approximations give the same result.

digits(4)
vpa(r)
vpa(f)
vpa(d)
vpa(e)
ans =
3.142
 
ans =
3.142
 
ans =
3.142
 
ans =
3.142 - 0.5515*eps

Now, set the number of digits to 40. The differences between the symbolic approximations of pi become more visible.

digits(40)
vpa(r)
vpa(f)
vpa(d)
vpa(e)
ans =
3.141592653589793238462643383279502884197
 
ans =
3.141592653589793115997963468544185161591
 
ans =
3.1415926535897931159979634685442
 
ans =
3.141592653589793238462643383279502884197 -...
0.5515320334261838440111420612813370473538*eps

Input Arguments

expand all

d — New accuracy settingnumber | symbolic number

New accuracy setting, specified as a number or symbolic number. The setting specifies the number of significant decimal digits to be used for variable-precision calculations. If the value d is not an integer, digits rounds it to the nearest integer.

Output Arguments

expand all

d1 — Current accuracy settingdouble-precision number

Current accuracy setting, returned as a double-precision number. The setting specifies the number of significant decimal digits currently used for variable-precision calculations.

See Also

|

Related Examples

Was this topic helpful?