I've been doing a bit of math with Javascript for a project I'm currently working on, and have just stumbled across the following:
I'm calculating an amount of results remaining in a list of results (there's a "Load More" button)
The code is as follows:
if (data.length > startIndex + maxLoadAmount) {
var loadMoreButton = new LoadButton()
loadButton.ToLoad = data.length - startIndex + maxLoadAmount;
loadMoreButton.click(function () {
var amountOfCurrentWidgets = $(this).parent().children('.widget').length;
this.remove();
loadChildren(amountOfCurrentWidgets, selector, data, widgetType);
});
$(selector).append(loadMoreButton);
}
I was surprised at the result when I ran it!
In a particular test, there are 16 available widgets, and when starting at index 0 with a maximum of 5 displayed, the result is 21.
I tracked this down to javascript performing the subtraction before the addition. You can reproduce it in jsfiddle here: http://jsfiddle.net/JamesPattison/3hvr4vsr/
The workaround is to surround the addition in brackets, though I guess what i'm really asking is:
Am I going insane?
In JavaScript addition and subtraction have the same precedence and both have left-to-right associativity. So with the code in your fiddle:
var x = 16, y = 0, z = 5;
var result = x-y+z; //16-0+5, expected by BODMAS: 16-(0+5) = 11
$('#results').html(result.toString())
result
is calculated like this:
(16 - 0) + 5
That's how it's evaluated in JavaScript, Java, C, C++, C#, and every other language I can think of.
The only way it would be broken up differently would be if addition had a higher precedence than subtraction, like multiplication does:
16 - 0 * 5 => 16 - (0 * 5)
But it doesn't.
Re BODMAS, it really should be written:
B O DM AS
From the WikiPedia article on order of operations:
These mnemonics may be misleading when written this way, especially if the user is not aware that multiplication and division are of equal precedence, as are addition and subtraction.
Similarly, all of the following references say that in standard arithmetic, division and multiplication have the same precedence, and addition and subtraction have the same precedence:
The Order of Operations: PEMDAS - purplemath.com:
A common technique for remembering the order of operations is the abbreviation "PEMDAS", which is turned into the phrase "Please Excuse My Dear Aunt Sally". It stands for "Parentheses, Exponents, Multiplication and Division, and Addition and Subtraction". This tells you the ranks of the operations: Parentheses outrank exponents, which outrank multiplication and division (but multiplication and division are at the same rank), and these two outrank addition and subtraction (which are together on the bottom rank)
Math Steps - eduplace.com:
The rules are:
- Multiply and divide from left to right.
- Add and subtract from left to right.
Rules of arithmetic - mathcentre.ac.uk:
This is hard to remember so here’s an acronym to help you — BODMAS. It means:
where division and multiplication have the same priority, and also addition and subtraction have the same priority, so in each case we have bracketed them together.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With