How can I increase the precision of a variable after decimal?
Show older comments
Dear Contributers,
I am in trouble with digits. I need to tell you that I searched for long time. However I could not find the answer. I hope I can find in here. The problem is;
There is c value which is changing depends on previous operations. and there is r
let say; after operations, c=75.00002874 and r=75 when program implements c-r , the result becomes 0. Because c is taken as 75.0000 by program. I need to increase the digit numbers after decimal for c. But not by using fprintf , format long etc. Because They only affect for displaying the value on the screen. I do not need to display c or r. I need the correct result when c-r operated such as; c-r = 75.00002874 - 75 = 0.00002874
the equation of c is >>
c= hypot(X(1) , Y(1))
X(1) and Y(1) depends on previous operations, too. But their digits number after decimal is enough to evaluate the c. So I only need to increase the digits number of c so the program can take the c into the c-r operation with increased precision.
How can I do that? I am so stucked. Thanks
19 Comments
John D'Errico
on 4 Dec 2015
Edited: John D'Errico
on 4 Dec 2015
Sorry. But you are simply wrong in your claim.
c=75.00002874;
r=75;
c - r
ans =
2.874e-05
I don't know what you are doing, but the fact is, your claim about what MATLAB is doing is simply incorrect. Even if those numbers were stored in single precision, the difference is still not identically zero.
MATLAB operates in double precision, which is entirely adequate to compute this difference. No, you cannot increase the precision, at least not simply. You can use tools like the symbolic toolbox, or my own HPF, but they will greatly slow down the speed of computations.
Star Strider
on 4 Dec 2015
Ender Rencuzogullari
on 4 Dec 2015
John D'Errico
on 4 Dec 2015
No. It simply does not do this in double precision. Again, you are wrong. Give an example that shows your claim. You will not succeed in doing so, at least not at that level of precision.
Perhaps you are working in single precision, which is ALMOST at the edge of doing something like this. But then the answer is simple. USE DOUBLE PRECISION!
Ender Rencuzogullari
on 4 Dec 2015
Ender Rencuzogullari
on 4 Dec 2015
John D'Errico
on 4 Dec 2015
But this proves NOTHING.
All you have shown is that when displayed to 6 significant digits, that both numbers are identical. The tiny picture you have pasted in is unreadable, so meaningless.
Using format long, show the full value of those numbers. Then compute the difference, using MATLAB. You will see that indeed, MATLAB is not making the mistake you claim it is doing.
Star Strider
on 4 Dec 2015
You did not ask anything wrong.
It seems the point you defined as the origin of the tangents to your circle is either on or in the circle. That will produce erroneous results, so my code throws an error and exits the function.
Please type:
format long
c
rr
d = c - rr
in your Command Window and then copy those results and paste them to a Comment here.
Ender Rencuzogullari
on 4 Dec 2015
Edited: per isakson
on 4 Dec 2015
John D'Errico
on 5 Dec 2015
So I fail to see the problem. Both c and rr are identical. You have proved this yourself. When subtracted, you get exactly 0. Why are you surprised?
Star Strider
on 5 Dec 2015
Edited: Star Strider
on 5 Dec 2015
That will fail with my code, as it should. The point has to be farther from the centre of the circle than the radius. It is not.
It is on the circle, so my first code in your other Question (that plots the tangent to the circle from a point on the circle) is what you want. (It will fail as well for an infinite slope — a vertical line — but I did not specifically trap that in my code.)
Ender Rencuzogullari
on 5 Dec 2015
Ender Rencuzogullari
on 5 Dec 2015
Edited: Ender Rencuzogullari
on 5 Dec 2015
John D'Errico
on 5 Dec 2015
Edited: John D'Errico
on 5 Dec 2015
I'm telling you that you think something is happening, but that it is not actually happening as you think it is. That is fact. Truth is tough. You THINK that event A happened, and are looking for a solution to the wrong problem, because, in fact, event A did NOT happen. Something DID happen, of that I am sure. You have a problem. But now you need to start looking for the actual problem.
You need to learn to use the debugging tools. You will not understand what you are doing wrong until you examine your code closely to find the problem. But asking why something happens that does not actually happen as you say is a meaningless question. Again, fact.
Look at the result of EVERY line of your code (at least every line where you suspect something strange might happen.) See where something happens that is wrong.
Ender Rencuzogullari
on 5 Dec 2015
Edited: Ender Rencuzogullari
on 5 Dec 2015
Star Strider
on 5 Dec 2015
That is correct. If you want to see the tangent lines touch the circle from a point that close, you have to increase the resolution of your data calculations. For instance, instead of using the default value of 100 points in the linspace function, increase it to 1000 or more as necessary to plot the tangents correctly:
v = linspace(a, b, 1000);
The calculated values of the points on the circle where the tangent lines touch the circle are correct.
Ender Rencuzogullari
on 5 Dec 2015
Edited: Ender Rencuzogullari
on 5 Dec 2015
Star Strider
on 5 Dec 2015
As always, my pleasure!
Walter Roberson
on 6 Dec 2015
Please post a summary Answer and accept it.
Answers (1)
Star Strider
on 6 Dec 2015
The problem began with a previous Question (reference: how to plot tangent line from specified point to a circle ?) and the current problem arose when the point from which tangents were to be drawn was defined to be on the circle circumference, from which no tangents to the circle could be drawn.
This discussion spanned the other thread as well, in which I suggested one possible solution, that being to subtract a small amount from the radius of the circle, or alternatively add a small amount to the distance of the point from the circle. That seems to have worked, and the points on the circle where the tangent lines intercepted the circle were calculated and plotted correctly, although the resolution of the circle and other vectors did not give sufficient resolution with a point at such close proximity to the circumference of the circle to plot the tangent lines themselves correctly. My reply was:
‘That is correct. If you want to see the tangent lines touch the circle from a point that close, you have to increase the resolution of your data calculations. For instance, instead of using the default value of 100 points in the linspace function, increase it to 1000 or more as necessary to plot the tangents correctly:
v = linspace(a, b, 1000);
The calculated values of the points on the circle where the tangent lines touch the circle are correct.’
Ender Rencuzogullari replied:
‘Thank you so much. You are the best, sir ! Especially,
rr= rr - sqrt(eps);
solved my problem. the All my problems and answers which I am looking for long time are done now, under your profession.’
To which I replied ‘My pleasure’ and considered the problem resolved.
1 Comment
Star Strider
on 6 Dec 2015
@Walter — Please edit my Answer for content and clarity as necssary. I have been too close to it too long to be able to do that efficiently just now.
Categories
Find more on Logical in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
