josdejong / mathjs

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

format fraction to a decimal string. dont work with large numbers? #604

Closed kostja250581 closed 2 years ago

kostja250581 commented 8 years ago

Just make a test. Create a fraction number with a size over 10^21.

Than format it first with: math.format( number, { fraction: 'ratio' } ) ); The result will be: 1e21 / 1

Make the same with: math.format( number, { fraction: 'decimal' } ) ); The result will be: 10000

After 10^21 you can type any exponent you wont, like 10^50. The rerult is 10000!

ericman314 commented 8 years ago

@infusion, this seems to be an issue with fraction.js here.

When N is greater than about 1e21, String(N) returns the number in exponential notation: 1e+21. Since this is only 5 characters, (now I haven't digged into the details too much, so I might be wrong), perhaps the fraction.toString function only expects to output 5 characters, hence the result 10000.

infusion commented 8 years ago

@ericman314, you're right. It's the String(N) function which causes the problem. Unfortunately, I have no idea how to fix this without a digit-wise loop for such large numbers. toFixed(k) works only for k<=20. But I wonder if we really should treat numbers like this, as it is way beyond Number.MAX_SAFE_INTEGER.

kostja250581 commented 8 years ago

I'm working on a calculator. Users don't like apps, where the input freedom is limited.

infusion commented 8 years ago

Hm, absolutely, but computers don't like infinitely large numbers. We discussed it already to extend rational numbers into the BigInt area, but haven't worked on it yet. If you really want to support this precision for your app, I would use math.js fractions until a certain size is reached and fall back to a math.js decimal implementation with an own rational arithmetic logic. Problems of this scale usually can't be handled with a one-size-fits-all solution. I don't know what calculations your app is all about, but 10^60 is quite a lot and I wonder if this boundary faces a real use case.

kostja250581 commented 8 years ago

Your right, Infinity is a little bit to mutch. :-)

But it is a Calculator for students and engineers, 10^-60 until 10^60 is regular.

infusion commented 8 years ago

What mathematical operands are you about to support? And how do you support non-rational/fractional numbers?

kostja250581 commented 8 years ago

Just have a look. :-)

Smartmath 7.0

7.5

The calculator have type steps. First initialisation is in fractions. If one operation set a fraction to a bignumber/number. All following numbers are bignumbers. One of the current problems is, converting an infinity fraction to a big number.

infusion commented 8 years ago

This is really cool! I was thinking of yet another calculator app, but this is really beneficial! I think learning platforms could really profit from a system like that and if you would add a latex export, it would be useful to quickly formulate equations quite easily with it in the academic world. And math.js has a good latex export already. I don't know what you're planning to do with it, but would love to see a website, where something like that would be possible without registration - just as in the demo video.

Another cool addition would be complex numbers.

Anyway. The precision problem remains, and it will always be there as long as you don't bet on inf-precision libraries. As we don't have a general solution yet - and inf-precision isn't necessary in every use case, especially when performance matters - I would suggest to go the hard way, take fraction.js (which is the base of math.js rational number arithmetic) and replace every basic algebraic operation (+, -, , /, %, Math.) with bigint equivalents. This should be a job of 10min. Here is the lib: https://github.com/infusion/Fraction.js

kostja250581 commented 8 years ago

Thank you! :-) I like it too.

There will be a converter.

Smartmath -> Latex Smartmath -> Word and and and

This app is more a collection, of apps. The calculator is just the first one. The second will be something like Smart-Matrices, the third Smart-Grapher and more.

I think, i put some parts of the app in the web. But it is written for tablets and smartphones.

Today I'm busy with the GUI of Smartmath. It is not easy, write it all on my own! Mathjs import is the last step for Smartmath. I just tell josdejong my ideas and write the issues. Because he ask me to do that. :-)

josdejong commented 8 years ago

@infusion what is the status of this formatting issue?

infusion commented 8 years ago

I'd declare it a wont-fix as it is a feature, which can't be handled with normal number arithmetic. Deciding to do it with a big decimal/integer library is a different project. For the calculator app, I'd implement the few rational operations based on math.js decimal.

josdejong commented 8 years ago

Would it be possible to throw an error instead of returning invalid output?

josdejong commented 2 years ago

Closing this discussion now because it has been inactive for a long time. Feel free to reopen if needed.