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

Fraction Calculation #90

Closed
moliata opened this issue Oct 8, 2017 · 25 comments
Closed

Fraction Calculation #90

moliata opened this issue Oct 8, 2017 · 25 comments

Comments

@moliata
Copy link

moliata commented Oct 8, 2017

Hello, I am amazed by how powerful and simple this library is! I am currently stuck by this question, How to calculate fractions? Regards.

@mariuszgromada
Copy link
Owner

Hi,

Thx for your feedback. What do you mean to calculate fractions? You can write 3/4 or 5/2 - mXparser will perform division. But remember - the final number is always returned as double.

Best regards

@mariuszgromada
Copy link
Owner

I will introduce new method toFraction(double number) to convert double to fraction on request :-)

@moliata
Copy link
Author

moliata commented Oct 9, 2017

Thanks! When will v4.2 should be coming?

@mariuszgromada
Copy link
Owner

I do not plan to release v.4.2 any soon. But the code for will be on github in a few days. Which platform do you use? Java//.net?

@mariuszgromada
Copy link
Owner

mariuszgromada commented Oct 9, 2017

The only condition is that you will give a star to the repo :-)

@moliata
Copy link
Author

moliata commented Oct 9, 2017

Hello, O am using Java. Also, what do you mean I will give a start to the repo

@mariuszgromada
Copy link
Owner

mariuszgromada commented Oct 9, 2017

:-) Star on github, that you like mXparser :-) I just misspelled "star" as "start" - sorry.

@mariuszgromada
Copy link
Owner

mariuszgromada commented Oct 9, 2017

So I will build new jar just for you, containing this new feature. This feature will enable to convert the final result, which is double, to two integer numbers (numerator and denominator) or to one string "numerator/denominator". The method will be in mXparser calls. Possible usage will be
Expression e = new Expression("3/2 * 5/7");
String s = mXparser.toFraction( e.calculate() );

s will be "15/14"

@moliata
Copy link
Author

moliata commented Oct 9, 2017

Cool! I am just thinking what if I have a mixed fraction (you can learn more about what I mean on Google), what should I do. Also, this custom jar, won't change how division works. So 3/2 * 5/7 result would be 1.0714285715 and if I use toFraction(e.calculate) then it would return fraction, right? Ps: I am working on a PhotoMath alternative & I am thinking about making mXparser - a core calculator for app. So I need fractions and etc. support.

@mariuszgromada
Copy link
Owner

mariuszgromada commented Oct 9, 2017

Currently this cannot be supported, but I will think on that.

@mariuszgromada
Copy link
Owner

This could work as that:

5_2_3
10_5
2_4
3_5_4

But this is much bigger change as requires grammar modification.

@moliata
Copy link
Author

moliata commented Oct 9, 2017

Let me know what you can make, I usually check GitHub Notifications every 4-6h, thanks!

@mariuszgromada
Copy link
Owner

Decided - it will be possible to use number literals in fraction form directly in expression. Expample

Expression e = new Expression("2_3 + 2_1_2");

will evaluate to 3,16666666666667, and then using mXparser.tofraction( double ) it will be possible to convert it to a fraction again.

Bes regards

@moliata
Copy link
Author

moliata commented Oct 10, 2017

Oh ok! Thanks for the info.

@mariuszgromada
Copy link
Owner

mariuszgromada commented Oct 17, 2017

So it is done :-)

Fractions number literals support
Expression e = new Expression("2_3_2 + 1_2");

Double to fraction conversion

mXparser.toFraction(double value) - double[]
mXparser.toMixedFraction(double value) - double[]
mXparser.fractionToString(double[] fraction) - String
mXparser.toFractionString(double value) - String
mXparser.toMixedFractionString(double value) - String
mXparser.setToFractionInitSearchSize(long n)
mXparser.getToFractionInitSearchSize() - long

I recommend to start testing on this ones

mXparser.toFractionString(double value) - String
mXparser.toMixedFractionString(double value) - String

:-)

@moliata
Copy link
Author

moliata commented Oct 17, 2017

Thank you so much, regards.

@mariuszgromada
Copy link
Owner

mariuszgromada commented Oct 17, 2017

Please let me know your views on implemented changes. It required a lot of work :-) You should have jar on your email.

@mariuszgromada
Copy link
Owner

Aha - i decided to represent fraction as array of double, I did not wanted to introduce new Fraction class as majority of use case will be on strings.

normal fraction - double[3]
sign - at index 0
numerator - at index 1
denominator at index 2

Mixed fraction - double[4]
sign - at index 0
whole number - at index 1
numerator - at index 2
denominator at index 3

Bes regards

@moliata
Copy link
Author

moliata commented Oct 18, 2017

Great idea, amazing!

@mariuszgromada
Copy link
Owner

Please let me know when you test it.

@moliata
Copy link
Author

moliata commented Oct 18, 2017

Did you use double arrays?

@mariuszgromada
Copy link
Owner

mariuszgromada commented Oct 18, 2017

Yes, double has much bigger range. Long is limited to 2^64, double is something 10^307. All the mXparser is based on double. The method toFractionString formats double to nice integer, but allowing huge range. In this double array always integer is stored (not the int type, the value without decimals).

@mariuszgromada
Copy link
Owner

Did you manage to test it?

@moliata
Copy link
Author

moliata commented Oct 27, 2017

Hello, it works! Currently I am using my own custom instance of SympyGamma on App Engine, but this will be used for a normal calculator in my app.

@mariuszgromada
Copy link
Owner

Released - check nuget or maven :-)

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

No branches or pull requests

2 participants