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

This commit is contained in:
jos 2016-11-05 19:44:04 +01:00
parent 4306200c4c
commit c9591339b1
3 changed files with 13 additions and 3 deletions

View File

@ -33,9 +33,7 @@ math.eval('0.1 + 0.2'); // BigNumber, 0.3
The default precision for BigNumber is 64 digits, and can be configured with
the option `precision`.
*Important:
BigNumber is not supported by all functions, like `arg`, and `random`.
These functions will downgrade BigNumber to Number, and return a Number.*
Note that BigNumbers are not supported by all functions.
## Round-off errors

View File

@ -80,6 +80,13 @@ exports.create = function create(type) {
convert: function (x) {
return new type.Complex(x.toNumber(), 0);
}
}, {
from: 'Fraction',
to: 'BigNumber',
convert: function (x) {
throw new TypeError('Cannot implicitly convert a Fraction to BigNumber or vice versa. ' +
'Use function bignumber(x) to convert to BigNumber or fraction(x) to convert to Fraction.');
}
}, {
from: 'Fraction',
to: 'Complex',

View File

@ -59,6 +59,11 @@ describe('multiply', function() {
assert.throws(function () {multiply(bignumber(1).div(3), 1/3);}, /Cannot implicitly convert a number with >15 significant digits to BigNumber/);
});
it('should throw an error when multipling mixed fractions and bignumbers', function() {
assert.throws(function () {multiply(math.bignumber('2'), math.fraction(1,3))}, /Cannot implicitly convert a Fraction to BigNumber/);
assert.throws(function () {multiply(math.fraction(1,3), math.bignumber('2'))}, /Cannot implicitly convert a Fraction to BigNumber/);
});
it('should multiply mixed booleans and bignumbers', function() {
assert.deepEqual(multiply(bignumber(0.3), true), bignumber(0.3));
assert.deepEqual(multiply(bignumber(0.3), false), bignumber(0));