Float calculation for beginner

for the uv sensor, i need to use a libary, which has a calculation included
can someone explain, what the float calculation do?
They use this calculating:

float mapfloat(float x, float in_min, float in_max, float out_min, float out_max) {
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

this are the calculating values

mapfloat(outputVoltage, 0.99, 2.9, 0.0, 15.0);

can someone tell me, what this do?
i write only in php. c++ is not my favourite language.
thank you a lot!

That function acts like the map function, but for float variables, rather than integers.

Do you not understand what this equation calculates as "result"?

result = (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;

It "maps" values to a different range and offset in a linear way.

Suppose an analog channel has a value of 0...1023, and that indicates a pressure of 10...100.

So you get:

0 -> 10
1023 -> 100 
Everything in between should be in a linear way.

The input range is 1023 (from 0 to 1023).
The output range is 90 (from 10 to 100).
The input offset is 0 and the output offset is 10.

The most important is the range. The 1023 should be mapped onto the 90. That is done by dividing with 1023 and multiplying with 90.

Add the offset and you get:

int value = analogRead(A0);
pressure = float(value) / 1023 * 90 + 10;

The range for the input is (in_max - in_min).
The offset for the input is in_min.
The range for the output is (out_max - out_min).
The offset for the output is out_min.

Put the division with the range in a function, add the input offset and output offset, and then you get that calculation.

Can you see the overall intention ? It is about the range. The offsets are solved before and after mapping the ranges onto each other.

i see.
I can copy multiple values with a linear range, to a new range.

modify 1 to 100
to a new range like 20 to 50

Yes, that's all.

There is even a different way to look at it.

Suppose that you have a voltage of 0...5V and you need to convert that to a speed of 2...-10 m/s.
That means you have to get rid of the unit "voltage". That is done by dividing with the voltage range (which is 5). After that there is a universal value of 0...1 that does not have a unit.
Then you have to introduce the unit "speed". That is done by multiplying with the speed range (which is -12).

Solve the offsets before and after as usual.

it's a great function!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.