-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add support for different types in map() function #447
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CLA assistant check
All committers have signed the CLA.
|
Memory usage change @ 8ec190d
Click for full report table
Click for full report CSV |
oqibidipo
commented
Apr 9, 2022
It is not that simple. On AVR this will easily give incorrect results due to 16-bit int overflow.
A common example: scale ADC value to 0–100.
template <typename T>
T tmap(T x, T in_min, T in_max, T out_min, T out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
void setup() {
Serial.begin(115200);
for (int i = 0; i < 1024; i += 33) {
int x1 = map(i, 0, 1023, 0, 100);
int x2 = tmap(i, 0, 1023, 0, 100);
Serial.print(i);
Serial.print('\t');
Serial.print(x1);
Serial.print('\t');
Serial.println(x2);
}
}
void loop() { }
Output
0 0 0
33 3 3
66 6 6
99 9 9
132 12 12
165 16 16
198 19 19
231 22 22
264 25 25
297 29 29
330 32 -31
363 35 -28
396 38 -25
429 41 -22
462 45 -18
495 48 -15
528 51 -12
561 54 -9
594 58 -5
627 61 -2
660 64 0
693 67 3
726 70 6
759 74 10
792 77 13
825 80 16
858 83 19
891 87 23
924 90 26
957 93 29
990 96 -31
1023 100 -28
Change type of i to unsigned int
sketch.ino: In function 'void setup()':
sketch.ino:11:37: error: no matching function for call to 'tmap(unsigned int&, int, int, int, int)'
int x2 = tmap(i, 0, 1023, 0, 100);
^
sketch.ino:2:3: note: candidate: template<class T> T tmap(T, T, T, T, T)
T tmap(T x, T in_min, T in_max, T out_min, T out_max)
^~~~
sketch.ino:2:3: note: template argument deduction/substitution failed:
sketch.ino:11:37: note: deduced conflicting types for parameter 'T' ('unsigned int' and 'int')
int x2 = tmap(i, 0, 1023, 0, 100);
^
Error during build: exit status 1
Let's try
int x2 = tmap(i, 0u, 1023u, 0u, 100u);
Nope.
0 0 0
33 3 3
66 6 6
99 9 9
132 12 12
165 16 16
198 19 19
231 22 22
264 25 25
297 29 29
330 32 32
363 35 35
396 38 38
429 41 41
462 45 45
495 48 48
528 51 51
561 54 54
594 58 58
627 61 61
660 64 0
693 67 3
726 70 6
759 74 10
792 77 13
825 80 16
858 83 19
891 87 23
924 90 26
957 93 29
990 96 32
1023 100 35
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I have seen how many other PR's and issues are related to the map() function, but well, hope it is of any use.
In any case, it should be backwards compatible with any C++ project out there.