Skip to content

runners/calculator: Improve output base handling

Reimplement krunner base conversion through libqalculate PrintOptions.base instead of parsing decimal output and converting it to hex. This approach allows for correct display of multiple solutions converted to different base, for example: hex= x * x = 4 will output x = 0x2 || x = -0x2. Also, this makes it possible to easily implement more bases like bin, oct, time, roman, etc.

To detect required base findPrefix function was implemented in QalculateEngine, this function checks are there any common bases like hex, oct, bin, roman, time, pi and if there is it sets PrintOptions.base to corresponding base, this common base mappings were taken from libqalculate/src/qalc.cc set_option function (lines from 747 to 776, libqalcualate commit d767c71). To handle non-common bases prefix base was implemented, examples: base 42=64 results to 1M, base 2**16=123456789 results to \1883\52501. When findPrefix function encounters base keyword in prefix, it sets base to BASE_CUSTOM, then at QalculateEngine::evaluate if base is set to BASE_CUSTOM everything in prefix after base keyword is calculated and set as custom base for main calculation via CALCULATOR->setCustomOutputBase.

BUG: 450558

Examples

  • Quadratic equation with hex output

    hex=x*x=4 results to x = 0x2 || x = -0x2

  • Convert to roman numerals

    roman=42 results to XLII

  • Manipluate time

    time=12:34 - 11:42 results to 0:52

  • Convert to custom bases

    base 42=256 results to 64 base 2**16=123456789 to \1883\52501

Edited by Max Ramanouski

Merge request reports