Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arduino Map equivalent function in Java

Is there a function similar to the Map function in Arduino for Java?

I need to map a range of values to another range of values, so I was wondering if there was something similar to it in Java, I've been searching but I only get the Java's Map function.

like image 742
trigoman Avatar asked Sep 21 '11 20:09

trigoman


People also ask

Is there a map function in Java?

The map() function is a method in the Stream class that represents a functional programming concept. In simple words, the map() is used to transform one object into other by applying a function. That's why the Stream. map(Function mapper) takes a function as an argument.

What is Arduino map function?

The map() function provided by the Arduino language allows you to map that range of values to a different range.

What does the map command do?

map() applies function to each item in iterable in a loop and returns a new iterator that yields transformed items on demand. function can be any Python function that takes a number of arguments equal to the number of iterables you pass to map() .


1 Answers

The code of map() from Arduino's library is this:

long map(long x, long in_min, long in_max, long out_min, long out_max)
{
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

This will work in Java just the same - no real magic. But standard Java has nothing predefined like this.

like image 74
A.H. Avatar answered Oct 07 '22 14:10

A.H.