-
-
Notifications
You must be signed in to change notification settings - Fork 359
Fixing the euclid alg to allow for negative inputs #14
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
Conversation
This seems like an unnecessary change. This isn't meant to be production code but example code for people to try and understand the concepts and by adding the abs() it lengthens the code / makes it slightly harder to read. |
I'am on your side there. I think we have to mark the lines of code for handling the negative values clearly or provide two versions of the functions. |
Yeah, I think adding an abs() doesn't make things too confusing and serves a purpose. I do agree with KingFredrickVI in general, though. Code cleanliness is most important here. I think I'll merge these two in this case. |
@kingfredrickvi Just to clarify something since we talked about this in length over on Discord: because of some C/C++ idiosyncrasies the behavior of the one without int main() {
return -10 % -7;
} The compiler is free to translate this to the following: int main() {
return 0;
} I think avoiding hard to debug and non-obvious mistakes is a nice goal in a public algorithm collection. |
I agree with unnecessary change. Example code is best kept as clean and concise as possible. abs should do the trick. |
@Gustorn I don't believe that's true of any modern versions of C or C++. Also, implementation defined is quite different from unspecified (is quite different from undefined). Implementation defined means it must follow a specific set of rules - in this case:
(C89 draft standard) Let's go through it with your example - Well, the two valid answers to the question
Let's look at the equation for finding the remainder operator, given
given given Therefore, the only two valid answers to C++11, C99, and later standards fix this, so that integer division must round towards zero - thus, side note: this is why I called the Rust version |
@ubsan You are right, for some reason I thought the return value would be unspecified even though I rembered that it was implementation-defined. (and yes, I know the difference between undefined/unspecified/implementation-defined). Unfortunately it still has the ability to mess with the algorithm without the |
@Gustorn yeah, but in a completely defined way 😄 I'm assuming C99/C++11 |
No description provided.