Calculator.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "Calculator.h"
  7. #include "KeypadValue.h"
  8. #include <AK/Assertions.h>
  9. #include <AK/Math.h>
  10. Calculator::Calculator()
  11. {
  12. }
  13. Calculator::~Calculator()
  14. {
  15. }
  16. KeypadValue Calculator::begin_operation(Operation operation, KeypadValue argument)
  17. {
  18. KeypadValue res = 0;
  19. switch (operation) {
  20. case Operation::None:
  21. VERIFY_NOT_REACHED();
  22. case Operation::Add:
  23. case Operation::Subtract:
  24. case Operation::Multiply:
  25. case Operation::Divide:
  26. m_saved_argument = argument;
  27. m_operation_in_progress = operation;
  28. return argument;
  29. case Operation::Sqrt:
  30. if (argument < 0) {
  31. m_has_error = true;
  32. return argument;
  33. }
  34. res = argument.sqrt();
  35. clear_operation();
  36. break;
  37. case Operation::Inverse:
  38. if (argument == 0) {
  39. m_has_error = true;
  40. return argument;
  41. }
  42. res = argument.invert();
  43. clear_operation();
  44. break;
  45. case Operation::Percent:
  46. res = argument * KeypadValue { 1, 2 }; // also known as `KeypadValue{0.01}`
  47. break;
  48. case Operation::ToggleSign:
  49. res = -argument;
  50. break;
  51. case Operation::MemClear:
  52. m_mem = 0;
  53. res = argument;
  54. break;
  55. case Operation::MemRecall:
  56. res = m_mem;
  57. break;
  58. case Operation::MemSave:
  59. m_mem = argument;
  60. res = argument;
  61. break;
  62. case Operation::MemAdd:
  63. m_mem = m_mem + argument; // avoids the need for operator+=()
  64. res = m_mem;
  65. break;
  66. }
  67. if (should_be_rounded(res))
  68. round(res);
  69. return res;
  70. }
  71. KeypadValue Calculator::finish_operation(KeypadValue argument)
  72. {
  73. KeypadValue res = 0;
  74. switch (m_operation_in_progress) {
  75. case Operation::None:
  76. return argument;
  77. case Operation::Add:
  78. res = m_saved_argument + argument;
  79. break;
  80. case Operation::Subtract:
  81. res = m_saved_argument - argument;
  82. break;
  83. case Operation::Multiply:
  84. res = m_saved_argument * argument;
  85. break;
  86. case Operation::Divide:
  87. if (argument == 0) {
  88. m_has_error = true;
  89. return argument;
  90. }
  91. res = m_saved_argument / argument;
  92. break;
  93. case Operation::Sqrt:
  94. case Operation::Inverse:
  95. case Operation::Percent:
  96. case Operation::ToggleSign:
  97. case Operation::MemClear:
  98. case Operation::MemRecall:
  99. case Operation::MemSave:
  100. case Operation::MemAdd:
  101. VERIFY_NOT_REACHED();
  102. }
  103. if (should_be_rounded(res))
  104. round(res);
  105. clear_operation();
  106. return res;
  107. }
  108. void Calculator::clear_operation()
  109. {
  110. m_operation_in_progress = Operation::None;
  111. m_saved_argument = 0;
  112. clear_error();
  113. }
  114. bool Calculator::should_be_rounded(KeypadValue value)
  115. {
  116. // We check if pow(10, value.m_decimal_places) overflow.
  117. // If it does, the value can't be displayed (and provoke a division by zero), see Keypad::set_value()
  118. // For u64, the threshold is 19
  119. return value.m_decimal_places > rounding_threshold;
  120. }
  121. void Calculator::round(KeypadValue& value)
  122. {
  123. while (value.m_decimal_places > rounding_threshold) {
  124. bool const need_increment = value.m_value % 10 > 4;
  125. value.m_value /= 10;
  126. if (need_increment)
  127. value.m_value++;
  128. value.m_decimal_places--;
  129. if (value.m_value == 0) {
  130. value = 0;
  131. return;
  132. }
  133. }
  134. }