Java Avoiding Math Confusion in Your Programs

Avoiding Math Confusion in Your Programs

Across the internet, there are posts asking what the correct answer is for what seems like a simple math problem. These generally result in hundreds or thousands of responses arguing the correct answer. For example, what is the value of X using the following math problem?

X = 6 / 2(1+2)

While a multitude of answers are often seen for this problem, the results generally center on two possible answers, 1 and 9. When you view answers people post on social media, you’ll find that most give the answer of 1 using the following logic:

X = 6/2(1+2)

X = 6/2(3)

X = 6/6

X = 1

In fact, if you type the equation into an older calculator, you are likely to get a result of one as well, as shown in this Casio fx-82MS:

Only one answer is correct for this problem and it is not 1. The Casio is wrong by today’s standards! You can confirm this by creating a simple program in your favorite programming language, such as Java:

public class SimpleMath {
    public static void main(String[] args) {
        double X = 6 / 2 * (1+2);
        System.out.println("X = " + X );
    }
}

When run, this program produces the simple result of:

X = 9

In the past, the manner outlined above to get to the result of 1 was considered correct. This is because when doing division, it was common to take what was on the right side and put it below what was on the left. This made sense when you did math problems such as:

X = 6 / 2Y

Then it could be interpreted as: 

 6

—-

 2Y

Thus, if y was equal to (1+2), then the result would be 1. This, however, is not how the problem would be evaluated today. You can again test this with your favorite programming language. I’ll again use Java:

public class SimpleMath {
    public static void main(String[] args) {
        double y = 1+2;
        double x = 6 / 2*y;

        System.out.println("y = " + y );
        System.out.println("x = " + x );
    }
}

The output this time is:

y = 3.0

x = 9.0

What has changed is that division is no longer considered to be everything on the left divided by everything on the right. Rather, it falls into standard levels of operator precedence. This order is generally BODMAS:

  • Brackets

  • Orders (exponents, powers, square roots, etc.)

  • Division and Multiplication

  • Addition and Subtraction

Items on each level of the list are done in left-to-right order, thus in our problem above, the 6 is clearly divided by the 2 first and then multiplied by 3 (the result of 1+2), thus giving the correct answer of 9.

What about Exponential Power?

Related to this, the handling of powers can also be evaluated erroneously. What would be the result if you had an equation with exponents such a 4 to the power of 3 to the power of 2:

Two possible answers are often cited for this problem as well. They are 4,096 and 262,144, but which is correct? A little HTML with a bit of JavaScript code can help provide the answer:

js exponents

As you can see, the correct result is 262,144. The following two numbers help to illustrate that exponential operations are not done from left-to-right, but rather from right-to-left. 

The Lesson to Take Away

The order of operator evaluation is important to understand. Remember what was mentioned above. Also remember that it is left-to-right order for evaluating items at the same level; however, powers would be evaluated from right-to-left. The most important lesson to take away, however, is that parentheses are your friend. If you want to remove all doubt from the order that expressions will be evaluated, then add parenthesis and confusion should be removed. After all, “(6 / 2) * (1+2)” is clearer than “6/2 * (1+2)” even if they do the same thing.

Latest Posts

Related Stories