0

I have tried these options

System.out.println(sin(Math.toRadians(180.0)))
System.out.println(sin(Math.toDegrees(180.0)))
System.out.println(sin(180.0))

But they gave different answers

1.2246467991473532E-16
0.5715301650260188
-0.8011526357338304

I understand that I did not understand something, but there was nothing on the Internet that could help me.

TenolToSy
  • 3
  • 1
  • 3
  • 2
    `Math.sin(Math.toRadians(180.0))` is correct (same as `Math.sin(Math.PI)`) – Progman Jun 24 '23 at 10:50
  • `sin(Math.toDegrees(180.0))` makes zero sense, it convert 180 radian to degrees, and use that value as radian to pass into `sin`. And there's no way to make `sin(180°) = 0` because sin always works in radian, and there's no way to get exactly pi in floating point. The only way to overcome this is to find some sind method that works directly in degree – phuclv Jun 24 '23 at 10:52
  • _there was nothing on the Internet that could help me_ Did you try the [documentation](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Math.html#sin(double))? It clearly states that the [method] parameter is _an angle, in radians_. – Abra Jun 24 '23 at 10:56
  • 2
    Note that this value (1.2246467991473532E-16, the other values are meaningless) corresponds to `pi - Math.PI` (where `pi` is the *actual* mathematical value of pi, and `Math.PI` is pi rounded to the nearest `double`). That's not an accident and this isn't just some arbitrary floating point nonsense, it's due to the sine working exactly as well as it possibly good. – harold Jun 24 '23 at 10:58
  • Let me reword that, `assertAlmostEqual(Math.sin(Math.PI), 0)`, eeh, close enough. – Lie Ryan Jun 24 '23 at 11:12
  • Thanks, now I understand how important accuracy is in Pi, I think for the calculator you need to use the prepared options then – TenolToSy Jun 24 '23 at 15:19

0 Answers0