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

Configure implicit conversion between bignumbers, fractions, and numbers #710

Open
gitKad opened this Issue Sep 12, 2016 · 5 comments

Comments

Projects
None yet
2 participants
@gitKad

gitKad commented Sep 12, 2016

I can't multiply bignumbers and fractions. Here's a failing test I added to function\arithmetic\multiply.test.js:

it('should multiply mixed fractions and big numbers', function() {
  assert.equal(multiply(math.bignumber('2'), math.fraction(1,3)), math.fraction(2,3));
  assert.equal(multiply(math.fraction(1,3), math.bignumber('2')), math.fraction(2,3));
});
@josdejong

This comment has been minimized.

Owner

josdejong commented Sep 12, 2016

Thanks for bringing this up.

This is quite funny. Currently, there is no relation defined in math.js to convert Fractions to BigNumbers or vice versa, since it's ambiguous whether you would like to convert to one or the other. But the internally used typed-function is so clever that it found a conversion that is defined for both: namely converting both to a Complex number :D. Not really the intention.

To solve this, we have the following options:

  1. Always convert to BigNumber when using mixed BigNumbers and Fractions
  2. Always convert to Fraction when using mixed BigNumbers and Fractions
  3. Don't convert, throw an error. Force the user to convert to either BigNumber or Fraction
  4. Make it configurable whether to convert to BigNumber or Fraction

I think option 1 (convert to BigNumber) would be the best default choice. If you want to have a Fraction as output, you can do so by first converting all arguments to Fractions.

EDIT: there is an option 4, making this behavior configurable

@gitKad

This comment has been minimized.

gitKad commented Sep 12, 2016

I need more information about how mathjs behave before I can share which options makes more sense for my use case. Would the bignumber type be able to convert to exact fractions involving large numbers numerators or denominators?

Either way, wouldn't the following be a fourth acceptable option?

  • Either convert to BigNumber or Fraction based on math.config.number
@josdejong

This comment has been minimized.

Owner

josdejong commented Sep 12, 2016

It indeed depends on the use case what's desired behavior.

  • Converting a Fraction to BigNumber would simply create a BigNumber like math.divide(math.bignumber(num), math.bignumber(den)). You lose your exact representation.
  • Converting a BigNumber which cannot be represented as a Fraction to a Fraction will simply replace the 64 digit precision with a regular number of ~16 digits which goes into the numerator, having a denominator of 1. You lose your high precision.

Both aren't nice solutions, that's why we originally didn't want to choose one or the other and throw an error instead. Maybe option 3 is still the best.

@josdejong

This comment has been minimized.

Owner

josdejong commented Sep 12, 2016

There is another interesting discussion here: #694, which is about being able to have the num/den of a Fraction be represented with a BigNumber, and to be able to use BigNumbers and Fractions as the re/im parts of Complex values and the values of Units.

@gitKad gitKad changed the title from multiply _bignumbers_ and _fractions_ to multiply bignumbers and fractions Sep 12, 2016

@josdejong josdejong changed the title from multiply bignumbers and fractions to Configure implicit conversion between bignumbers, fractions, and numbers Nov 5, 2016

@josdejong josdejong added the feature label Nov 5, 2016

@josdejong

This comment has been minimized.

Owner

josdejong commented Nov 5, 2016

I updated the title of this issue. I think what we should do here is:

  • Configure implicit conversion between bignumbers and fractions, choose from:
    • none (defaul) don't convert from BigNumber to Fraction or vice versa but throw an error
    • bignumber: convert Fractions to BigNumbers
    • fraction: convert BigNumbers to Fractions
  • Configurable conversion from number to BigNumber, with the following options:
    • convert numbers to BigNumbers, throw an error when the number has >15 digits (number is rounded already).
    • convert numbers to BigNumbers, never throw an error
    • convert BigNumbers to numbers
    • don't convert numbers and BigNumbers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment