# Precdence Hierarchy: Left-to-Right, Right-to-Left, and more?

Hi all -
I’m struggling a little bit to wrap my head around the mathematical precedence in SuperCollider. I’m in the midst of trying to translate a large chunk of JavaScript code and it is getting very tangled up in the syntax.
My preferred question is: is there a simple function that can reformat the order-of-operations from one language to another?
The more involved question would be: can anyone suggest a way to get more comfortable with the order-of-operations in SC? I remember PEMDAS from grade school - can I just SAD MEP?

Thanks!

It is just left to right but it respects parens. I just use a lot of parens for clarity.

2 + 3 - 10 * 20 is (((2+3) - 10) * 20)

Hope that helps.

Josh

Hi @josh -
It never bothered me before - but now that I’m trying to translate from JavaScript into SC, I’m finding it quite difficult to put the parens in the right place…

For instance, this is a line of code that I’m translating:

`sinm * (1.914602 - t * (0.004817 + 0.000014 * t)) + sin2m * (0.019993 - 0.000101 * t) + sin3m * 0.000289;`

Now I’m dealing with preexisting parens, possibly different precedence based on the operator, and I’m unsure how it would shake out in SC.

I think this would work, enclosing every multiplication in parens:

`(sinm * (1.914602 - t * (0.004817 + 0.000014 * t))) + (sin2m * (0.019993 - 0.000101 * t)) + (sin3m * 0.000289);`

@jpburstrom - I don’t think this works.

In SC, this code:

``````	(
var sinm = 2;
var t = 0.1;
var sin2m = 3;
var sin3m = 4;
(sinm * (1.914602 - t * (0.004817 + 0.000014 * t))) + (sin2m * (0.019993 - 0.000101 * t)) + (sin3m * 0.000289);
)

``````

returns 0.0088768684524.

Whereas, in Javascript:

``````{
var sinm = 2;
var t = 0.1;
var sin2m = 3;
var sin3m = 4;
print(sinm * (1.914602 - t * (0.004817 + 0.000014 * t)) + sin2m * (0.019993 - 0.000101 * t) + sin3m * 0.000289);
}
``````

returns: 3.88934502

Maybe there’s something methodical and obvious about this to the more mathematically-inclined SC users - but I’d definitely appreciate some advice about how to break this down more.

May be helpful to expand it into multiple lines to show the structure absolutely clearly.

``````sinm * (
1.914602 -
t * (
0.004817 +
0.000014 * t
)
)
+ sin2m * (
0.019993 -
0.000101 * t
)
+ sin3m * 0.000289
``````

Now it’s clear where the `*` operations are (the ones which SC will not automatically do first). Then, parenthesize them.

I’ll include your vars and test the calculation…

``````(
var sinm = 2;
var t = 0.1;
var sin2m = 3;
var sin3m = 4;

(sinm * (
1.914602 -
(t * (
0.004817 +
(0.000014 * t)
))
))
+ (sin2m * (
0.019993 -
(0.000101 * t)
))
+ (sin3m * 0.000289)
)

-> 3.88934502  // ok
``````

hjh