How do I take the absolute value of a number?

This may be an obvious question but I can’t seem to find an answer so I figured I’d ask here. How do I take the absolute value of a number?

I am using line to produce a series of numbers which contain both negative and positive numbers to be used to control various things. One of the things I’d like to control only can take positive numbers. I’d like to use the same series to feed into this control rather than creating a separate positive line series. Hence, I’m looking for a way to return the absolute value of the current number of that series.

Here you go: https://ruby-doc.org/core-2.5.0/Numeric.html#method-i-abs

It’s not specifically part of the Sonic Pi syntax but of the Ruby language that Sonic Pi is built on. (Which is why it’s not documented in the Sonic Pi language reference documents).

Oh excellent. There should be a lot of other interesting things I can play around with now that I’m aware of this. Guess I’m spending the next week learning Ruby haha.

Sure. It’s worth keeping in mind that only Sonic Pi commands are officially supported; anything plain Ruby may or may not work :slight_smile:

1 Like

Alright good to know. I’m sure all the important math stuff works anyways.

Another option would be to test for being less than 0 and subtract the result from 0 if it is.

x = 0 - x if x < 0

Ahh, yeah I guess theres always a work around for things like this. Thanks.