josdejong / mathjs

An extensive math library for JavaScript and Node.js
https://mathjs.org
Apache License 2.0
14.31k stars 1.24k forks source link

BigNumber, simplify, and exactFractions don't interoperate well #2816

Open nycos62 opened 1 year ago

nycos62 commented 1 year ago

Hello,

is there any chance to use bigNumbers and simplify ?

my config is this :

math.config({
    number: 'BigNumber',      // Default type of number:
                              // 'number' (default), 'BigNumber', or 'Fraction'
    precision: 509             // Number of significant digits for BigNumbers
  }) 
const f = math.parse('2/3+3');
const simplified = math.simplify(f,{},{exactFractions:true});
console.log(simplified.toString());

I get : 3.6666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666667

the correct answer should be : 11 / 3

if I use this config, I loose my precious bigNumbers and fall on number format which I want to avoid

math.config({
    number: 'number',      // Default type of number:
                              // 'number' (default), 'BigNumber', or 'Fraction'
    precision: 509             // Number of significant digits for BigNumbers
  })
math.simplify('(3/2)^20').toString()
'3.486784401e+9 / 1.048576e+6'

If only I could get this format with bigNumbers :'(

math.simplify('(3/2)^20').toString()
'3486784401 / 1048576'

Am I missing something ? I haven't found any details in the documentation about simplify with bigNumbers

Kind regards, Nicolas

josdejong commented 1 year ago

math.js uses the Fraction.js library to work with fractions. This library only supports numbers. It has no support for BigNumbers. It would be nice if it could support BigNumbers though :).

If anyone is interested in looking into support for Fractions+Bignumbers please let me know.

nycos62 commented 1 year ago

did you see there is a BigFraction type ? max int => BigInt(9007199254740991);

I don't know how to handle bigNumbers into Fractions.js because the type BigNumbers is not included in Fraction.js package :( https://github.com/infusion/Fraction.js/blob/master/bigfraction.js

josdejong commented 1 year ago

Ah, maybe I've seen it in the past but I didn't remember at least. That is a very interesting idea! I guess we first have to implement support for BigInt and then can implement utilizing that with BigFraction.

See the discussion about BigInt: #2737

nycos62 commented 1 year ago

I guess we should work hard to turn basic Number type of math.js into infinite integers as in pyhton. you declare any integer, you don't care of the length/type of integer, it handle it. You could also, by the way, use simplify to turn every floating number into almost infinite integer fractions depending on your device settings.