Raycast is not returning exact number / Mesh is not exact dimensions

I am trying to select faces of a cube with raycasting. I converted the RaycastHit.point to a local vector relative the transform of the collider hit. The box collider I created is exactly 0.2 units. Therefore when I got my RaycastHit.point and I converted it to local coordinates, I tried to use the Vector3 to select the face. So for example the vector (0,0.1,0) would be the top of the cube. The problem is that when I log the vector axes, the value that should be 0.1 (The face) is actually logged as 0.0999999. I can’t even use this number to choose the faces because it is infinitely repeating and I have no idea how to solve this problem. Any advice would be greatly appreciated.

So this is actually due to how floating point numbers are handled in computers (not Unity specifically). Floating point numbers are expressed as powers of 2.

0.125 is a power of 2 (2^-3) but 0.1 is not and can therefor not be perfectly represented with a float (or double for that matter). This is why you should never check equality between floating point numbers. They just can’t be represented in the number of decimal places you have.

That being said, you can use Mathf.Approximately to check if floating point numbers are “close enough” to one another.

Well it seems the number is close enough each time so just round it:

double num =  Math.Round (yourNum, 1); // Rounds to first decimal