# What exactly does Mathf.InverseLerp() do?

Example `Mathf.InverseLerp(5.0f, 10.0f, 8.0f)`

this return 0.6, whats mean this value returned?

Unity doc say me that this value is inear parameter t that produces the interpolant value within the range [a, b]. What does that mean?

**Answer** by orionsyndrome
·
Feb 03, 2019 at 07:47 PM

If you need to find how far the value is in respect with two fixed values (obviously as a ratio, which can be thought of as a percentage), you need a Lerp, which is short for 'linear interpolation'.

Simply put, if you think about (i.e.) point1 at 0% and some other point2 at 100%, there is a simple way to find any other point in between these two if you imagine a line between them.

If you think about it, you can express all points on this line as *point1 + offset*. If your offset is 0, then *point1 + 0* and you're exactly at point1. If your offset is equal to difference, where *difference = (point2 - point1)*, then you arrive at point2 (because *
point1
+ (point2 -
point1
)*). Conveniently, you can introduce a multiplier to this difference, and so you come up with a formula

*point1 + factor x (point2 - point1)*where a value of 0 gives you point1, a value of 1 gives you point2, and 0.5 (or 50%), gives you exactly the midpoint between the two.

This is what Lerp does.

However, sometimes we need to do the opposite, we need that multiplier when we only had a concrete value. For this we solve this equation backwards.

```
value = point1 + factor x (point2 - point1), where value is known, but the factor is unknown
factor x (point2 - point1) = value - point1, now we divide the whole thing by (point2 - point1)
factor = (value - point1) / (point2 - point1)
```

And this is what InverseLerp does.

Oh btw because of this division, you might want to make sure the denominator isn't zero (if for some reason you're implementing this math on your own).

If it's zero, that means the points coincide in space, and thus the line cannot be defined properly.. Therefore, we need a check: if(Mathf.Abs(point2 - point1) < epsilon) return point1;

This check is due to floating point precision errors, as you cannot simply check for zero and be absolutely sure that the difference will be exactly zero.

Epsilon is typically a very small value in the order of 1E-5 (that's 10^-5). Or, if you're working with Vector3, you could use the provided kEpsilon.

With this line added, this is exactly what InverseLerp does for you.

```
float InverseLerp(float min, float max, float value) {
if(Mathf.Abs(max - min) < epsilon) return min;
return (value - min) / (max - min);
}
```

On the other hand, if the points do coincide, for your implementation you might decide that the result is undefined instead, and throw an error such as ArgumentException (which is a more robust way to handle this in the more general scenario).

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.