PaddiM8 / kalker

Scientific calculator with math syntax that supports user-defined variables and functions, complex numbers, and estimation of derivatives and integrals
https://kalker.xyz
MIT License
1.59k stars 70 forks source link

Slightly inaccurate when integrating over floor/ceil. #30

Open fwip opened 3 years ago

fwip commented 3 years ago

I'm not sure if this is a known limitation or something that should be an issue. I suspect it applies to all step-wise functions but I haven't checked.

>> ∫ (0, 1, ⌈x⌉, dx)
0.9995833333

This should be 1 exactly, but I suspect that the code falls back to numerical integration in these cases.

More examples:

>> ∫ (1/10000, 1, ⌈x⌉, dx)
0.9999 # correct
>> ∫ (1/100000, 1, ⌈x⌉, dx)
1  # should be 0.99999
>> ∫ (1/1000000, 1, ⌈x⌉, dx)
1 # should be 0.999999
>> ∫ (1/10000000, 1, ⌈x⌉, dx)
1.0004165666   # over one? should be 0.9999999
PaddiM8 commented 3 years ago

Well, integration will often not be perfectly accurate, since functions can really vary a lot. Sometimes it's spot on, sometimes it's not. in this case, it looks like it's related to floating point numbers not being perfectly accurate, since it gets less accurate when you have numbers like 1/100000. This is the kind of thing I'm interested in making more accurate though, so this is a great example! It appears to be just slightly better (for me at least) on the desktop version, that is better at dealing with very large/very small numbers. Eventually, I'd like this to be the case for the web version as well, but the language it's written in (Rust) seems to lack a mature library for this.

fwip commented 3 years ago

Makes sense, thank you for the quick reply! Please feel free to close this issue if it is not useful to you to have it open. :)

MitsuhaMiyamizu commented 3 years ago

Hi, I encountered the same result just like this one

Given ∫(-2,2,(√(4-x^2))*(1/2+x^3*cos(x/2))dx)

We suppose to get the result pi, which is 3.1415926535897 However, I got 3.1415340713 using Kalk

Is there something that can be improved in the future? Many thanks.

PaddiM8 commented 3 years ago

Hi, I encountered the same result just like this one

Given ∫(-2,2,(√(4-x^2))*(1/2+x^3*cos(x/2))dx)

We suppose to get the result pi, which is 3.1415926535897 However, I got 3.1415340713 using Kalk

Is there something that can be improved in the future? Many thanks.

Ah. Well, when it comes to more "complicated" ones like this, it may be difficult to get more accuracy. It can always be tweaked though, and maybe made more flexible. I'll look into it!

timokoesters commented 3 months ago

∫ (1/10000000, 1, ⌈x⌉, dx) 1.0004165666 # over one? should be 0.9999999

I can't reproduce this, it says ≈ 1 for me.

I think a good solution to this problem would be to add an optional parameter for the integrate function to set the accuracy.