Precise conversions from double to symbolic

11 views (last 30 days)
Qian Feng
Qian Feng on 20 Dec 2016
Answered: Karan Gill on 9 Jan 2017
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.
Walter Roberson
Walter Roberson on 20 Dec 2016
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.)

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 20 Dec 2016
Edited: Walter Roberson on 20 Dec 2016
r = 2.1234
s = sym(sprintf('%.16g', r));
Walter Roberson
Walter Roberson on 5 Jan 2017
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.)

Sign in to comment.

More Answers (2)

John D'Errico
John D'Errico on 21 Dec 2016
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...
ans =
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.
ans =
A simple solution is to go directly to symbolic form, but even there one must be careful.
r = sym('1.78503')
r =
ans =
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 =
ans =
Or, you can use my HPF toolbox.
ans =
  1 Comment
James Tursa
James Tursa on 21 Dec 2016
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 ...

Sign in to comment.

Karan Gill
Karan Gill on 9 Jan 2017
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 =

Community Treasure Hunt

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

Start Hunting!

Translated by