Note that there are some explanatory texts on larger screens.

plurals
  1. POCan Javascript ever violate the mathematical PEMDAS convention?
    primarykey
    data
    text
    <p>I was taught that in maths we evaluate things, with the acronym BODMAS Brackets, Orders(powers), Division, Multiplication, Addition, Subtraction.</p> <p>I understand that in Javascript, <code>*</code> and <code>/</code> have equal precedence. <code>+</code> and <code>-</code> do too. And left associativity is used.</p> <p>From the few examples I can think of, it seems to work just as well, producing the same results as BODMAs.</p> <p>It looks like in the reality of the mathematical rules, <s>it doesn't matter whether addition or subtraction is done first</s>. It can matter whether additional or subtraction can done first 5-2+3. And for division and multiplication, it seems like it's only an issue when division is left of multiplication like <code>9/3*4</code> then division must be done first. And Javascript does / first in that instance. </p> <p>But maybe there is an example where it breaks it! Where it gives a different result to standard maths. </p> <p>Can Javascript break logical mathematical rules?</p> <p><strong>UPDATE-</strong></p> <p>To answer myself, when in Mathematics one says, PEDMAS where D and M are equal precedence and done left to right. And ditto A and S. You are saying they're left associative, i.e. where the implied brackets are. So programming languages that 'implement PEDMAS' the priority/precedence, and DM and AS being left associative, are totally following the PEDMAS rule. So, if there is any breaking of mathematical rules, it isn't from the associativity of M,D,A,S which is the same as in (conventional) mathematics. Also, in Mathematics, the unary minus is here- PEUDMAS so -5^2=-(5^2)=-25. exponent takes priority over unary minus. Though javascript probably gets that right. Apparently floating points in javascript break rules, as shown in some answers, though that wasn't what I had in mind as what I had in mind was purely re PEDMAS, but it seems parentheses make a difference with floating point numbers, where they shouldn't in normal mathematics, as shown in the answer I accepted.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload