- Home /
How to compare float for equality?
How to compare float for equality? I try
hit:RaycastHit;
if (hit.transform.position.x - hit.point.x)== 0.5 {/todo/}
And some times it does not working...
Answer by Eric5h5 · Feb 22, 2011 at 09:52 AM
It's unlikely that floats will be exactly equal, unless they are explicitly set that way. Use a range, or <= and >=, depending on what you want to do.
How to compare float == float.... or Vector3.x with another Vector3.x for ==.
Range is the only one way?
You use ==, however because of floating point imprecision and other factors, two floats are unlikely to be exactly equal, as I said. $$anonymous$$athf.Approximately is often not approximate enough, so I'd suggest a larger range or another solution.
If approximately isn't a wide enough range you can always do $$anonymous$$athf.Abs(x1-x2)< whatever you want
Your answer
![](https://koobas.hobune.stream/wayback/20220613105645im_/https://answers.unity.com/themes/thub/images/avi.jpg)