Skip to content

Commit

Permalink
Improve map() function
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulStoffregen committed Oct 22, 2020
1 parent 0bd7c42 commit 92b126d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
26 changes: 23 additions & 3 deletions teensy3/wiring.h
Expand Up @@ -49,15 +49,35 @@ long map(T _x, A _in_min, B _in_max, C _out_min, D _out_max, typename std::enabl
{
long x = _x, in_min = _in_min, in_max = _in_max, out_min = _out_min, out_max = _out_max;
// Arduino's traditional algorithm
//return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
#if 0
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
#endif
#if 0
// st42's suggestion: https://github.com/arduino/Arduino/issues/2466#issuecomment-69873889
// more conversation:
// https://forum.pjrc.com/threads/44503-map()-function-improvements
if ((in_max - in_min) > (out_max - out_min)) {
return (x - in_min) * (out_max - out_min+1) / (in_max - in_min+1) + out_min;
} else {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
#endif
long in_range = in_max - in_min;
long out_range = out_max - out_min;
if (in_range == 0) return out_min + out_range / 2;
long num = (x - in_min) * out_range;
if (out_range >= 0) {
num += in_range / 2;
} else {
num -= in_range / 2;
}
long result = num / in_range + out_min;
if (out_range >= 0) {
if (in_range * num < 0) return result - 1;
} else {
if (in_range * num >= 0) return result + 1;
}
return result;
// more conversation:
// https://forum.pjrc.com/threads/44503-map()-function-improvements
}
// when the input is a float or double, do all math using the input's type
template <class T, class A, class B, class C, class D>
Expand Down
26 changes: 23 additions & 3 deletions teensy4/wiring.h
Expand Up @@ -49,15 +49,35 @@ long map(T _x, A _in_min, B _in_max, C _out_min, D _out_max, typename std::enabl
{
long x = _x, in_min = _in_min, in_max = _in_max, out_min = _out_min, out_max = _out_max;
// Arduino's traditional algorithm
//return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
#if 0
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
#endif
#if 0
// st42's suggestion: https://github.com/arduino/Arduino/issues/2466#issuecomment-69873889
// more conversation:
// https://forum.pjrc.com/threads/44503-map()-function-improvements
if ((in_max - in_min) > (out_max - out_min)) {
return (x - in_min) * (out_max - out_min+1) / (in_max - in_min+1) + out_min;
} else {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
#endif
long in_range = in_max - in_min;
long out_range = out_max - out_min;
if (in_range == 0) return out_min + out_range / 2;
long num = (x - in_min) * out_range;
if (out_range >= 0) {
num += in_range / 2;
} else {
num -= in_range / 2;
}
long result = num / in_range + out_min;
if (out_range >= 0) {
if (in_range * num < 0) return result - 1;
} else {
if (in_range * num >= 0) return result + 1;
}
return result;
// more conversation:
// https://forum.pjrc.com/threads/44503-map()-function-improvements
}
// when the input is a float or double, do all math using the input's type
template <class T, class A, class B, class C, class D>
Expand Down

0 comments on commit 92b126d

Please sign in to comment.