3

Let's say a user inputs two different string, expecting a result for each one. They both contain either addition or subtraction of a percentage. How should this be handled?

My immediate thoughts are that addition and subtraction are commutative, and when we talk about "adding 10%" to a number, or "subtracting 20%", what we are really talking about is multiplying by 110%, or 80% respectively.

Examples

10 + 5 - 10% and 10 - 10% + 5.

Google
    10 + 5 - 10% = 14.5
    10 - 10% + 5 = 14

Wolfram Alpha
    10 + 5 - 10% = 13.5
    10 - 10% + 5 = 13.5

web2.0calc.com
    10 + 5 - 10% = 13.5
    10 - 10% + 5 = 14

Excel
    =10 + 5 - 10% = 1490% = 14.9
    =10 - 10% + 5 = 1490% = 14.9

Casio Calculator
    10 + 5 - 10% = 14.5
    10 - 10% + 5 = 14
  • Related bug report: https://bugs.launchpad.net/pantheon-calculator/+bug/1465680 – Lewis Goddard Sep 14 '15 at 22:14
  • An expression like "10 + 5 - 10%" is meaningless, I think it shouldn't be considered valid input from the user. – littleO Sep 14 '15 at 22:17
  • Some calculators say 1 + 2 * 3 = 9 while others say 1 + 2 * 3 = 7: it is up to you as the designer. – Henry Sep 14 '15 at 22:18
  • Wolfram Alpha's second answer looks difficult to justify. All others have an underlying logic of a sort. – Henry Sep 14 '15 at 22:19
  • 1
    @Henry I'm fairly certain that one should always be 7. I'd prefer the user be temporarily confused than have an answer that doesn't follow ordering rules. I'm curious about changing + 10% to being x 110% and -20% to * 80%. – Lewis Goddard Sep 14 '15 at 22:25
  • An expression involving %, as you have demonstrated, is dealt with outside the common computer operator precedence rules and as such is context/application/language dependent. There is no universal rule and there can't be one rule for all. For your application, define your rule and make sure the users know how you are performing the calculation. – NoChance Sep 14 '15 at 22:50

2 Answers2

2

$10%$ really just represents $\frac{10}{100} = 0.1$, and I always treat percentages this way for multiplication, addition, etc. Parsing for these allows you to only deal with standard floating point numbers, which are usually handled more easily. If you have any specific needs you can adjust accordingly. Under this definition: $$10 + 5 - 10\% = 10+5-0.1 = 14.9$$ $$10 - 10\% + 5 = 14.9$$

If you are trying to remove 10% of a quantity it is written x-10%x = x(1-10%) = 0.9x. This requires multiplication though, and is not represented only using addition or subtraction.

Of course, if this is your program you can write it however you like, as long as the end user knows what you have decided what $15 - 10\%$ means

  • Unfortunately, I think 0.1 is about the only option users aren't looking for in this situation, or they would have just typed 0.1 – Lewis Goddard Sep 14 '15 at 22:36
  • Well, that is not the fault of the programmer... that is the fault of the user. You could ask the user what they want 10% to mean (which is a hassle) or you can just program a general solution and require the user to conform. That is, unless you are building the application for a user specifically, in which case you should ask the user what they intend 10% to mean. I would say it is better to confuse the user temporarily and to go with the best solution, but again, if you have another approach in mind already go for it. It's your program, and only you know who will be using your program. – Brevan Ellefsen Sep 14 '15 at 22:42
2

You are trying to use an ambiguous process to come out with a unique correct solution. That's impossible!
The way to come up with a workable solution, is to impose some rules, let the user know them, and implement them in your application. For example, you can specify the order of execution, the priority of operands, and the use of parenthesis and brackets to give the user control of the order of execution. The order of execution is not commutative. Most applications give a higher priority to multiplication and division than addition and subtraction. Finding percent, involves multiplication/division, so this operations will be done before addition or subtraction. In the absence of any controls, default execution will be from left to right. Under these "rules," your examples will be calculated as follows: 1) the 5 will be added to the 10 making a total of 15, then 10% of 15 (1.5) will be subtracted from 15, giving a final result of 13.5; 2) 10% of 10 (1) is subtracted from10, giving 9, 5 is then added giving a final result of 14. If the user does not like these default results, then the order of execution must be specified. Changing the 1st example to 10 + (5 -10%) will give a final result of 10 + (5 - .5) 14.5. These examples make it clear that different results are obtained depending on the order of execution.

Guill
  • 134