I'm puzzled with the results of the map function:
uint16_t result = map(46340, 0, 46340, 0, 46340);
returns 46340 as expected
but...
uint16_t result = map(46341, 0, 46341, 0, 46341);
returns 19196
can someone explain what's going on?
I'm puzzled with the results of the map function:
uint16_t result = map(46340, 0, 46340, 0, 46340);
returns 46340 as expected
but...
uint16_t result = map(46341, 0, 46341, 0, 46341);
returns 19196
can someone explain what's going on?
map() returns a long integer, with maximum value 2,147,483,647.
jremington:
map() returns a long integer.
long result = map(46341, 0, 46341, 0, 46341);
returns -46340
it seems there's some kind of overflow there
What is the value of 463412?
thanks, I'll just use my custom map function:
uint32_t mymap(uint32_t x, uint32_t in_min, uint32_t in_max, uint32_t out_min, uint32_t out_max) {
return (x - in_min)*(out_max - out_min)/(in_max - in_min) + out_min;
}
mymap(46341, 0, 46341, 0, 46341); // returns 46341
it seems to work with unsigned args
Floats would work, and handle negative values as well.
I have never found the Arduino map function to be useful.
thanks for the help!
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;
}
Since in_min and out_min are both zero, the numerator is "x * out_max". That overflows the 'long' when both are 46341.
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.