map () and Constraint()

Hi all, i have a question about the map function; why does it not map somethings? In the arduino reference it says

Does not constrain values to within the range, because out-of-range values are sometimes intended and useful. The constrain() function may be used either before or after this function, if limits to the ranges are desired.

Is it random?( does it randomly not map certain values)?
In essence my question is what is the point of having a separate map and constrain function?
Maybe i do not understand exactly what they do; please explain.

The map function just does some simple (linear) algebra:

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;
}

It doesn’t compare the values of x and in_min or in_max. The constrain function just returns a value that’s always between the min and the max:

#define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))

jonathanped:
Hi all, i have a question about the map function; why does it not map somethings? In the arduino reference it says

Does not constrain values to within the range, because out-of-range values are sometimes intended and useful. The constrain() function may be used either before or after this function, if limits to the ranges are desired.

Is it random?( does it randomly not map certain values)?
In essence my question is what is the point of having a separate map and constrain function?
Maybe i do not understand exactly what they do; please explain.

Well I understand the benifit of seperate functions, but damn if I can express it well enough to myself let alone someone else. :wink:

By allowing the mapping function to work beyond the limits of the input to output points used in the function it allows the resulting linear curve to be extended in both directions if the input variable is outside those defined input points, that can be useful. Also a separate constrain function is general purpose and can and is used for many things other then with just the mapping function.

So how did I do? :wink:

Lefty

Lefty, that was a right explanation :wink: