Do float values from Mathf.Clamp() play nicely with equality operators?

In general, I understand the issue with float values being unreliable for use with equality operators due to lack of precision and tolerance. My confusion is around if you clamp a variable to a specific float value, could that be used to circumvent the issue and allow the code to reliably function? Will Mathf.Clamp() always clamp to exactly to its specified min and max values?

For example, could the following code be considered reliable?

Thanks!

float f = 0.0f;
while(f != 1.0f)
{
    f = Mathf.Clamp(f + 0.13, 0.0f, 1.0f);
}

You can use Mathf.Approximately(float a, float b); to do exactly what you want :slight_smile:

edit: To also directly answer your question, yes it should work with clamping too, but you are really looking for the approximate method I believe.

Mathf.Clamp is literally implemented like this:

public static float Clamp(float value, float min, float max)
{
	if (value < min)
	{
		value = min;
	}
	else
	{
		if (value > max)
		{
			value = max;
		}
	}
	return value;
}

So yes, when that method actually clamps the value to one of the provided limits min or max, it will return the passed limit without any additional calculations.