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

Fractions #84

Closed
husayt opened this issue Sep 24, 2013 · 26 comments
Closed

Fractions #84

husayt opened this issue Sep 24, 2013 · 26 comments
Labels

Comments

@husayt
Copy link

husayt commented Sep 24, 2013

A way to convert decimal numbers into their fraction form i.e. 0.25 = 1/4.

Operations on fractions:

  • 1/4+2/4=3/4
  • 1/4*2/4=3/4
  • and etc
@josdejong
Copy link
Owner

Yes, very nice. Working with ratios is one way to maintain arbitrary precision. Like Ratio.js.

@husayt
Copy link
Author

husayt commented Dec 5, 2013

This would be very nice, I would see rational as another type i.e. like complex, matrix.

We have big numbers and having rationals will mean we cover any (pretty much, except VERY BIG numbers) rational number. What could one want more for complete happiness?

@josdejong
Copy link
Owner

What could one want more for complete happiness?

Binary, hexadecimal, octal numbers :D

@Itangalo
Copy link

What could one want more for complete happiness?

Algebraic numbers! That is, a way to recognize that 1,41421… = √2

"Simpler" version: Recognizing only numbers that satisfy a^n = m and (where n and m are integers), and rewriting these numbers to the format ∛42 (or whatever)

More difficult (and complete) version: Recognizing numbers that could also be 2 + √5 – ∛42, or so. (That is, all numbers that satisfy a polynomial equations P(x) = 0, and express these as radicals to an arbitrary power.)

The more complete version is, I think, very difficult to achieve – even from a math theory perspective. I also think that the simpler version covers 99% of the use cases.

@Itangalo
Copy link

Another note: I don't think the complexity of Ratio.js is necessary here. As far as I'm concerned, I only need to know if a decimal number equals a reasonable simple fraction, or a reasonably simple radical. No need to maintain extreme precision in calculations.

(In this case, "reasonable simple fraction" could be interpreted as a fraction where the denominator is less than 1000, and a "reasonable simple radical" could be up to the 10th root. Or so.)

@josdejong
Copy link
Owner

Yes, support for symbolic computation would be great.

@quantum-webdesign
Copy link

I would strongly agree with Itangalo, a basic fractions component to math.js would significantly improve math.js :)

@husayt
Copy link
Author

husayt commented Apr 24, 2014

I think the best way to implement this is to add another type for fractions( ratios), like we do with complex numbers.

Then we could another type for symbolic computation. This way these types will maintain closed ecosystems for all the supported operations within and also bridges will allow cross type computation.

Something similar we have for bignumber and standard numbers currently

@josdejong
Copy link
Owner

I agree with you @husayt

Before we introduce more data types, I would love to introduce a modular approach to data types. I want to split functions in separate, decoupled sub-functions, each handling one data type, and make an easy way to describe relations and conversion rules to deal with cross type computations. We should be able to split math.js in standalone packages for Matrix, BigNumber, Unit, etc.

@husayt
Copy link
Author

husayt commented Apr 24, 2014

As usual, whenever I think I came with good idea, Jos just comes with even
better one.

Looking forward. Thanks
On 24 Apr 2014 20:51, "Jos de Jong" [email protected] wrote:

I agree with you @husayt https://github.com/husayt

Before we introduce more data types, I would love to introduce a modular
approach to data types. I want to split functions in separate, decoupled
sub-functions, each handling one data type, and make an easy way to
describe relations and conversion rules to deal with cross type
computations. We should be able to split math.js in standalone packages for
Matrix, BigNumber, Unit, etc.


Reply to this email directly or view it on GitHubhttps://github.com//issues/84#issuecomment-41323918
.

@josdejong
Copy link
Owner

Ha ha. That's not really true, you and others bring up a lot of good ideas, like this symbolic computation, which will be really cool. Without you guys this library would be far from as good as it is right now, and there is still tons of good ideas waiting to be implemented :).

Regarding a modular structure: see also #71

@husayt
Copy link
Author

husayt commented Jun 7, 2014

Meanwhile, this might be a useful addition to math.js.

 frac(30.34)="30+17/50"
 frac(30)="30"

This function prints any decimal number in its simplified fraction format.

this.frac = function(x)
{
    var math=mathjs(),i,limit=100000000000;
    var intPart= math.fix(x);
    var decPart= math.subtract(x,intPart);

    for(i =1;math.unequal(decPart,math.fix(decPart))&&i<limit;i*=10)
    {
        decPart=math.multiply(decPart,10);
    }
    var gcd=math.gcd(i,decPart);
    return i==1?intPart:math.print('$a+$b/$c',
    {
        a: intPart,
        b: math.divide(decPart,gcd),
        c: math.divide(i,gcd)
        });
}

to test input this method into http://www.numerics.info/scripts and try in calculator.

Also, very useful simplify fractions method:

simplify(30,34)= "15/17"

the code is:

this.simplify = function(x,y)
{
    var math=mathjs();
    var gcd=math.gcd(x,y);
    return math.print('$b/$c',
    {
        b: math.divide(x,gcd),
        c: math.divide(y,gcd)
        });
}

@josdejong
Copy link
Owner

Thanks, these are some nice, little functions. The function names should not collide of course with future functionality.

@husayt
Copy link
Author

husayt commented Sep 17, 2014

Hi Jos, congratulations with recent launch of 1.0 version.

Now that work is going towards 2.0 I was wondering if there are any plans for fractions. Would be good to know your thinking on this.

Thanks

@josdejong
Copy link
Owner

Thanks Huseyn :)

See my comment here regarding number base conversions, same holds for fractions, which would be great to have.

@husayt
Copy link
Author

husayt commented Sep 25, 2014

Jos, I was thinking about implementing this myself and it's not as simple as it seems.

The easiest part is to convert number to fraction, the difficulty start is when you want to use these fractions in expressions. It appears we need another type (like bignumber, complex number), call it fraction or rationalNumber.

If expression has a fraction in it then result of operations and should be also a fraction.

Only then we would get exact right answer for the following simple expression and alike:
1/3+1/3=2/3 or 2/5+4/5=6/5=1 1/5

Do you have any hints, if I want to introduce this type?

Thanks

@josdejong
Copy link
Owner

yes indeed. I think we can integrate Ratio.js similarly as Decimal.js is integrated right now for bignumber support.

@Yaffle
Copy link
Contributor

Yaffle commented Sep 26, 2014

What about fractions with BigIinteger numerators/denominators or may be even with polynomials in numerator/denominator - http://en.wikipedia.org/wiki/Rational_function ?

@josdejong
Copy link
Owner

That would be great. I too want to implement BigNumber support for Unit, that would be similar to BigNumbers in Fractions.

@josdejong
Copy link
Owner

I've implemented support for fractions in the v2 branch of the project. Powered by fraction.js. It works awesome!

docs: https://github.com/josdejong/mathjs/blob/v2/docs/datatypes/fractions.md
examples: https://github.com/josdejong/mathjs/blob/v2/examples/fractions.js

There is one thing I'm not yet certain about: what should be the default notation when formatting a fraction using math.format? Right now I have built in two formatting options:

var a = math.fraction(2, 3);
console.log(math.format(a));                          // Fraction, 2/3
console.log(math.format(a, {fraction: 'ratio'}));     // Fraction, 2/3
console.log(math.format(a, {fraction: 'decimal'}));   // Fraction, 0.(6)

Which value should be the default for option fraction? 'ratio' or 'decimal' notation?

@husayt
Copy link
Author

husayt commented May 12, 2015

THIS IS AMAZING NEWS. My most missing feature is not missing anymore. Thank you Jos.

I think ratio notation should be default, as not everything can be expressed as decimal.

@josdejong
Copy link
Owner

:)

It was cool to implement, it's now very easy in v2 as it automatically converts fractions to numbers for functions not supporting fractions :). v2 is not yet released, but is largely complete.

I agree with you that ratio should be the default. I was thinking that higher educated people may prefer the decimal notation, whilst regular users probably prefer the ratio notation.

@husayt
Copy link
Author

husayt commented May 12, 2015

This is so nice, I can't wait to v2 release. We will be integrating it into numerics straigthaway.

Just need to figure out best way of putting this in calculator. Before our calculator was working in bigNumber mode by default and it was ok. But now I am not sure.

On one side I want 6.5+3.4 to display 9.4, on the other 2/3+2/3 to display 4/3. Need to figure out the best way to config this to get best of both behavior.

Also, here are some issues/questions

  • support mixed fractions e.g ( 3 4/5)
  • convert improper fraction into mixed number and mixed number into improper fractions.
  • math.eval( math.fraction(2,3)) gives exception
  • help missing for math.fraction

@husayt
Copy link
Author

husayt commented May 13, 2015

Jos, shall I put issues above into different card? or is it ok to leave here?

@josdejong
Copy link
Owner

Please create a new issue for them, thanks!

@josdejong
Copy link
Owner

The just released v2 has support for fractions, powered by https://github.com/infusion/Fraction.js/

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

No branches or pull requests

5 participants