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.
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
=
, the number on the display when you press =
is used as the operand. In most cases, pressing -=
yields zero.=
once, additional presses apply the last operator and operand to the displayed value.+
) 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.Input: 4+3x3x3x=
Output: 135
Explanation:
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.
Input: 4+3x3x3x==
Output: 3645
Explanation:
x
and the last number used is 27, so you get 135 * 27 which is 3645Input: 4-3x3x3x=
Output: 81
Explanation:
Input: 4+3x3x3x==
Output: 2187
Explanation:
Note: Below, ^
represents the xy power function, which has higher precedence than x
.
Input: 4+3x2^=
Output: 38
Explanation:
=
is pressed, so operands are 4,3,2,2 and operators +,x,^ become x,^,+Input: 4+3x2^==
Output: 1444
Explanation: (4*(3^2)+2)^2 or 38^2
Input: 2x2x3^=
Output: 192
Explanation:
Input: 5+7x3^=
Output: 1718
Explanation: 5x(7^3)+3
Answered by Sara on November 28, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP