Why is vpa not converting double accurately?
6 次查看(过去 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. ;-)
19 个评论
Walter Roberson
2025-5-13
If it uses the same strategy as Maple does (MuPAD was designed as a Maple work-alike) then the strategy is something like using chains of 32 bit integers, out of which only 30 bits are (normally) used. Maple does not use base 100,000,000 (a number slightly below 2^30); it uses base 2^30 if I recall correctly.
At the moment, I do not recall how the decimal place information is stored.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Symbolic Variables, Expressions, Functions, and Settings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!