hello
sin 90 and 270 deg = must be 1 and -1
sin 180 and 0 = 0
but ath mathf for 180deg you cant get 0 value
i try many ways but at 180 deg i get bad value
for example Mathf.Sin(180 * Mathf.Deg2Rad) but again i see not true value
2 Answers
2I tried this JavaScript:
function Start () {
Debug.Log(Mathf.Sin(180f * Mathf.Deg2Rad));
}
And this C#:
void Start () {
Debug.Log(Mathf.Sin(180f * Mathf.Deg2Rad));
}
In both cases, the number logged is -8.742278E-08, or roughly -0.00000009. I get the same result if I pass Mathf.PI.
It’s true that is not exactly zero, but one limitation of floating point math is that its accuracy is limited.
When performing equality checks with floating point numbers, one should rarely use strict == operators (some programming languages will even prevent you from doing so); you can instead check for approximate equality, within some tolerance that will depend on your particular needs.
oh thanks you say right there is floating point at returnet value
Indeed the Mathf library contains the Approximately function for exactly this circumstance.
– whydoidoitInteresting note: in this specific example, comparing the result above with 0f using Mathf's Approximately will return false. I think it has a very tight tolerance?
– rutter