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)
sf = sprintf('%.50f',f)
str2sym(sf)
vpa(f)
double(vpa(f)) - f
vpa(f,50) % Using this form of function call doesn't help
digits 200 % this doesn't help either
vpa(f)
double(vpa(f)) - f % back conversion to double doesn't match either
What is going on here? Obviously vpa can store the exact floating point binary to decimal conversion of f:
vpa(str2sym(sf))
double(ans) - f
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
fprintf('%.50f\n',r)
vpa(r)
r = rand*100-50
fprintf('%.50f\n',r)
vpa(r)
r = rand*100-50
fprintf('%.50f\n',r)
vpa(r)
0 个评论
采纳的回答
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)
num2hex(f)
Good. That works.
vf = vpa(f)
num2hex(double(vf))
and clearly that is not the same number. So, is the problem in VPA? Instead, I'll try this.
sym(f)
Lol. That is sort of interesting. And a bit of a surprise.
digits 200
vpa(sym(f))
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
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)
which matches the SYM value exactly. I believe that up to 1075 digits are required, in the worst case.
Walter Roberson
2025-3-23
format long g
E = eps(0)
S = sprintf('%.2000f', E);
S = regexprep(S, '0+$', '')
length(S)
The 1076 includes the leading 0 and the decimal place
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File 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!