- Home /
mathf.sin((Mathf.PI / 180) * 180) get 0.8966..
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
Answer by rutter · Nov 14, 2013 at 08:22 AM
I 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.
Indeed the $$anonymous$$athf library contains the Approximately function for exactly this circumstance.
Interesting note: in this specific example, comparing the result above with 0f using $$anonymous$$athf's Approximately will return false. I think it has a very tight tolerance?
Answer by ehsan_wwe1 · Nov 14, 2013 at 08:25 AM
oh thanks you say right there is floating point at returnet value
Your answer
Follow this Question
Related Questions
radiusX and radiusY ? 1 Answer
Distance in a Trajectory of a projectile 0 Answers
Is there a Magic Code for getting delta values? 1 Answer
How can I make water waves ? 1 Answer
How do I get the 'normal' Sinus? 1 Answer