问题描述:

I have:

`double num1 = sc.nextInt();`

I used:

`double sin = Math.sin(Math.toRadians(num1));`

and made the output:

`if(calc.contains("sin")) {`

System.out.println (sin);

}

if i typed in for num1:

30 and it calculates sin. It gives me `0.49999999999999994`

π/6 cannot be represented exactly in a computer (it can't be represented exactly on paper, either). This will cause the result to be a little bit off too. Using "bc", a Unix high-precision calculator (note: I don't know just how accurate it is for sine and cosine), I find that the actual value in your program will be π/6 + ε where ε is about 5.3604 x 10^{-17}. Using the formula for sin(*x*+*y*), the expected result should be sin π/6 cos ε + sin ε cos π/6. cos ε is about 1 - 10^{-33}, so this difference won't be enough to affect the result. However, sin ε cos π/6 is about 4.64 x 10^{-17}. So the actual result should be something like 0.4999999999999999535774978 instead of 0.5.

This result won't be represented exactly in a `double`

, either. Because a `double`

has a mantissa of 52 bits, numbers whose values are >= 0.25 and < 0.5 could be represented by numbers that are off by as much as 2^{-54}. The `double`

used to represent this result would be 0.499999999999999944488848768742172978818416595458984375. When this is printed with `System.out.println`

, it will stop after a certain number of decimal places are printed, so this gets truncated to 0.49999999999999994, which is the result you're seeing. (The number of digits displayed is discussed in [http://docs.oracle.com/javase/8/docs/api/java/lang/Double.html#toString-double-](this javadoc).

sin(30°) is surely 0.5, but there is not a dictionary that contains a key of `30°`

with value `0.5`

, so computer need to calculate it.

The formula is showing above.

Let's calculate sin(30°), 30° = π/6, so f(x)= π/6 - π^3/1296 - π^5/933120 - ....

And then in this process, accuracy error can lead to "unpredictable"(actually predictable) problems.