~aleteoryx/muditaos

ref: 4de9970108f7b1b454edec463c352b9a956cedec muditaos/module-apps/application-calculator/data/CalculatorUtility.cpp -rw-r--r-- 4.8 KiB
4de99701 — Piotr Tański [EGD-5952] Changed service stack depths acc. to real usage 4 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "CalculatorUtility.hpp"
#include "application-calculator/widgets/CalculatorStyle.hpp"
#include <module-utils/tinyexpr/tinyexpr.h>
#include <i18n/i18n.hpp>
#include <Utils.hpp>
#include <cmath>

Result Calculator::calculate(std::string source)
{
    source = prepareEquationForParser(source);
    int error;
    double result = te_interp(source.c_str(), &error);
    if (error == 0 && !std::isinf(result) && !std::isnan(result)) {
        auto output = utils::to_string(result);
        if (output.length() > CalculatorConstants::maxStringLength) {
            output = getValueThatFitsOnScreen(result);
        }
        if (utils::localize.get("app_calculator_decimal_separator") == style::calculator::symbols::strings::comma) {
            output.replace(output.find(style::calculator::symbols::strings::full_stop),
                           std::size(std::string_view(style::calculator::symbols::strings::full_stop)),
                           style::calculator::symbols::strings::comma);
        }
        return Result{source, output, false};
    }
    return Result{source, utils::localize.get("app_calculator_error"), true};
}

std::string Calculator::prepareEquationForParser(std::string input)
{
    input = replaceAllOccurrences(
        input, style::calculator::symbols::strings::division, style::calculator::symbols::strings::solidus);
    input = replaceAllOccurrences(
        input, style::calculator::symbols::strings::multiplication, style::calculator::symbols::strings::asterisk);
    input = replaceAllOccurrences(
        input, style::calculator::symbols::strings::comma, style::calculator::symbols::strings::full_stop);
    return input;
}

std::string Calculator::replaceAllOccurrences(std::string input, const std::string &from, const std::string &to)
{
    size_t index = 0;
    while (true) {
        index = input.find(from, index);
        if (index == std::string::npos)
            break;
        input.replace(index, from.length(), to);
        index += to.length();
    }
    return input;
}

std::string Calculator::getValueThatFitsOnScreen(double result)
{
    auto base   = static_cast<long long>(result);
    auto length = utils::to_string(base).length();
    if (base < 0) {
        length -= 1;
    }
    if (length > CalculatorConstants::expLength + 1) {
        return convertToNumberWithPositiveExponent(result, length - 1);
    }
    else if (length == CalculatorConstants::expLength + 1) {
        if (result < 0) {
            return utils::to_string(getCoefficient(result, CalculatorConstants::veryLowPrecision));
        }
        return utils::to_string(getCoefficient(result, CalculatorConstants::lowPrecision));
    }
    else if (length == 1 && result < -1) {
        return utils::to_string(getCoefficient(result, CalculatorConstants::lowPrecision));
    }
    else if (result > 1) {
        return utils::to_string(getCoefficient(result, CalculatorConstants::precision));
    }
    else {
        return convertToNumberWithNegativeExponent(result, base);
    }
}

std::string Calculator::convertToNumberWithPositiveExponent(double result, uint32_t exponent)
{
    result /= pow(10, exponent);
    auto exponentLength = utils::to_string(exponent).length();
    auto decimalPlace   = CalculatorConstants::precision - exponentLength - CalculatorConstants::expLength;
    if (result < 0) {
        decimalPlace -= 1;
    }
    return utils::to_string(getCoefficient(result, decimalPlace)) + "e" + utils::to_string(exponent);
}

std::string Calculator::convertToNumberWithNegativeExponent(double result, long long base)
{
    double frac = (result - base) * pow(10, CalculatorConstants::highPrecision);
    if (result < 0) {
        frac *= -1;
    }
    auto fractionalPart = static_cast<unsigned long int>(round(frac));
    auto fracLength     = utils::to_string(fractionalPart).length();
    auto exponent       = CalculatorConstants::highPrecision - fracLength + 1;
    if (exponent > CalculatorConstants::minusExpLength + 1) {
        result *= pow(10, exponent);
        auto exponentLength = utils::to_string(exponent).length();
        auto decimalPlace   = CalculatorConstants::precision - exponentLength - CalculatorConstants::minusExpLength;
        if (result < 0) {
            decimalPlace -= 1;
        }
        return utils::to_string(getCoefficient(result, decimalPlace)) + "e-" + utils::to_string(exponent);
    }
    else if (result < 0) {
        return utils::to_string(getCoefficient(result, CalculatorConstants::lowPrecision));
    }
    return utils::to_string(getCoefficient(result, CalculatorConstants::precision));
}

long double Calculator::getCoefficient(double result, uint32_t precision)
{
    return std::roundl(result * pow(10, precision)) / pow(10, precision);
}