-
-
Notifications
You must be signed in to change notification settings - Fork 732
Add an example code to abs() [CNT-1196] #841
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
Hi, we got a request to add an example code of abs(). Could this work? It is a pretty straightforward function. See the Jira task here: https://arduino.atlassian.net/browse/CNT-1196
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.
I don't find this to be a useful demonstration of abs()
. The result is most interesting when applied to negative numbers, but it will take a long time for a
to overflow to a negative number. The mixture of int
and float
types is also odd.
If you want a standalone sketch, I would suggest something more straightforward like this:
void setup() {
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
int x = 42;
Serial.print("The absolute value of ");
Serial.print(x);
Serial.print(" is ");
Serial.println(abs(x));
x = -42;
Serial.print("The absolute value of ");
Serial.print(x);
Serial.print(" is ");
Serial.println(abs(x));
}
void loop() {
}
or perhaps just a simple on-liner as is done for some of the other math functions:
https://www.arduino.cc/reference/en/language/functions/math/max/#_example_code
a++; //Adds one to the variable a | ||
absolute = abs(a); //Stores the absolute value of the variable a to the variable absolute | ||
Serial.println(absolute); //Prints to the Serial Monitor the value of the variable absolute | ||
} |
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.
@mkaivo maybe we can add a delay in the end.
Hi! I have updated the example sketch with your example. Thanks |
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.
Hi. I apologize for my slow response. Just one more small suggestion.
Co-authored-by: per1234 <[email protected]>
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.
Thanks @mkaivo!
Hi, we got a request to add an example code of abs(). Could this work? It is a pretty straightforward function. See the Jira task here: https://arduino.atlassian.net/browse/CNT-1196