Is there a function like Arduino's map in C#?
How do you map a value to another value? If you need to map or translate inputs to arbitrary values, you can use the VLOOKUP function. Since there is no way to derive the output (i.e. it's arbitrary), we need to do some kind of lookup. The VLOOKUP function provides an easy way to do this.
Maps are part of the C++ STL (Standard Template Library). Maps are the associative containers that store sorted key-value pair, in which each key is unique and it can be inserted or deleted but cannot be altered. Values associated with keys can be changed.
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.
Python map() functionmap() function returns a map object(which is an iterator) of the results after applying the given function to each item of a given iterable (list, tuple etc.) Parameters : fun : It is a function to which map passes each element of given iterable. iter : It is a iterable which is to be mapped.
You can do it with an Extension Method (for decimal
for example):
public static class ExtensionMethods
{
public static decimal Map (this decimal value, decimal fromSource, decimal toSource, decimal fromTarget, decimal toTarget)
{
return (value - fromSource) / (toSource - fromSource) * (toTarget - fromTarget) + fromTarget;
}
}
Then you can use it like:
decimal res = 2.Map(1, 3, 0, 10);
// res will be 5
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With