arduino -map function for fractional values

Thread Starter

Dabu WhiteHacker

Joined Sep 5, 2017
68
i was wondering what will happen if the map function is given a value of fraction to be mapped.
for example i have a code like this
sensor_value = map(sensor_value, 80, 20, 10, 1);
here sensor value could be in fraction like 50.23
what will happen to the output of this function ?
and also what will happen if sensor_value does not come within 80 and 20
 

AlbertHall

Joined Jun 4, 2014
12,346
From the arduino reference for the map function:

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

So the values into and out of the function are long integers and fractional values are not going to work.
I don't know whether using a floating point value will produce an error or the value may be coerced to an integer value.
 

MrChips

Joined Oct 2, 2009
30,806
Why would anyone want to use a map( ) library function?
Is this simply an evaluation to a straight line fit?
If you want floating point results (and it is imperative to have floating point results) then use floating point math.

I am obviously not an Arduino user and perhaps this demonstrates why some people continue to use Arduino libraries.
 
i was wondering what will happen if the map function is given a value of fraction to be mapped.
for example i have a code like this
sensor_value = map(sensor_value, 80, 20, 10, 1);
here sensor value could be in fraction like 50.23
what will happen to the output of this function ?
and also what will happen if sensor_value does not come within 80 and 20
It is a good question and good to ask before you use that function. I have not tested all possible outcomes, but I think it might be a good idea to cast the values, including all arguments, to type long and note also that a long type is returned (as @AlbertHall has already stated,

You can read about some of these issues here and here.

Hope it helps.
 
Top