Please explain this to me : multiplying done before dividing?

I was pulling my hairs for not being able to solve a simple N-body setup until I noticed something strange. I was under the impression that multiplying was done before dividing but not in this case. Am I overlooking something?
Thanks for your help.
Cheers,
Adrian.

int num = 1000; 
float radius = 400;  
float density = num / PI*sq(radius);
println (density);
 Result : 5.092958E7

int num = 1000; 
float radius = 400;  
float density = num / (PI*sq(radius));
println (density);
// result 0.0019894368

I think they are of the same priority; it’s just what’s left comes first (from left to right).

It is just + and - is before * and /

1 Like

Hi @adrinalino,

That’s the rules …

Cheers
— mnse

2 Likes

Thanks guys, we’re taught this differently over in the Netherlands but thanks for the clarification.
Cheers,
Adrian.

1 Like
1 Like

Hi @adrinalino,

That’s really hard to believe…
If that would be the case, your math in the Netherlands would be broken. I’m from your neighboring country and on equal priority (ie. multiply and divide) it is always left to right. :wink:

Cheers
— mnse

2 Likes

I think the confusion is b/c computational operator for division / is equivalent to mathematical operator Ă·

However, operator ÷ doesn’t behave exactly the same as a mathematical fractional bar: \color{magenta}{3\,\times\,a\over5\,\times\,b}

That is, \color{cyan}{3\,\times\, a\over5\,\times\,b} isn’t the same as \color{red}3\times a\;\div\;5\times b

B/c we don’t want \color{brown}3\times a\;\div\;5 to happen before 5 is multiplied by b.

We’d need to wrap \color{orange}5\times b w/ parentheses to force multiplication to happen before ÷: \color{gold}3\times a\;\div\;(5\times b)

That’s why expression num/PI * sq(radius) is wrong while num / ( PI*sq(radius) ) is correct.

3 Likes

We learn a frase to remember it “Meneer van Dalen wacht op antwoord”.
m : machtsverheffen (exponentiation)
v : vermenigvuldigen (multiply)
d : delen (devide)
w : worteltrekken (square root)
o : optellen (add)
a : aftrekken (subtract)

Cheers,
Adrian.