1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 09:37:34 +00:00

Calculator: Add adding/subtracting/multiplying/dividing by a percentage

It's now possible to easily calculate 50% of 50. :^)
This commit is contained in:
Karol Baraniecki 2022-12-26 17:38:27 +01:00 committed by Andreas Kling
parent 21cc8f65f5
commit 451ae985bf

View file

@ -12,6 +12,12 @@
Optional<Crypto::BigFraction> Calculator::operation_with_literal_argument(Operation operation, Crypto::BigFraction argument)
{
// Support binary operations with percentages, for example "2+3%" == 2.06
if (m_binary_operation_in_progress != Operation::None && operation == Operation::Percent) {
argument = m_binary_operation_saved_left_side * Crypto::BigFraction { 1, 100 } * argument;
operation = Operation::None; // Don't apply the "%" operation twice
}
// If a previous operation is still in progress, finish it
// Makes hitting "1+2+3=" equivalent to hitting "1+2=+3="
if (m_binary_operation_in_progress != Operation::None) {
@ -20,7 +26,8 @@ Optional<Crypto::BigFraction> Calculator::operation_with_literal_argument(Operat
switch (operation) {
case Operation::None:
VERIFY_NOT_REACHED();
m_current_value = argument;
break;
case Operation::Add:
case Operation::Subtract: