Skip to content

abs function incorrect return description #3338

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

Closed
Netmisha opened this issue Jun 15, 2015 · 2 comments
Closed

abs function incorrect return description #3338

Netmisha opened this issue Jun 15, 2015 · 2 comments
Assignees
Labels
Component: Documentation Related to Arduino's documentation content Type: Bug Type: Invalid Off topic for this repository, or a bug report determined to not actually represent a bug
Milestone

Comments

@Netmisha
Copy link

http://www.arduino.cc/en/Reference/Abs

Returns
x: if x is greater than or equal to 0.
-x: if x is less than 0.

Should be:
Returns
x: if x is greater than or equal to 0.
x: if x is less than 0.

@ffissore ffissore added the Component: Documentation Related to Arduino's documentation content label Jun 15, 2015
@ffissore ffissore added this to the Release 1.6.6 milestone Jun 15, 2015
@agdl
Copy link
Member

agdl commented Jun 25, 2015

@Netmisha no it is correct because if it is less than zero -X is a positive number and this is what you get. It is intended in this way

@agdl agdl added the Waiting for feedback More information must be provided before we can proceed label Jun 26, 2015
@agdl agdl closed this as completed Jun 26, 2015
@bperrybap
Copy link

Actually, @Netmisha has a valild point. But the description on the reference page is correct, it is the macro that has the issue.
The macro in Arduino.h is this:

#define abs(x) ((x)>0?(x):-(x))

Technically it should be is:

#define abs(x) ((x)<0?-(x):(x))

The reason being is that there really a value of -0 and -0 is not the same value as 0
so the sign inversion should only be done for actual negative values.

In the bigger picture, why does Arduino still have these math function defines?
While this was a optimization trick back in the 80's or even 90's to avoid calling a function, gcc for quite some time has implemented these as an inline functions so there is no longer any value in using a macro for things like abs() and the macro comes with some pitfalls that can break some code.

@cmaglie cmaglie removed the Waiting for feedback More information must be provided before we can proceed label Oct 18, 2016
@per1234 per1234 added the Type: Invalid Off topic for this repository, or a bug report determined to not actually represent a bug label Aug 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Documentation Related to Arduino's documentation content Type: Bug Type: Invalid Off topic for this repository, or a bug report determined to not actually represent a bug
Projects
None yet
Development

No branches or pull requests

6 participants