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

Add `gcd`/`lcm` functions #73

Closed kiedtl closed 2 years ago

kiedtl commented 3 years ago

This PR adds the gcd and lcm functions.

>> gcd(12, 18)
6
>> lcm(12, 18)
36

GCD is calculated with the modified Euclidean algorithm, and LCM is calculated with:

             ⎛           ⎞
             ⎜    ⎜a⎜     ⎟
lcm(a, b) =  ⎜ ───────── ⎟ × ⎜b⎜
             ⎜ gcd(a, b) ⎟
             ⎝           ⎠
kiedtl commented 3 years ago

Hmm, what's the proper way to throw a non-terminating error from a function?

PaddiM8 commented 3 years ago

This is a great addition!

Hmm, what's the proper way to throw a non-terminating error from a function?

Hmm, calculator functions (like the ones you made) currently don't have a lot of options for error handling. It is assumed that any function will return a value. However, I believe some functions simply return NaN when a calculation isn't possible. You could do something like KalkNum::from(f64::NAN), I believe.

Now that I think about it, being able to throw error messages from inside these functions should probably be possible in the future. Then errors would be thrown by returning eg. CalcError::ErrorType(info) like in the rest of the code.

kiedtl commented 3 years ago

Now that I think about it, being able to throw error messages from inside these functions should probably be possible in the future. Then errors would be thrown by returning eg. CalcError::ErrorType(info) like in the rest of the code.

Definitely, it's better UX to throw an informative error message instead of silently returning NaN, which could be annoying to debug if the gcd/lcm call was part of a larger calculation/function call. However, I'll use the NaN solution for now :)

kiedtl commented 2 years ago

Alright, this is ready for review.

kiedtl commented 2 years ago

Oops, wrong button

kiedtl commented 2 years ago

On 21-10-05 07:14 , PaddiM8 wrote:

Also, I noticed that, when you give it complex numbers, it differs a bit from what eg. WolframAlpha and MathWorks are saying. It gives a result like 10 - 5i while WolframAlpha says 5 + 10i. Would the latter maybe be more of an expected result by users?

I'm honestly not sure. I had spent some time trying to wrap my head around number domains and other number theory stuff, but then gave up and copied something from SO, which I'm only beginning to feel like I understand. I do know, though, that there can be multiple factorizations for polynomials, so I'm assuming that both of those answers are correct... but I don't know which one would be preferred. (Could you double-check that the answer returned by my algorithm is correct and not an edge-case?)

PaddiM8 commented 2 years ago

I'm assuming that both of those answers are correct... but I don't know which one would be preferred.

Yeah this is what I'm thinking as well. When comparing to WolframAlpha, it seems to consistently give the same numbers, but often with the real and imaginary ones swapped, and the sign inverted. From what I can tell, it seems to give correct answers though, so it's probably good enough for now at least!

kiedtl commented 2 years ago

Alright, working now

PaddiM8 commented 2 years ago

Nice! This looks good now, I'll go ahead and merge it. Excited to have this in kalker.

PaddiM8 commented 2 years ago

These can now be used on https://kalker.xyz!