π/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).