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)

References

Top Articles
Some Mumbai residents angry as the son of Asia's richest man gets married in the year's most extravagant wedding
The son of Asia's richest man gets married in the year's most extravagant wedding
Washu Parking
Shoe Game Lit Svg
Pga Scores Cbs
oklahoma city for sale "new tulsa" - craigslist
Fully Enclosed IP20 Interface Modules To Ensure Safety In Industrial Environment
Georgia Vehicle Registration Fees Calculator
Acts 16 Nkjv
Craigslist Nj North Cars By Owner
David Packouz Girlfriend
Gina's Pizza Port Charlotte Fl
Sitcoms Online Message Board
Wordscape 5832
Oscar Nominated Brings Winning Profile to the Kentucky Turf Cup
California Department of Public Health
Luna Lola: The Moon Wolf book by Park Kara
Bj Alex Mangabuddy
Tygodnik Polityka - Polityka.pl
How to Create Your Very Own Crossword Puzzle
1989 Chevy Caprice For Sale Craigslist
Company History - Horizon NJ Health
Glover Park Community Garden
Tips and Walkthrough: Candy Crush Level 9795
If you have a Keurig, then try these hot cocoa options
Johnnie Walker Double Black Costco
Watertown Ford Quick Lane
Garden Grove Classlink
Safeway Aciu
Unreasonable Zen Riddle Crossword
Buhl Park Summer Concert Series 2023 Schedule
Valley Craigslist
Does Circle K Sell Elf Bars
A Grade Ahead Reviews the Book vs. The Movie: Cloudy with a Chance of Meatballs - A Grade Ahead Blog
UPS Drop Off Location Finder
Avance Primary Care Morrisville
Arcadia Lesson Plan | Day 4: Crossword Puzzle | GradeSaver
How are you feeling? Vocabulary & expressions to answer this common question!
Thanksgiving Point Luminaria Promo Code
Wo ein Pfand ist, ist auch Einweg
Suffix With Pent Crossword Clue
Ursula Creed Datasheet
Nail Salon Open On Monday Near Me
Backpage New York | massage in New York, New York
Amateur Lesbian Spanking
Colin Donnell Lpsg
Every Type of Sentinel in the Marvel Universe
Steam Input Per Game Setting
Marine Forecast Sandy Hook To Manasquan Inlet
Solving Quadratics All Methods Worksheet Answers
Karen Kripas Obituary
Stone Eater Bike Park
Latest Posts
Article information

Author: Allyn Kozey

Last Updated:

Views: 6205

Rating: 4.2 / 5 (43 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Allyn Kozey

Birthday: 1993-12-21

Address: Suite 454 40343 Larson Union, Port Melia, TX 16164

Phone: +2456904400762

Job: Investor Administrator

Hobby: Sketching, Puzzles, Pet, Mountaineering, Skydiving, Dowsing, Sports

Introduction: My name is Allyn Kozey, I am a outstanding, colorful, adventurous, encouraging, zealous, tender, helpful person who loves writing and wants to share my knowledge and understanding with you.