Go Down

Topic: map() function equation wrong (Read 5662 times) previous topic - next topic

st42

I know the map() function has been discussed before and I have read the previous posts but no one has stated the obvious problem. The equation in the function stated at http://arduino.cc/en/Reference/Map

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

is wrong. The example given:

val = map(val, 0, 1023, 0, 255);

In this example 0-1023 is mapped to 0-255. The equation gets it wrong by mapping 1023 numbers to 255 numbers, when it should be mapping 1024 numbers to 256 numbers.

0 through 1023 is 1024 numbers not 1023 : in_max - in_min : 1023-0=1023
0 through 255 is 256 numbers not 255 : out_max - out_min : 255-0=255

The corrected equation in the function should be:

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

In the following simpler example it will be easier to see:

val = map(val, 0, 3, 0, 1);

In this example 0,1,2 and 3 is mapped to 0 and 1. Mapped correctly it should be:

0 maps to 0
1 maps to 0
2 maps to 1
3 maps to 1

Using the equation in the the function as it is now, it is mapped as such:


0 maps to 0
1 maps to 0
2 maps to 0
3 maps to 1

This is a common problem programmers have dealt with before. When dealing with arrays you can not determine array size by simply subtracting first position from last position you will always be off by 1. That is why you add 1 to get proper size. I know this is for the most part a minor problem, but if your project requires accurate results this could be a serious issue.

Coding Badly

Quote
....but no one has stated the obvious problem.
I have.


st42

Coding Badly, I went back and read your post. You did present the correct solution but unfortunately you presented it as just the way you've always done it. I've learned the hard way over the years that unless you specifically state something is wrong, why it is wrong and supply the correct solution it will never get changed. I can present a hundred alternative ways of doing something but it will not be taken seriously if I present it as opinion. No one cares about opinion, just facts.

robtillaart

If you want this changed in the base code, please post an issue here - https://github.com/arduino/Arduino -


IIRC The map function used to be a #define and if used for float type variable it was correct the way it is formulated, however for integer types it is (slightly) incorrect.
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

st42

robtillaart, I posted it there right after I posted it here. I realized after I posted it here that it should have been posted there. I never intended to have a discussion about it, I just wanted it changed. Not that it impacts me any but I hate to see bad code go unchanged. Ive been a programmer for... well to give you an idea of how long, I started programming computers for the military with punchcards. Ill let you do the math. Any way whenever I see problems like this I go out of my way to have them fixed. Because I've seen small bits of bad code like this snowball into major problems.

robtillaart

Thanks for posting the issue on github.
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up