Precise conversions from double to symbolic

조회 수: 2 (최근 30일)
Qian Feng
Qian Feng 2016년 12월 20일
답변: Karan Gill 2017년 1월 9일
I have the following number
r = 1.78503
I want to obtain the exact symbolic representation of 1/r, and I applied
p = 1/sym(r);
which gives me 1125899906842624/2009765110711289.
However, if I apply the form sym(1/r) then I got 2522982598259131/4503599627370496 which is different from the previous one. I understand this is due to the floating point numeric 1/r so the later form may not be accurate. Based on this observation, what measures should be taken in order to have exact values in a situation like this ? Is the form 1/sym(r) fully able to extract the exact symbolic representation here ? Thanks.
  댓글 수: 3
Qian Feng
Qian Feng 2016년 12월 20일
편집: Qian Feng 2016년 12월 21일
My apology for the mistake in my code, the value of r should be 1.78503.
Walter Roberson
Walter Roberson 2016년 12월 20일
편집: Walter Roberson 2016년 12월 20일
Change the assignment in the code I showed, and run the steps, and pick the version that you want. 2522982598259131/4503599627370496 or 1125899906842624/2009765110711289 or 100000/178503 or 560214674263177649675355596264489/1000000000000000000000000000000000 (the last would change if you changed the number of digits you have set.)

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2016년 12월 20일
편집: Walter Roberson 2016년 12월 20일
Compare:
r = 2.1234
sym(1/r)
1/sym(r)
1/sym(r,'d')
1/sym(r,'e')
1/sym(r,'f')
1/sym(r,'r')
s = sym(sprintf('%.16g', r));
feval(symengine,'numeric::rationalize',1/s,'Exact')
1/feval(symengine,'numeric::rationalize',s,'Exact')
  댓글 수: 9
Qian Feng
Qian Feng 2017년 1월 4일
Yes I understand that now. To get the exact value of 1/3 we can sym(1)/sym(3) and it does not have a error problem in this case. How about if we mix 'r' representation with 'd' objects ? I found out it seems that the rational object is automatically transferred into digits form.
Walter Roberson
Walter Roberson 2017년 1월 5일
Correct, when there are rational and decimal numeric constants being added or multiplied with each other, the rational are converted to decimal. Also, elementary functions such as exp() and cos() will be evaluated when a decimal format sym is passed in, but not when a rational is passed in. (powers of a rational might be simplified but not fully evaluated.)

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

John D'Errico
John D'Errico 2016년 12월 21일
Too late of course. But the point is that when you create r as a double:
r = 1.78503;
Then it is NOT stored exactly, as 1.78503. Nothing you do will then allow MATLAB to know that you really intended 1.78503, and not the number actually stored, which is...
sprintf('%0.55f',1.78503)
ans =
1.7850299999999998945554580132011324167251586914062500000
Even if you try to pass that number into sym, MATLAB will get it wrong, because you passed in a double precision number as far as sym was concerned.
vpa(sym(1.78503),55)
ans =
1.78502999999999989455545801320113241672515869140625
A simple solution is to go directly to symbolic form, but even there one must be careful.
r = sym('1.78503')
r =
1.78503
vpa(r,55)
ans =
1.785029999999999999999999999999999999999842248658119649
So it looks like r only had about 40 decimal digits stored. (As a guess, roughly 128 bits in the mantissa.) Still better than 16 digits though.
You can do better, by avoiding decimals completely. Integers work best.
r = sym('178503/100000')
r =
178503/100000
vpa(r,300)
ans =
1.78503
Or, you can use my HPF toolbox.
hpf('1.78503',100)
ans =
1.78503
  댓글 수: 1
James Tursa
James Tursa 2016년 12월 21일
OP still hasn't stated why there is a need for "exact" conversions and what they are being used for downstream. So I have yet to be convinced that this all isn't just a pointless exercise ...

댓글을 달려면 로그인하십시오.


Karan Gill
Karan Gill 2017년 1월 9일
Use quotes to keep your input exactly as it.
r = sym('1.78503')
But you don't get the fractional representation since you asked to keep it exactly as the decimal.
>> p = 1/r
p =
0.56021467426317764967535559626449

카테고리

Help CenterFile Exchange에서 Numbers and Precision에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by