# Map function query

**URL:** <https://forum.arduino.cc/t/map-function-query/1313237>\
**Category:** Programming\
**Created:** [October 20, 2024, 7:31am UTC](https://forum.arduino.cc/t/map-function-query/1313237 "2024-10-20T07:31:35Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![petercl14](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/petercl14/32/1013044_2.png) [@petercl14](https://forum.arduino.cc/u/petercl14)\
**Post date:** [October 20, 2024, 7:31am UTC](https://forum.arduino.cc/t/map-function-query/1313237/1 "2024-10-20T07:31:35Z")

</div>

Hi, I am using this map function. VAL= map(a1,0,90,80,30) . When a1 =0, val=80. when a1 =90 val=30. I am getting when a1=37.69, VAL=60. The map function only works in integers so a1 would be seen as 38.  
Can anybody give me the maths here to calculate VAL from the value of a1? I cannot see how it is calculated.  
Thanks.

---

<div class="post-metadata">

**Author:** ![robtillaart](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robtillaart/32/1098811_2.png) [@robtillaart](https://forum.arduino.cc/u/robtillaart)\
**Post date:** [October 20, 2024, 7:37am UTC](https://forum.arduino.cc/t/map-function-query/1313237/2 "2024-10-20T07:37:23Z")

</div>

Use this variation which can handle float

```cpp
float mapfloat(float x, float in_min, float in_max, float out_min, float out_max)
{
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

```

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [October 20, 2024, 12:09pm UTC](https://forum.arduino.cc/t/map-function-query/1313237/4 "2024-10-20T12:09:55Z")

</div>

> [@petercl14](#):
>
> The map function only works in integers so a1 would be seen as 38.

_a1_ would be "seen" as 37. Going from float to integer truncates.

a7

---

<div class="post-metadata">

**Author:** ![noiasca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/noiasca/32/1126910_2.png) [@noiasca](https://forum.arduino.cc/u/noiasca)\
**Post date:** [October 21, 2024, 5:45am UTC](https://forum.arduino.cc/t/map-function-query/1313237/5 "2024-10-21T05:45:06Z")

</div>

> [@petercl14](#):
>
> I cannot see how it is calculated.

you should read the Arduino reference, it is explained in

[https://docs.arduino.cc/language-reference/en/functions/math/map/](https://docs.arduino.cc/language-reference/en/functions/math/map/)

---

<div class="post-metadata">

**Author:** ![johnerrington](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnerrington/32/340106_2.png) [@johnerrington](https://forum.arduino.cc/u/johnerrington)\
**Post date:** [October 21, 2024, 7:51am UTC](https://forum.arduino.cc/t/map-function-query/1313237/6 "2024-10-21T07:51:09Z")

</div>

> [@petercl14](#):
>
> I am using this map function. VAL= map(a1,0,90,80,30)

I'd suggest you dont use the map function. Where did you hget those values?

---

<div class="post-metadata">

**Author:** ![petercl14](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/petercl14/32/1013044_2.png) [@petercl14](https://forum.arduino.cc/u/petercl14)\
**Post date:** [October 21, 2024, 7:59am UTC](https://forum.arduino.cc/t/map-function-query/1313237/7 "2024-10-21T07:59:53Z")

</div>

using my figures and the maths I am getting the 60 return. thanks for the extra accuracy although for my purposes integers are accurate enough. It is no wonder I could not figure out the maths for getting 60. 37 is slightly more accurate than 38 as suggested by alto777.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [April 19, 2025, 8:00am UTC](https://forum.arduino.cc/t/map-function-query/1313237/8 "2025-04-19T08:00:48Z")

</div>

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
