vpa Does Not Show the Required Number of Significant Digits
19 ビュー (過去 30 日間)
For the below example, MATLAB shows exactly 50 significant digits:
But here it shows a much fewer digits than requested.
Why? If I embed 10 with sym, it works fine:
Why can't MATLAB produce the desired output without sym? For example, when vpa applies to pi, then no need for sym:
回答 (2 件)
the cyclist 2021 年 9 月 11 日
I'd have to spend more time to figure out the truly complete answer to this, but I'm confident that the reason is that the MATLAB representation of cos(10) is zero after the 53rd digit. So, the trailing zeros are dropped, similarly to the reason why
doesn't show all of the trailing zeros.
This kinda just shifts to the question, "Why is the MATLAB representation of cos(10) zero after 53 digits?". I don't have a full answer to that off the top of my head. I am sure someone else here will. :-)
Walter Roberson 2021 年 9 月 11 日
No, it is not correct that trailing digits after 16 digits are zero. Double precision is binary and cannot exactly represent fractions of 10 (except for special ones like 5/10 or 25/100)
When the Symbolic toolbox is asked to convert a double precision number, it goes through the following algorithm:
- is the number close to being a rational multiple of π with denominator no more than 10000 ? If so, the emit the rational fraction time symbolic π
- use a continued-fraction algorithm to determine whether the number is close to being a rational number times the square root of an integer
- use a continued-fraction algorithm to determine whether the number is close to being a rational number
- if none of the above, use the exact binary ratio
So vpa(pi,100) works because the floating point constant returned by pi is recognized as being a multiple of π first, and then vpa() of π can produce an indefinite number of digits of π
vpa(cos(10), 100) calculates cos(10) in double precision, and that gets processed though the algorithm, which gives up and represents it as an exact binary ratio -7557684451371807/9007199254740992 -- which happens to be representable in decimal in fewer than 100 digits.