native rational numbers in Max

freepoulite's icon

Hello,

Does max developers plans to integrate rational numbers to Max?
I'm a bit curious about it because as soon as you want to do some "complex" rythmn things you really need it to keep precision.
the awesome bach library support it in many ways but it is far much slow than native types in max and it become a problem when you need high speed calculation in real time music context
Does anyone know a faster alternative? (external, abstraction)
I know that the next update of bach will clearly increase speed but I'm a bit hurry...

best

Pierre

andrea agostini's icon

Hi.

No clue about Cycling's plans of course... but would you mind posting an example in which you think bach's rationals are the bottleneck? This might be very useful for us to do some profiling.

On the other hand, it won't be difficult at all implementing a small library of abstractions for dealing with rational numbers - you can express your rationals as lists of numerator and denominator, and the only relatively tricky thing would be computing the lcm.

Cheers
aa

$Adam's icon

Hi,

although that's indeed not an 'out-of-the-box' solution, you can deal with rational numbers (as Andrea already pointed out) if you represent them as lists of two numbers (numerator & denominator) quite easily. To compute the lcm and the gcd you could use the appropriate externals from my library as well ([sadam.gcd] and [sadam.lcm]), which are both quite well speed-optimized (see http://www.sadam.hu/software ).

Hope this helps,
Ádám

freepoulite's icon

Hello

Look at this patch
I've tried to make my own rational abstraction but it is a bit slow too

4207.Timersintfloatratnumrat.maxpat
Max Patch
freepoulite's icon

thanks a lot for your help and for your libraries!

best

Pierre

andrea agostini's icon

Thank you for the patch, I'll look into it asap!

$Adam's icon

Hi,

actually the [fromsymbol] & friends generate up A LOT of cpu usage. If you use simple lists instead of the symbol-based representation, you can make your [p ratSimp] patch around 10 times faster:

Max Patch
Copy patch and select New From Clipboard in Max.

Hope this helps,
Ádám

freepoulite's icon

woaw that's fast! :)

thanks a lot

Pierre

Roman Thilenius's icon

i guess what uses the most CPU in bach is the the sublist formatting, not the actual process.

using custom abstractions should be fine, i see no need for an external.

btw, Timersintfloatratnumrat.maxpat simply chrashes my max 5 app.

btw2, i agree with the idea that the most effective solution is to use a list of 2 integers (as opposed to
formatting symbols such as "ratnum_4|3" or similar bs.)

$Adam's icon

Hi Pierre,

how about this small set of abstractions?

Max Patch
Copy patch and select New From Clipboard in Max.

Hope that helps,
Ádám

Peter McCulloch's icon

This is tangential, but Clojure does support rational numbers and can be used inside Max. Haven't done it myself, but if you wanted to go all OpenMusic / PWGL on it...

freepoulite's icon

@Adam
Thank you a lot your abstractions it looks really simple and fast!

Thanks to everyone for your contributions

Best

Pierre