Saturday, May 21, 2016

98: C++ "Feature"

See http://dicks-photon-arduino.blogspot.com/, post 40. The useful map() function accepts float arguments without complaint but truncates them to ints. Return values can be wildly inaccurate. This may be a feature of all C++ compilers.

IMHO, map() should be coded like this:

float map(float value, float istart, float istop, float ostart, float ostop) {
    return ostart + (ostop - ostart) * ((value - istart) / (istop - istart));
}

No comments:

Post a Comment