# Maths and mapping

**URL:** <https://in-thread.sonic-pi.net/t/maths-and-mapping/206>\
**Category:** \[P\] Patreon Support\
**Created:** [October 8, 2017, 11:05pm UTC](https://in-thread.sonic-pi.net/t/maths-and-mapping/206 "2017-10-08T23:05:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jkbrogan](https://avatars.discourse-cdn.com/v4/letter/j/858c86/32.png) [@jkbrogan](https://in-thread.sonic-pi.net/u/jkbrogan)\
**Post date:** [October 8, 2017, 11:05pm UTC](https://in-thread.sonic-pi.net/t/maths-and-mapping/206/1 "2017-10-08T23:05:53Z")

</div>

Hi everyone. Where might I find information about mathematic functions and methods, or more broadly, dealing with numeric values, in Sonic Pi?

For example, I’m looking to map incoming MIDI data (i.e., 0–127) onto a new range. In Max, I would do this by using the scale object. Additionally, how might I go about dealing with conversion, e.g., from float to integer?

---

<div class="post-metadata">

**Author:** ![samaaron](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/samaaron/32/7_2.png) [@samaaron](https://in-thread.sonic-pi.net/u/samaaron)\
**Post date:** [October 9, 2017, 7:40am UTC](https://in-thread.sonic-pi.net/t/maths-and-mapping/206/2 "2017-10-09T07:40:19Z")

</div>

@jkbrogan

Are you looking to do a linear mapping? If so, there’s no supported functionality built-into Sonic Pi at this stage (although this is planned for a future version). For now, this is how you might calculate a linear range mapping:

```ruby
           (out_max - out_min) (val - in_min)
   f (x) = -------------------------------- + out_min
                    in_max - in_min

```

With respect to converting from float to integer - in which context are you trying to do this? You typically shouldn’t need to worry if something’s a float or an int…

---

<div class="post-metadata">

**Author:** ![256k](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/256k/32/860_2.png) [@256k](https://in-thread.sonic-pi.net/u/256k)\
**Post date:** [February 12, 2019, 3:29am UTC](https://in-thread.sonic-pi.net/t/maths-and-mapping/206/3 "2019-02-12T03:29:26Z")

</div>

sorry to revive this thread, but I’m looking for the same solution.

i’ve used processing and there’s a map function to take the range you give and spread it over the range you want…

I’m not sure how to do this in sonic Pi even with the function you’ve shown here…

---

<div class="post-metadata">

**Author:** ![bradgonesurfing](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/bradgonesurfing/32/889_2.png) [@bradgonesurfing](https://in-thread.sonic-pi.net/u/bradgonesurfing)\
**Post date:** [March 15, 2019, 12:44pm UTC](https://in-thread.sonic-pi.net/t/maths-and-mapping/206/4 "2019-03-15T12:44:44Z")

</div>

You can define a function to do it

```
   define :rMap do |rIn, rOut, v|
      dOut = rOut[1] - rOut[0]
      dIn = rIn[1] - rIn[0]
      dOut / dIn * (v - rIn[0]) + rOut[0]
    end

    rIn = [0.0, 2.5]
    rOut = [10, 20.0]

    puts rMap(rIn, rOut, 0)
    puts rMap(rIn, rOut, 1)
    puts rMap(rIn, rOut, 2.5)

```

generates

> 10.0  
> 14.0  
> 20.0

[http://rubyfiddle.com/riddles/3b2f9](http://rubyfiddle.com/riddles/3b2f9)
