tan 45 gives me 0.9999

前端 未结 4 668
清酒与你
清酒与你 2021-01-12 08:30

Why does tan 45(0.7853981633974483 in radian) give me 0.9999? What\'s wrong with the following code?

System.out.println(Math.tan(M         


        
4条回答
  •  广开言路
    2021-01-12 09:03

    Floating point calculations will often lead to such inaccuracies. The problem is that numbers cannot be accurately represented within a fixed number of bits.

    To give you another example (in decimal), we all agree that 3 * (1/3) = 1. However, if your calculator only has 4 decimal places, 1/3 would be represented as 0.3333. When that's multiplied with 3, you would get 0.9999 not 1.

    As further information, floating points on most systems are usually represented using the IEEE754 standard. You could search for it, or refer the Wikipedia page for more details. IEEE floating point

提交回复
热议问题