New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add gcd
/lcm
functions
#73
Conversation
Hmm, what's the proper way to throw a non-terminating error from a |
This is a great addition!
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 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. |
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great! Would be good if you could also run rustfmt
on the file (this should've been mentioned in the README, I'll add that now). 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?
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?)
|
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! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The "0" constants need to be specified as f64 for it to work when not using rug
. Eg. x.value.clone().fract() != 0f64
. You can test this by running something like cargo test --no-default-features
I believe.
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: