# Mathf.Approximately with a threshold ?

Hey guys, Is there any function that does like Mathf.Approximately but that I can pass it a threshold, because my value differs for like 0.01 or so and approximately returns false because it must be a smaller difference than that, but I wonder if there is any other function in that already exists that could do thing that I wanna do without writing it manually.

Thanks a lot for your time

Claude

Although @robertbuâ€™s answer is perfect, hereâ€™s in the form of a method you could use anywhere. Itâ€™s also a little bit faster than using Mathf.Abs()
(Code is in C#)

``````public static bool FastApproximately(float a, float b, float threshold)
{
return ((a - b) < 0 ? ((a - b) * -1) : (a - b)) <= threshold;
}
``````

``````public static bool FastApproximately(float a, float b, float threshold)
{
if (threshold > 0f)
{
return Mathf.Abs(a- b) <= m_Threshold;
}
else
{
return Mathf.Approximately(a, b);
}
}
``````

Is much more readable than the ternary operator used in other answer. Optimizing to this point is counter productive.
Also, in case threshold equals 0, you must use float.Approximately to avoid rounding error.