J-M-L:
You did sorry got confused with Krupski code based on doubles which is slower than integer mapping.Your function works as most people would expect map to work - agree with that but still view that the doc by mentioning truncating rather than rounding is clear to me (hence why I don't use it
and do the math manually)
I would say the biggest issue with the Arduino map() function is the Arduino web page documentation for map() since I don't believe the documentation on that page is clear on how the mapping is done.
With the exception of the actual code of the map() function being provided - which I'd say most arduino users won't understand or at least not the subtly of how it works -
there are only two sentences that describes how the actual mapping or mapping calculation is done and those two sentences are so vague that it isn't clear what to expect.
Consider only the two descriptive sentences without the benefit of seeing or knowing the code implementation as any good explanation of an API function should not require knowledge of the specific code implementation.
The map() function uses integer math so will not generate fractions, when the math might indicate that it should do so. Fractional remainders are truncated, and are not rounded or averaged.
The first line:
The map() function uses integer math so will not generate fractions, when the math might indicate that it should do so.
That statement is terrible in several ways.
First fractions is not a good term to use since computers actually use floating point representation not actual fractions.
I'm guessing the intent of will not generate fractions is that it only returns integers and not floating point values that can specify values that are in between integer values.
But the second part is where the real ambiguity is:
What does when the math might indicate that it should do so mean?
Any math involved is in the code implementation and how could the caller indicate using fractions (floating point)?
Is it trying to say that the all the parameters passed to map() will be converted to integers?
It simply isn't clear what that was trying say.
The second line:
Fractional remainders are truncated, and are not rounded or averaged.
This is not clear and does not help explain how the mapping is done.
Since the description of how the mapping is done as well as the main description of the map() function:
Re-maps a number from one range to another. That is, a value of fromLow would get mapped to toLow, a value of fromHigh to toHigh, values in-between to values in-between, etc.
are so vague,
the alternate map() function I presented would also conform to the current documentation and work with the example.
And THAT is the problem. The documentation is not clear how map() works.
Adding to the confusion is the example provided which is not a good example since it does not help explain how the mapping function does the mapping.
Given the minimal explanation and the example, it isn't obvious that the maximum PWM value (255) is not achieved until the maximum analogRead value of 1023 is achieved.
What probably needs to happen is an update to the map() documentation page to really explain how it works and to show two examples, that shows how to use the function for two ways of mapping.
- truncating target values as in the "birthday" example.
- selecting target values from evenly distributed regions which many people seem to want.
as either way of mapping can be accommodated with the current map() code by fudging the "High" range parameters.
I believe this would solve all the confusion as well as show people how to get the type of mapping they want.
This would be a very good thing.
--- bill