DDMathParser icon indicating copy to clipboard operation
DDMathParser copied to clipboard

Support fractions using superscripts and subscripts

Open peterhoucll opened this issue 6 years ago • 19 comments

As the title.

Please check the attached examples, thank you. example

peterhoucll avatar May 08 '19 03:05 peterhoucll

+1

kerusan avatar May 08 '19 07:05 kerusan

Can you describe more what you’re asking for? This project is primarily for turning text in to math. There is already support for the Unicode-defines fractions (½, , , ¼, ¾, , , , , , , , , , and ), but I’m not sure how you’d want to write a fraction in a way that’s not division.

If you’re asking about more general features such as reducing fractions and keeping them as rational numbers instead of performing the division and turning them in to Doubles, we can chat about that too. It’s something I’ve thought about, but I’m reluctant to turn this project in to a full symbolic evaluation engine a la Mathematica.

davedelong avatar May 08 '19 15:05 davedelong

I hope to support the calculation of any fractions, thank you. ¹²⁄₂ + ⁴²⁄₂ = 27 ¹⁴⁄₅ + ⁵⁄₂₁ 4 ⁵⁄₆ + 3 ²⁵⁄₈

peterhoucll avatar May 09 '19 02:05 peterhoucll

How do propose representing these fractions in a string of text?

davedelong avatar May 09 '19 02:05 davedelong

Oh, are those superscript and subscript characters?

davedelong avatar May 09 '19 02:05 davedelong

Yes, can you support it?

peterhoucll avatar May 09 '19 02:05 peterhoucll

Perhaps... I already recognize superscript characters as exponents, such as . I'd have to think about how to account for this sort of possibility.

@kerusan is this what you have in mind as well?

davedelong avatar May 09 '19 02:05 davedelong

The separate superscript is still expressed as an exponents. When the superscript + ⁄ + subscript is expressed as a fraction, is this ok?

peterhoucll avatar May 09 '19 02:05 peterhoucll

Yes, checking for the / + subscripts would be the way to go

davedelong avatar May 09 '19 02:05 davedelong

Some of the trickiness would be to correctly interpret 5¹/₂ as 5.5 and not 5 * ½

davedelong avatar May 09 '19 02:05 davedelong

Can this solution be used? https://github.com/davedelong/DDMathParser/issues/142

peterhoucll avatar May 09 '19 02:05 peterhoucll

... I completely forgot I'd implemented that already. Yes, that was basically how I was thinking of doing it (using implicit addition instead of implicit multiplication)

davedelong avatar May 09 '19 02:05 davedelong

So, are you going to support fractions calculation?

peterhoucll avatar May 09 '19 02:05 peterhoucll

Yeah, this will probably be ok. I'll warn you up-front however that there will be no way to get ³/₂ as an answer. You'll get 1.5, because everything gets converted to Doubles.

davedelong avatar May 09 '19 02:05 davedelong

Is it possible to add a switch for the calculation result? If it is on, it is a fraction; if it is off, it is a Double, which is off by default.

peterhoucll avatar May 09 '19 02:05 peterhoucll

Not at this time. DDMathParser only supports calculations as Doubles. See #99.

Supporting a different kind of evaluation number would require a very very very large and invasive change to the entire codebase. It's something I'd like to do eventually, but that sort of change is years down the road. This sort of "recognize fractions" bit is doable in days.

davedelong avatar May 09 '19 02:05 davedelong

How is this solution? https://stackoverflow.com/questions/35895154/decimal-to-fraction-conversion-in-swift

peterhoucll avatar May 09 '19 02:05 peterhoucll

For using in your own code, that seems reasonable at first glance.

The problem with "using rational arithmetic throughout all calculations" as the answer suggests is that there is an entire domain of numbers that are not rational. As soon as you toss something like π in your string, you have to start dealing with mixing rational and irrational numbers as part of parsing and evaluation. This means that you're no longer doing evaluation of mathematical values, but are instead doing symbolic manipulations of them. Basically, you start dealing with numbers as a type that have a rational component, an irrational component, and an imaginary component, and then things get really weird as you try to define what it means to manipulate them.

As much as I'd like DDMathParser to be a "complete" solution for parsing code and handling this sort of stuff, I'm just one guy doing this in my free time. 😁

davedelong avatar May 09 '19 02:05 davedelong

Ok, I am converting the calculation results myself, huh.

thank you very much.

peterhoucll avatar May 09 '19 02:05 peterhoucll