mirror of
https://github.com/RGBCube/serenity
synced 2025-07-28 00:57:44 +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:
parent
939bf3e864
commit
7532ef78ad
3 changed files with 50 additions and 1 deletions
|
@ -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 };
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue