What is BODMAS and why is it useful in programming?
相关问题
- d3.js moving average with previous and next data v
- How to get a fixed number of evenly spaced points
- Check if a number is a perfect power of another nu
- How to find the distance between a point and a par
- How would I perform math within a sql query to cal
相关文章
- ceil conterpart for Math.floorDiv in Java?
- why 48 bit seed in util Random class?
- Need help generating discrete random numbers from
- How do you create a formula that has diminishing r
- Math.Max vs Enumerable.Max
- How do I remove axis from a rotation matrix?
- How to calculate end points of perpendicular line
- Logarithmic scale returns NaN
http://www.easymaths.com/What_on_earth_is_Bodmas.htm:
Why it is useful in programming? No idea, but i assume it's because you can get rid of some brackets? I am a quite defensive programmer, so my lines can look like this:
with BODMAS you can make this a bit clearer:
I think i'd still use the first variant - more brackets, but that way i do not have to learn yet another rule and i run into less risk of forgetting it and causing those weird hard to debug errors?
Just guessing at that part though.
Mike Stone EDIT: Fixed math as Gaius points out
Another version of this (in middle school) was "Please Excuse My Dear Aunt Sally".
The mnemonic device was helpful in school, and still useful in programming today.
Order of operations in an expression, such as:
source: http://www.mathsisfun.com/operation-order-bodmas.html
I don't have the power to edit @Michael Stum's answer, but it's not quite correct. He reduces
to
They are not equivalent. The best reduction I can get for the whole expression is
or
When I learned this in grade school (in Canada) it was referred to as BEDMAS:
Brackets
Exponents
Division
Multiplication
Addition
Subtraction
Just for those from this part of the world...
I read somewhere that especially in C/C++ splitting your expressions into small statements was better for optimisation; so instead of writing hugely complex expressions in one line, you cache the parts into variables and do each one in steps, then build them up as you go along.
The optimisation routines will use registers in places where you had variables so it shouldn't impact space but it can help the compiler a little.