Skip to content
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

Use std::abs for a float-compatible abs() function #2738

Merged
merged 1 commit into from
May 11, 2019

Conversation

projectgus
Copy link
Contributor

@projectgus projectgus commented May 3, 2019

This fix tries to keep in the spirit of #1783 by using libstdc++ rather than a macro, for the same effect (abs can take any compatible type). The other option would be to include <cstdlib> before defining the abs() macro, so it doesn't get undef-ed again later on.

This will probably also fix #2128 as the abs() macro is no longer defined.

* Other Arduino cores uses a macro to redefine libc abs() to take any
  type, meaning abs(-3.3) == 3.3 not the normal libc result of 3.

* 1e4bf14 (espressif#1783) replaced similar min, max macros with c++ stdlib. However
  this change includes <algorithm> after the line which defines the abs() macro.
  <algorithm> includes <cstdlib> which undefines abs() and re-defines it.

* This means abs() becomes the plain libc version again which only takes
  integers, so abs(-3.3) == 3. As reported here:
  espressif/esp-idf#3405

This fix tries to keep in the spirit of espressif#1783 by using libstdc++. The other
option would be to include <cstdlib> before defining the abs() macro, so it
doesn't get undef-ed again later on.
@ghost
Copy link

ghost commented May 4, 2019

@projectgus My apologies. I deleted my last comment, as I wrongly assumed that the change to using the standard library wouldn't work with floats.

@projectgus
Copy link
Contributor Author

@MartinL1 No worries, I oversimplified a long story in the PR summary so it's not immediately clear to someone who hasn't followed the whole saga. :) I edited it a bit to try and make it clearer what this change does, vs what was a regression in earlier changes.

@me-no-dev
Copy link
Member

This has been PITA for a loong time. As far as I remember, we have tried this before and libraries broke.
Fingers crossed 🤞

@me-no-dev me-no-dev merged commit 2f249ed into espressif:master May 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Arduino.h: error on abs macro definition
2 participants