Qalculate / libqalculate

Qalculate! library and CLI
https://qalculate.github.io/
GNU General Public License v2.0
1.82k stars 146 forks source link

Eats all RAM with calculation with huge result #16

Closed kbroulik closed 7 years ago

kbroulik commented 7 years ago

When I try to calculate the following

2^2^2^2^2^2^2

it starts to eat all RAM until the application is killed. (Using libqalculate 0.9.7-9.1)

Downstream bug report: https://bugs.kde.org/show_bug.cgi?id=277011

hanna-kn commented 7 years ago

I consider it foremost an issue (there are multiple others) with how krunner uses libqalculate. The calculator runner should use the libqalculate functions the stops slow RAM-eating calculations after a specified amount of time.

numsim1415 commented 7 years ago

Firstly: I agree with @hanna-kn that krunner should terminate libqalculate calls after a timeout.

Yet, Note: This memory consuming bug occurs even with simple, everyday calculations! 2^0.333333333 The memory issue triggers only when using at least 9 digits after the comma. Such calculations are not extraordinary and no user would expect this to freeze his system.

@hanna-kn Can you confirm that the latest version of libqalculate has an issue with 2^0.333333333 ? PS: I guess i had to install libxml2-dev (debian) so autogen is happy. I tried to build but then it fails because at the configure stage it forgot to warn be about the cln dependency.

@hanna-kn Question: Is there a switch that krunner could use such that normal double precision calculations are performed when calculations are going to take (too) long?

hanna-kn commented 7 years ago

If exact or 'try exact' mode is used 2^0.333333333 is calculated as (2^333333333)^(1/1E9), which for obvious reasons is slow (although the result is returned after less then four seconds on my computer). In approximate mode (perhaps most appropriate for krunner) the result is returned practically instantaneously.

hanna-kn commented 7 years ago

I have now fixed slow calculation of exponentiations in exact and "try exact" modes. When exact calculation fails the exponent is now no longer split up in numerator and denominator if the numerator is larger than 10 000 (or smaller than -10 000).

numsim1415 commented 7 years ago

@hanna-kn Just tested the change. Works great. Thank you very much!

edit: @hanna-kn I forgot the ./ in ./qalc facepalm. Thats why it showed the old behaviour.

hanna-kn commented 7 years ago

@numsim1415 No, that is not expected. With the change, no matter how many 3's I enter the result is returned instantaneously.

hanna-kn commented 7 years ago

Qalculate! will now realize that 2^2^2^2^2^2^2 (= 2^(2^(2^(2^(2^(2^2)))) = 2^(2^2,0035299E19728)) is ridiculous and not try to calculate it.