Reputation: 1685
I'm encountering a strange issue with the basic unix "bc" command on a mac and I want to just make sure I'm understanding it correctly. From the man pages, it says it respects order of operations, which I remember from my schooldays as "My Dear Aunt Sally" (multiply, divide, add, subtract)
When evaluating the following string in bc I'm getting an unexpected value
5/2+4+6-8*4+5*7+8
23
However, if I go through and I parenthesize the order of operations I stated earlier, I get a different result:
(((5/2)+(4+6))-((8*4)+((5*7)+8)))
-63
Do I have a fundamental misunderstanding of the bc command, or math?
Upvotes: 3
Views: 993
Reputation: 1204
We know that x-y+z is not equal to x-(y+z). Thus you are committing a mistake by expecting 5/2+4+6-8*4+5*7+8 to be equal to (((5/2)+(4+6))-((8*4)+((5*7)+8))). Also, you need to study a bit more about operator precedence and truncation in division. For example, 5/2 though is equal to 2.5 is truncated to 2. Hence, 5/2+4+6-8*4+5*7+8 = 2+4+6-32+35+8 = 23. Hope, it helped
Upvotes: 2