Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support fractions using superscripts and subscripts #166

Open
peterhoucll opened this issue May 8, 2019 · 19 comments
Open

Support fractions using superscripts and subscripts #166

peterhoucll opened this issue May 8, 2019 · 19 comments

Comments

@peterhoucll
Copy link

As the title.

Please check the attached examples, thank you.
example

@kerusan
Copy link

kerusan commented May 8, 2019

+1

@davedelong
Copy link
Owner

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
Copy link
Author

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

@davedelong
Copy link
Owner

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

@davedelong
Copy link
Owner

Oh, are those superscript and subscript characters?

@peterhoucll
Copy link
Author

Yes, can you support it?

@davedelong
Copy link
Owner

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
Copy link
Author

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

@davedelong
Copy link
Owner

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

@davedelong
Copy link
Owner

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

@peterhoucll
Copy link
Author

Can this solution be used?
#142

@davedelong
Copy link
Owner

... 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
Copy link
Author

So, are you going to support fractions calculation?

@davedelong
Copy link
Owner

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 davedelong changed the title Hello, I hope to support the fractions calculation, thank you. Support fractions using superscripts and subscripts May 9, 2019
@peterhoucll
Copy link
Author

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
Copy link
Owner

davedelong commented May 9, 2019

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
Copy link
Author

@davedelong
Copy link
Owner

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
Copy link
Author

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

thank you very much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants