TransWikia.com

Algorithm used in Mac OS Calculator

Ask Different Asked by vbrin27 on November 28, 2021

I am trying to understand the algorithm used in basic calculator of Mac OS but I couldn’t crack the logic behind certain operations.

I could understand the following operations,

Input: 2+3×3=
Output: 11

Input: 2+3×3==
Output: 33
Explanation: The value of [2+3×3=] is 11, when we press = again,
the following operation is done,

Current Value (11) LastUsedOperator(x) LastPressedNumber(3) [11×3] = 33

But I couldn’t understand these and I don’t understand why these operations are supported,

Input: 4+3x3x3x=
Output: 135

Input: 4+3x3x3x==
Output: 3645

Input: 4-3x3x3x=
Output: 81

Input: 4+3x3x3x==
Output: 2187

I would like to know the algorithm used behind these operations.

One Answer

Honestly, I think it is a bug in the calculator, though possibly it could be defended as some sort of usability concession. Note: I am confining my analysis to the floating-point calculators. The programmer calculator behaves differently.

The general rules seem to be

  1. If you do not enter a number between pressing an operator key and pressing =, the number on the display when you press = is used as the operand. In most cases, pressing -= yields zero.
  2. After pressing = once, additional presses apply the last operator and operand to the displayed value.
  3. The bug is that if there is a low-precedence operator (like +) being deferred because a higher precedence operator (like x) is computing, the low precedence operator moves to the end of the equation for the initial computation.

Examples from the original question

Input: 4+3x3x3x=
Output: 135 Explanation:

  • 4+3x puts 4 on the stack while it computes the higher priority 3x...
  • 3x3x3 is 27
  • When you end with x= it has to decide what to do for the final x to complete the equation. What it does is replace the final operator with the deferred operator and shift the rest left, then it takes the currently displayed number, which is 27, adds it as the final operand.
    • you had 4 + (3x3x3x and a display showing 27
    • the calculator probably should convert that to 4 + (3x3x3x27)
    • instead it makes a bigger jump and converts it to (4x3x3x3) + 27
  • Like I said, probably a bug. You end up with
  • 4x3x3x3 + 27 which is 108 + 27 which is 135

Input: 4+3x3x3x==
Output: 3645 Explanation:

  • From the previous example, the last entered operation is x and the last number used is 27, so you get 135 * 27 which is 3645

Input: 4-3x3x3x=
Output: 81 Explanation:

  • Just like the first example, except 108 - 27 is 81

Input: 4+3x3x3x==
Output: 2187 Explanation:

  • Just like the second example, except now it is 81x27 is 2187

Additional examples

Note: Below, ^ represents the xy power function, which has higher precedence than x.

Input: 4+3x2^=
Output: 38
Explanation:

  • Display shows "2" when = is pressed, so operands are 4,3,2,2 and operators +,x,^ become x,^,+
  • 4*(3^2)+2 -> 4*9+2 -> 36+2 is 38

Input: 4+3x2^==
Output: 1444
Explanation: (4*(3^2)+2)^2 or 38^2

Input: 2x2x3^=
Output: 192
Explanation:

  • 3 is on display when = is pressed , ^ has precedence over x, which shifts to the end, so operands are 2,2,3,3 and operators x,x,^ become x,^,x
  • ((2x2)^3)x3 -> 4^3x3 -> 64*3 is 192

Input: 5+7x3^=
Output: 1718
Explanation: 5x(7^3)+3

Answered by Sara on November 28, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP