Change variable precision used - MATLAB digits (2024)

Change variable precision used

collapse all in page

Syntax

digits(d)

d1 = digits

d1 = digits(d)

Description

example

digits(d) sets theprecision used by vpa to d significantdecimal digits. The default is 32 digits.

example

d1 = digits returnsthe current precision used by vpa.

example

d1 = digits(d) setsthe new precision d and returns the old precisionin d1.

Examples

Increase Precision of Results

By default, MATLAB® uses 16 digits of precision.For higher precision, use vpa.The default precision for vpa is 32 digits. Increaseprecision beyond 32 digits by using digits.

Find pi using vpa, whichuses the default 32 digits of precision. Confirm that the currentprecision is 32 by using digits.

pi32 = vpa(pi)
pi32 =3.1415926535897932384626433832795
currentPrecision = digits

Save the current value of digits in digitsOld andset the new precision to 100 digits. Find pi using vpa.The result has 100 digits.

digitsOld = digits(100);pi100 = vpa(pi)
pi100 =3.1415926535897932384626433832795028841971693993751058209...74944592307816406286208998628034825342117068

Note

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

Lastly, restore the old value of digits forfurther calculations.

digits(digitsOld)

For more information, see Increase Precision of Numeric Calculations.

Increase Speed by Decreasing Precision

Increase the speed of MATLAB calculationsby using vpa with a lower precision. Set thelower precision by using digits.

First, find the time taken to perform an operation on a largeinput.

input = 1:0.01:500;ticzeta(input);toc
Elapsed time is 48.968983 seconds.

Now, repeat the operation with a lower precision by using vpa.Lower the precision to 10 digits by using digits.Then, use vpa to reduce the precision of input andperform the same operation. The time taken decreases significantly.

digitsOld = digits(10);vpaInput = vpa(input);ticzeta(vpaInput);toc
Elapsed time is 31.450342 seconds.

Note

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

Lastly, restore the old value of digits forfurther calculations.

digits(digitsOld)

For more information, see Increase Speed by Reducing Precision.

Guard Digits

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

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

Now, display a using 20 digits. The resultshows that the toolbox internally used more than four digits whencomputing a. The last digits in the following resultare 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 with10-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 resultis not 0:

a - b
ans =0.000000000000000000086736173798840354720600815844403

The difference a - b is not equal to zerobecause the toolbox internally boosts the 10-digit number b= 0.1 to 32-digit accuracy. This process implies round-offerrors. 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, andthen perform VPA operations on that object. The results can dependon the conversion technique that you used to convert a floating-pointnumber to a symbolic object. The sym function letsyou choose the conversion technique by specifying the optional secondargument, which can be 'r', 'f', 'd',or 'e'. The default is 'r'.For example, convert the constant π=3.141592653589793... toa 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 thescreen, they are rational approximations of pi.Use vpa to convert these rational approximationsof pi back to floating-point values.

Set the number of digits to 4. Three of the four approximationsgive 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 betweenthe 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

collapse all

dNew accuracy setting
number | symbolic number

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

Output Arguments

collapse all

d1 — Current accuracy setting
double-precision number

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

Version History

Introduced before R2006a

See Also

double | vpa

Topics

  • Increase Precision of Numeric Calculations
  • Recognize and Avoid Round-Off Errors
  • Increase Speed by Reducing Precision
  • Change Output Format of Symbolic and Variable-Precision Arithmetic
Change variable precision used - MATLAB digits (2024)
Top Articles
Latest Posts
Article information

Author: Wyatt Volkman LLD

Last Updated:

Views: 6251

Rating: 4.6 / 5 (66 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Wyatt Volkman LLD

Birthday: 1992-02-16

Address: Suite 851 78549 Lubowitz Well, Wardside, TX 98080-8615

Phone: +67618977178100

Job: Manufacturing Director

Hobby: Running, Mountaineering, Inline skating, Writing, Baton twirling, Computer programming, Stone skipping

Introduction: My name is Wyatt Volkman LLD, I am a handsome, rich, comfortable, lively, zealous, graceful, gifted person who loves writing and wants to share my knowledge and understanding with you.