How can I increase the precision of a variable after decimal?

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

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.
I probably explained my question wrong. if I had input or define the value of c and r as you wrote above, It would be okay and I could take the correct result. However, the values of c and r are coming from their formula while the program is running. I do not define the values. I may not know something about this topic in matlab and I could not express my question correctly.
the actual value of c is xx.0000xxxx. but when computes and saves the equation of c=hypot(X(1) Y(1)), the program is just taking the c as xx.0000
I need c as xx.0000xxxx . I hope I could express good enough.
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!
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.
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.
format long
c
rr
d = c - rr
c =
68.750000000000000
rr =
68.750000000000000
d =
0
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?
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.)
yes sir, starstrider, I have lots of thing to learn
Dear John D'errico,
I don't know why your attitude is tough. I am trying to learn something. I am not saying Matlab is doing wrong. I am doing wrong something that is why I am here. You probably did not understand my question. I told that the real result of c is not above I wrote as a comment. Matlab takes the result as above. I am trying to figure out why. I am doing wrong something not matlab. may be I asked idiotic question. Otherwise, How can I learn ..
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.
yes sir, I actually checked before. I found the problem. the problem is me. Sorry about my confusion and mistakes. May I ask you an another different question? there exists a tangent line from a point to a circle. this point is too close to circle such as 0.013 micron. And the when I zoom the image, I see there a gap between line and circle. Is it okay?
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.
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.
Please post a summary Answer and accept it.

Sign in to comment.

Answers (1)

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

@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.

Sign in to comment.

Products

Commented:

on 6 Dec 2015

Community Treasure Hunt

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

Start Hunting!