# Windows Calculator Showing Strange Output

Try this on Windows

Steps:

1. Open Calculator directly or by typing calc in RUN.
2. Then type 4 and take square root "sqrt"
3. And whatever you get(ideally you will get 2!), substract 2 from it.

Now My Question is, is this mathematically right ??

-

## migrated from stackoverflow.comNov 9 '10 at 10:46

This question came from our site for professional and enthusiast programmers.

possible duplicate of Do-s and Don't-s for floating point arithmetic? – Gabe Nov 9 '10 at 7:18
How? Didnt get you. – Anonymous Nov 9 '10 at 7:22
@Infinite: That is very wrong. The sqrt: (R \ R^-) -> (R \ R^-) function is a function, that is, a rule, that to every element in the domain associates exactly one element in the range. sqrt(4) is 2, and nothing else. However, the equation x^2 = 4 has exactly two roots, -2 and 2, that is, +sqrt(4) and -sqrt(4). – Andreas Rejbrand Dec 13 '10 at 11:30
@Infinite: Yes, which makes it even worse (and I am a mathematician). (Well, in complex analysis, one sometimes consider multiple-valued functions, but that is far beyond this discussion. In elementary mathematics (and almost all applied math), sqrt is equal to the positive root of the corresponding quadratic equation.) – Andreas Rejbrand Dec 13 '10 at 11:35

## 3 Answers

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.

-
Ok Thanks. But Why such bug occur and how to avoid such errors while making computations in any or specific programming languages.What care is needed to be taken. – Anonymous Nov 9 '10 at 7:26
Unless you're going to display numbers using scientific notation, like a calculator, you probably wouldn't notice an error this small when writing code. But it's one reason you should never do range checks like `while (x != 0.0) x += floatingPointResult();` rather than something like `while(x <= 0.0)`. – Mud Nov 9 '10 at 7:44
Umm... I thought the Windows calculator used 256-bit fixed point numbers since Vista. – Mircea Chirea Dec 13 '10 at 13:24
@iconiK I don't know what this has to do with anything. Regardless of the number's precision, there are always rounding errors when performing floating point arithmetic (especially on irrational numbers). – Breakthrough Mar 12 '11 at 16:25
@Breakthough, fixed point != floating point. Fixed point is exact arithmetic, and with 256 bits worth of data it's highly unlikely you'll overflow that; even if you do, it probably rounds to the nearest 256-bit value - which is a really long list of digits. – Mircea Chirea Mar 12 '11 at 16:53

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.

-
I got exactly 0 for the pi thing... – syockit Jan 19 '14 at 7:37

it is giving -8.1648465955514287168521180122928e-39

which is basically = 0 as it have 10^-39...

-
-1. That is very wrong. The sqrt: (R \ R^-) -> (R \ R^-) function is a function, that is, a rule, that to every element in the domain associates exactly one element in the range. sqrt(4) is 2, and nothing else. However, the equation x^2 = 4 has exactly two roots, -2 and 2, that is, +sqrt(4) and -sqrt(4). – Andreas Rejbrand Dec 13 '10 at 11:32
What does the fact that `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. – Cody Gray Dec 13 '10 at 11:32
Besides, the fact that x^2 = c has two roots has absolutely nothing to do with the issue at hand... – Andreas Rejbrand Dec 13 '10 at 11:33
@Code Gray: Very good remark, except for the fact that sqrt(x) is ONE value, not two! – Andreas Rejbrand Dec 13 '10 at 11:34
@Andreas: Yeah, I saw that you had just commented that. I knew it sounded wrong to me, but I couldn't articulate it nearly as well as you did. I was more interested in how it was relevant to the answer. – Cody Gray Dec 13 '10 at 11:36