Why is vpa not converting double accurately?

13 次查看(过去 30 天)
I ran across this example that I don't understand. I don't use the Symbolic Toolbox much, but I thought vpa by default converted doubles to symbolic representations that were an accurate conversion up to so many decimal digits. I.e., an exact floating point binary to decimal conversion up to the specified number of decimal digits. But the following did not match my expectations:
format long
digits 50
hf = '40108c55a5de2880'; % making sure I am starting from the value I think I am
f = hex2num(hf)
f =
4.137045470890484
sf = sprintf('%.50f',f)
sf = '4.13704547089048446650849655270576477050781250000000'
str2sym(sf)
ans = 
4.1370454708904844665084965527057647705078125
vpa(f)
ans = 
4.1370454708905203952304813814278565783465431783307
double(vpa(f)) - f
ans =
3.552713678800501e-14
vpa(f,50) % Using this form of function call doesn't help
ans = 
4.1370454708905203952304813814278565783465431783307
digits 200 % this doesn't help either
vpa(f)
ans = 
4.1370454708905203952304813814278565783465431783307106731417481082606305993533207364559999183814049903246844172718745791741538190785178147855046143279189519799161307279637872963853860781001401307209969
double(vpa(f)) - f % back conversion to double doesn't match either
ans =
3.552713678800501e-14
What is going on here? Obviously vpa can store the exact floating point binary to decimal conversion of f:
vpa(str2sym(sf))
ans = 
4.1370454708904844665084965527057647705078125
double(ans) - f
ans =
0
I have to use fprintf with str2sym to get an accurate conversion. Why doesn't vpa do that with f in the first place? Is there some setting I am unaware of? And what are all those digits pouring out of vpa for this f?
Is this some type of p/q morphing going on in the background?
Other random values generally match my expectations. E.g.,
r = rand*100-50
r =
36.006640442455534
fprintf('%.50f\n',r)
36.00664044245553441214724443852901458740234375000000
vpa(r)
ans = 
36.00664044245553441214724443852901458740234375
r = rand*100-50
r =
-47.047616571398464
fprintf('%.50f\n',r)
-47.04761657139846420250250957906246185302734375000000
vpa(r)
ans = 
r = rand*100-50
r =
4.524320317487962
fprintf('%.50f\n',r)
4.52432031748796248393773566931486129760742187500000
vpa(r)
ans = 
4.524320317487962483937735669314861297607421875

采纳的回答

John D'Errico
John D'Errico 2025-3-22
编辑:John D'Errico 2025-3-22
Quite interesting. When I first saw the title, I assumed it would be an obvious mistake. Then I saw the poster, and knew it would be interesting. My first assumption is the problem must lie in VPA, which sometimes seems to stand for:
VPA - Very Poor Arithmetic
If I look at what is happening though...
format long
digits 50
hf = '40108c55a5de2880'; % making sure I am starting from the value I think I am
f = hex2num(hf)
f =
4.137045470890484
num2hex(f)
ans = '40108c55a5de2880'
Good. That works.
vf = vpa(f)
vf = 
4.1370454708905203952304813814278565783465431783307
num2hex(double(vf))
ans = '40108c55a5de28a8'
and clearly that is not the same number. So, is the problem in VPA? Instead, I'll try this.
sym(f)
ans = 
Lol. That is sort of interesting. And a bit of a surprise.
digits 200
vpa(sym(f))
ans = 
4.1370454708905203952304813814278565783465431783307106731417481082606305993533207364559999183814049903246844172718745791741538190785178147855046143279189519799161307279637872963853860781001401307209969
Hmm. Is that really what is happening? What does that strange number with the radicals resolve to as a decimal? I'll try HPF, since I know exactly what HPF is doing. Hey, I trust HPF implicitly, since I know the guy who wrote it.
DefaultNumberOfDigits 200
x = sqrt(hpf(241))*sqrt(hpf(16499))/hpf(482)
x =
4.1370454708905203952304813814278565783465431783307106731417481082606305993533207364559999183814049903246844172718745791741538190785178147855046143279189519799161307279637872963853860781001401307209969
Ah. So now I know what happened. Well, I think I do, I think I do.
When you do this:
vf = vpa(f)
MATLAB FIRST converts the number to a sym, because VPA only understands symbolic numbers. Effectively, it expands it as
vf = vpa(sym(f))
But what does sym do? It decides the result of sym(f) is sqrt(241)*sqrt(16499)/482. After all, that is what we would all do, right? The obvious form. Then, and ONLY then, does it throw it into VPA.
So the problem is not in VPA. The problem lies in the decision to approximate that floating point number as a sym in the form it chose. The problem lies in sym.
My head hurts, just a little. ;-)
  17 个评论
Stephen23
Stephen23 2025-3-23
"Am I misunderstanding the quoted passage from the doc page (or some other aspect of this example)?"
15 digits are not enough to show the exact numeric value. The numeric value is precisely
hf = '40108c55a5de2880'; % making sure I am starting from the value I think I am
f = hex2num(hf);
fprintf('%.999g\n',f)
4.1370454708904844665084965527057647705078125
which matches the SYM value exactly. I believe that up to 1075 digits are required, in the worst case.
Walter Roberson
Walter Roberson 2025-3-23
format long g
E = eps(0)
E =
4.94065645841247e-324
S = sprintf('%.2000f', E);
S = regexprep(S, '0+$', '')
S = '0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004940656458412465441765687928682213723650598026143247644255856825006755072702087518652998363616359923797965646954457177309266567103559397963987747960107818781263007131903114045278458171678489821036887186360569987307230500063874091535649843873124733972731696151400317153853980741262385655911710266585566867681870395603106249319452715914924553293054565444011274801297099995419319894090804165633245247571478690147267801593552386115501348035264934720193790268107107491703332226844753335720832431936092382893458368060106011506169809753078342277318329247904982524730776375927247874656084778203734469699533647017972677717585125660551199131504891101451037862738167250955837389733598993664809941164205702637090279242767544565229087538682506419718265533447265625'
length(S)
ans =
1076
The 1076 includes the leading 0 and the decimal place

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Symbolic Variables, Expressions, Functions, and Preferences 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by