jherkenhoff / qalculate-android

Powerful and versatile multi-purpose calculator for the Android platform
GNU General Public License v2.0
127 stars 6 forks source link

Parsing with `to` looks a bit weird #45

Closed redfast00 closed 1 week ago

redfast00 commented 3 months ago

I'm using the pre-release v1 right now. When I type in 10 miles to km, it parses as 10 mi t B km, but shows the correct result.

image

Aside: thanks for making the release available; I'm looking forwards to installing via F-Droid as well :)

jherkenhoff commented 3 months ago

Hi redfast,

thanks for your interest in the app! Yes, the "to" handling is a bit weird. The "to" expressions are only automatically handled by the calculator.calculate function with its input argument being a string. All other functions (like calculator.parse) do not properly recognize the "to" keyword. I can imagine that the MathStructure object can not represent the to expressions natively? As far as I could see, the qalculate-cli and qalculate-gtk project manually add code to recognize the "to" keyword and handle the conversion in a large case structure.

I will need to ask the upstream Qalculate developers to shed some light on this. @hanna-kn, if you have time, can you maybe shed some light on the "to" keyword and conversion handling?

hanna-kn commented 3 months ago

The "to" keyword affects how the result is presented. The keyword is not represented in MathStructure and is not supported by Calculator::parse() (and neither is "where"). The simplest solution is to use Calculator::separateToExpression() (and Calculator::separateWhereExpression()).

For unit conversion, and other conversions supported directly by Calculator::calculate() (with string input), the expression after "to" can however be represented by MathStructure, and can optionally be returned by Calculator::calculate() (in the MathStructure *to_struct argument which is used both as an output and an input argument).

Calculator::calculateAndPrint() (and the newly introduced Calculator::parseToExpression()) and the user interfaces supports additional "to" conversions, such as number base conversion, primarily affecting the output from MathStructure::print(). These can not be represented by MathStructure.

jherkenhoff commented 1 week ago

Fixed in 0a76e8750a2ef7ed12ed1f820d2ae4180c25d4fb