1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 05:07:35 +00:00

Calculator: Round small number to prevent crash

Small numbers (smaller than 1e-19) can't be displayed in the calculator.
They provoke a division by zero in Keypad::set_value(), as 10^20
overflows.
This commit is contained in:
Lucas CHOLLET 2022-01-01 21:49:30 +01:00 committed by Andreas Kling
parent 939bf3e864
commit 7532ef78ad
3 changed files with 50 additions and 1 deletions

View file

@ -47,6 +47,22 @@ public:
void clear_error() { m_has_error = false; }
private:
static bool should_be_rounded(KeypadValue);
static void round(KeypadValue&);
static constexpr auto rounding_threshold = []() consteval
{
using used_type = u64;
auto count = 1;
used_type res = 10;
while (!__builtin_mul_overflow(res, (used_type)10, &res)) {
count++;
}
return count;
}
();
Operation m_operation_in_progress { Operation::None };
KeypadValue m_saved_argument { (i64)0 };
KeypadValue m_mem { (i64)0 };