Try this on Windows
Steps:
- Open Calculator directly or by typing calc in RUN.
- Then type 4 and take square root "sqrt"
- And whatever you get(ideally you will get 2!), substract 2 from it.
Now My Question is, is this mathematically right ??
Try this on Windows
Steps:
Now My Question is, is this mathematically right ??
Ideally not right, but the way floating point number are represented inside computer would result in such unexpected answer.
If you look into the numerical value of the answer, it is very close to 0, that is the expected answer.
while (x != 0.0) x += floatingPointResult();
rather than something like while(x <= 0.0)
.
No, it's not mathematically right. 2-2 = 0 :)
Calc is giving is -0.0000000000000000001, which means the sqrt function yielded a number very close to 2, but which had some tiny fractional portion rounded off for display purposes. When you subtract 2, you get a number which is very close to 0, but not quite.
It's an artifact of how floating point numbers are stored in a computer. Every (non symbolic) calculator will have some problems that produce incorrect results like that. For instance, try (sqrt(pi))^2-pi, which should be 0. I just tried it in this online calculator and got -1.3691388027.
it is giving -8.1648465955514287168521180122928e-39
which is basically = 0 as it have 10^-39...
sqrt(4)
= +2 and -2 have to do with the inaccuracy of floating point arithmetic? That the answer could also be -2 doesn't explain why the answer isn't exactly 0. I don't understand the relevance of the last part to your answer.
Commented
Dec 13, 2010 at 11:32