Why is the map() function integer based?

Just out of curiosity, why is the map function designed for integer remapping Is it for performance reasons? Was wondering why my code wasn’t working till I read the reference above :smiley: In anycase, I have a workaround in place. Sorta.


The majority of applications that use map() are integer based. And as you point out, for performance reasons.

The function on the reference page can be easily converted to float simple by changing all longs to float (rename the function to fmap() to prevent name conflict.

float fmap(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; }

If you know more about the range of the datatypes you might consider using unsigned long or if the ranges are small you might even consider changing the datatype to (unsigned) integer. Be aware that internally the math can overflow if you use smaller datatypes, or use large longs. - http://www.arduino.cc/en/Reference/Map

On the playground several variations of the map function are discussed, see section - http://arduino.cc/playground/Main/GeneralCodeLibrary#Math_code

Forummembers who showed interest in the map() function often also like the constrain() function ;) - http://www.arduino.cc/en/Reference/Constrain