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.64k stars 74 forks source link

Inconsistent sqrt syntax #139

Open timokoesters opened 6 months ago

timokoesters commented 6 months ago

√(2)×2 is 2√2, but √2×2 = √4 = 2

I think multiplication should always be parsed as being outside of the sqrt.

PaddiM8 commented 6 months ago

Hmm this is a tricky one, because you would expect something like cos2x to be parsed as cos(2x) but sqrt2*2 as sqrt(2) * 2 I guess. Maybe it should make a distinction between implicit and explicit multiplication in this context? That's what Wolframalpha seems to do.

Your issues have been really helpful by the way. Thanks!

timokoesters commented 6 months ago

Even better, WolframAlpha differentiates between cos 2 x and cos 2x. I think leaving a space is the same as explicit multiplication

PaddiM8 commented 6 months ago

Oh yeah that makes sense. A bit trickier to parse, but should be possible

Kuuuube commented 2 months ago

I think a bigger issue here is this:

√2*2 = √(2*2) = 2

but then also this:

√2+2 = √(2)+2 = 3.41...

The sqrt should either be greedy and capture any operation after it or capture nothing after it (unless theres parentheses wrapping everything under the sqrt) not both at the same time depending on the operator.

For example this is how I think they should be parsed:

√2*2 = √(2)*2 = 2.82...
√2+2 = √(2)+2 = 3.41...

and require the user to add parentheses for these operations to be pulled into the sqrt:

√(2*2) = 2
√(2+2) = 2