Skip to content

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

Merged
merged 3 commits into from
Nov 2, 2021
Merged

Conversation

mkaivo
Copy link
Contributor

@mkaivo mkaivo commented Jul 16, 2021

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

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
@CLAassistant
Copy link

CLAassistant commented Jul 16, 2021

CLA assistant check
All committers have signed the CLA.

@mkaivo mkaivo changed the title Add an example code to abs() CNT-1196 Add an example code to abs() [CNT-1196] Jul 16, 2021
Copy link
Collaborator

@per1234 per1234 left a 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
}

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.

@mkaivo
Copy link
Contributor Author

mkaivo commented Aug 13, 2021

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

Hi! I have updated the example sketch with your example. Thanks

@mkaivo mkaivo requested a review from per1234 August 13, 2021 13:51
Copy link
Collaborator

@per1234 per1234 left a 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.

Copy link
Collaborator

@per1234 per1234 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @mkaivo!

@per1234 per1234 merged commit afdbba3 into arduino:master Nov 2, 2021
@per1234 per1234 mentioned this pull request Aug 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants