Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.9k views
in Technique[技术] by (71.8m points)

math - What does the term "BODMAS" mean?

What is BODMAS and why is it useful in programming?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

http://www.easymaths.com/What_on_earth_is_Bodmas.htm:

What do you think the answer to 2 + 3 x 5 is?

Is it (2 + 3) x 5 = 5 x 5 = 25 ?

or 2 + (3 x 5) = 2 + 15 = 17 ?

BODMAS can come to the rescue and give us rules to follow so that we always get the right answer:

(B)rackets (O)rder (D)ivision (M)ultiplication (A)ddition (S)ubtraction

According to BODMAS, multiplication should always be done before addition, therefore 17 is actually the correct answer according to BODMAS and will also be the answer which your calculator will give if you type in 2 + 3 x 5 .

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:

result = (((i + 4) - (a + b)) * MAGIC_NUMBER) - ANOTHER_MAGIC_NUMBER;

with BODMAS you can make this a bit clearer:

result = (i + 4 - (a + b)) * MAGIC_NUMBER - ANOTHER_MAGIC_NUMBER;

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...