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

Open
opened this Issue Sep 12, 2016 · 5 comments

Projects
None yet
2 participants

### 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)); }); ``````
Owner

### josdejong commented Sep 12, 2016 • edited

 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: Always convert to BigNumber when using mixed BigNumbers and Fractions Always convert to Fraction when using mixed BigNumbers and Fractions Don't convert, throw an error. Force the user to convert to either BigNumber or Fraction 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 commented Sep 12, 2016 • edited

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

### josdejong commented Sep 12, 2016 • edited

 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.

Owner

### josdejong commented Nov 5, 2016 • edited

 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

Closed

### josdejong added a commit that referenced this issue Nov 5, 2016

``` Throw an error for implicit conversion between BigNumber and Fraction… ```
`… (see #710)`
``` c959133 ```