Closed kiedtl closed 2 years ago
Hmm, what's the proper way to throw a non-terminating error from a function?
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.
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 :)
Alright, this is ready for review.
Oops, wrong button
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 says5 + 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?)
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!
Alright, working now
Nice! This looks good now, I'll go ahead and merge it. Excited to have this in kalker.
These can now be used on https://kalker.xyz!
This PR adds the
gcd
andlcm
functions.GCD is calculated with the modified Euclidean algorithm, and LCM is calculated with: