davedelong / DDMathParser

String → Number
MIT License
854 stars 153 forks source link

Support fractions using superscripts and subscripts #166

Open peterhoucll opened 5 years ago

peterhoucll commented 5 years ago

As the title.

Please check the attached examples, thank you. example

kerusan commented 5 years ago

+1

davedelong commented 5 years ago

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.

peterhoucll commented 5 years ago

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

davedelong commented 5 years ago

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

davedelong commented 5 years ago

Oh, are those superscript and subscript characters?

peterhoucll commented 5 years ago

Yes, can you support it?

davedelong commented 5 years ago

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?

peterhoucll commented 5 years ago

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

davedelong commented 5 years ago

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

davedelong commented 5 years ago

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

peterhoucll commented 5 years ago

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

davedelong commented 5 years ago

... 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)

peterhoucll commented 5 years ago

So, are you going to support fractions calculation?

davedelong commented 5 years ago

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.

peterhoucll commented 5 years ago

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.

davedelong commented 5 years ago

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.

peterhoucll commented 5 years ago

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

davedelong commented 5 years ago

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. 😁

peterhoucll commented 5 years ago

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

thank you very much.