Changeset View
Changeset View
Standalone View
Standalone View
runners/calculator/qalculate_engine.cpp
Show First 20 Lines • Show All 73 Lines • ▼ Show 20 Line(s) | 73 | { | |||
---|---|---|---|---|---|
74 | if (job->error()) { | 74 | if (job->error()) { | ||
75 | qDebug() << i18n("The exchange rates could not be updated. The following error has been reported: %1",job->errorString()); | 75 | qDebug() << i18n("The exchange rates could not be updated. The following error has been reported: %1",job->errorString()); | ||
76 | } else { | 76 | } else { | ||
77 | // the exchange rates have been successfully updated, now load them | 77 | // the exchange rates have been successfully updated, now load them | ||
78 | CALCULATOR->loadExchangeRates(); | 78 | CALCULATOR->loadExchangeRates(); | ||
79 | } | 79 | } | ||
80 | } | 80 | } | ||
81 | 81 | | |||
82 | QString QalculateEngine::evaluate(const QString& expression) | 82 | QString QalculateEngine::evaluate(const QString& expression, bool *isApproximate) | ||
83 | { | 83 | { | ||
84 | if (expression.isEmpty()) { | 84 | if (expression.isEmpty()) { | ||
85 | return ""; | 85 | return ""; | ||
86 | } | 86 | } | ||
87 | 87 | | |||
88 | QString input = expression; | 88 | QString input = expression; | ||
89 | QByteArray ba = input.replace(QChar(0xA3), "GBP").replace(QChar(0xA5), "JPY").replace('$', "USD").replace(QChar(0x20AC), "EUR").toLatin1(); | 89 | QByteArray ba = input.replace(QChar(0xA3), "GBP").replace(QChar(0xA5), "JPY").replace('$', "USD").replace(QChar(0x20AC), "EUR").toLatin1(); | ||
90 | const char *ctext = ba.data(); | 90 | const char *ctext = ba.data(); | ||
91 | 91 | | |||
92 | CALCULATOR->terminateThreads(); | 92 | CALCULATOR->terminateThreads(); | ||
93 | EvaluationOptions eo; | 93 | EvaluationOptions eo; | ||
94 | 94 | | |||
95 | eo.auto_post_conversion = POST_CONVERSION_BEST; | 95 | eo.auto_post_conversion = POST_CONVERSION_BEST; | ||
96 | eo.keep_zero_units = false; | 96 | eo.keep_zero_units = false; | ||
97 | 97 | | |||
98 | eo.parse_options.angle_unit = ANGLE_UNIT_RADIANS; | 98 | eo.parse_options.angle_unit = ANGLE_UNIT_RADIANS; | ||
99 | eo.structuring = STRUCTURING_SIMPLIFY; | 99 | eo.structuring = STRUCTURING_SIMPLIFY; | ||
100 | 100 | | |||
101 | // suggested in https://github.com/Qalculate/libqalculate/issues/16 | ||||
102 | // to avoid memory overflow for seemingly innocent calculations (Bug 277011) | ||||
103 | eo.approximation = APPROXIMATION_APPROXIMATE; | ||||
mart: add an (approximate) string in the result, then good to go | |||||
104 | | ||||
101 | CALCULATOR->setPrecision(16); | 105 | CALCULATOR->setPrecision(16); | ||
102 | MathStructure result = CALCULATOR->calculate(ctext, eo); | 106 | MathStructure result = CALCULATOR->calculate(ctext, eo); | ||
103 | 107 | | |||
104 | PrintOptions po; | 108 | PrintOptions po; | ||
105 | po.number_fraction_format = FRACTION_DECIMAL; | 109 | po.number_fraction_format = FRACTION_DECIMAL; | ||
106 | po.indicate_infinite_series = false; | 110 | po.indicate_infinite_series = false; | ||
107 | po.use_all_prefixes = false; | 111 | po.use_all_prefixes = false; | ||
108 | po.use_denominator_prefix = true; | 112 | po.use_denominator_prefix = true; | ||
109 | po.negative_exponents = false; | 113 | po.negative_exponents = false; | ||
110 | po.lower_case_e = true; | 114 | po.lower_case_e = true; | ||
111 | po.base_display = BASE_DISPLAY_NORMAL; | 115 | po.base_display = BASE_DISPLAY_NORMAL; | ||
112 | 116 | | |||
113 | result.format(po); | 117 | result.format(po); | ||
114 | 118 | | |||
115 | m_lastResult = result.print(po).c_str(); | 119 | m_lastResult = result.print(po).c_str(); | ||
116 | 120 | | |||
121 | if (isApproximate) { | ||||
122 | *isApproximate = result.isApproximate(); | ||||
123 | } | ||||
124 | | ||||
117 | return m_lastResult; | 125 | return m_lastResult; | ||
118 | } | 126 | } | ||
119 | 127 | | |||
120 | void QalculateEngine::copyToClipboard(bool flag) | 128 | void QalculateEngine::copyToClipboard(bool flag) | ||
121 | { | 129 | { | ||
122 | Q_UNUSED(flag); | 130 | Q_UNUSED(flag); | ||
123 | 131 | | |||
124 | QApplication::clipboard()->setText(m_lastResult); | 132 | QApplication::clipboard()->setText(m_lastResult); | ||
125 | } | 133 | } | ||
126 | 134 | |
add an (approximate) string in the result, then good to go