-
-
Notifications
You must be signed in to change notification settings - Fork 732
abs() does not work on floats #362
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
Comments
The documentation is unclear on data types anyway. I do not know the fabs method, but this concern seems reasonable. @mastrolinux: Do you have a suggestion here? |
@cmaglie please look into this one |
I was led here from this Reddit discussion ... really nothing to add except that 'Calculates the absolute value of an integer.' might be clearer. Or perhaps 'returns, as an integer, the absolute value of the argument' which is more accurate. |
I plan to go through the entire Arduino Language Reference and document all parameter and return types. I think that will clear up the confusion about As for |
I took a closer look into this issue and found that it's a bit complicated. The standard C++ and C implementations of #define abs(x) ((x)>0?(x):-(x)) which does work with any type. This macro will be used in all .ino files, and any other source file that Out of the official Arduino hardware cores, the exception to this is Arduino megaAVR Boards, which does not define its own I also checked the most popular 3rd party hardware cores and found that Teensy's Arduino's official documentation can't be expected to cover 3rd party projects. Those projects must provide their own documentation for any differences from the official cores. I'm not sure why Arduino made the decision to depart from the convention they established with all their other hardware cores when it came to Arduino megaAVR Boards. I'll have to be sure to document that in the Arduino Language Reference's |
This should be documented on the page. And you're doing a great job by the way ;) |
I reported a similar issue in the espressive repo - it finally turned out that is was because of an abs() issue, too: IMO it's better to use |
I would recommend adding fabs() to the reference page. Long term, Arduino can address it in some other better yet portable way like creating its own proprietary Arduino math functions with different names that can handle any type. @per1234 I think you need a qualifier to your statement:
because I believe that this issue is actually something a little bit different in that, in this case, It isn't the 80's anymore where developers often wrote these type of macros to replace system provided functions in order to gain speed and/or potentially reduce code size. Today's compilers are way better at doing optimizations than simple macros could ever do and compilers now have special built in optimizations for these math functions. A more common way to handle these macro re-definitions was to use a different name with all caps being the more common one. IMO, if Arduino wants to create its own proprietary Arduino math functions that can handle any type that is perfectly ok, as long as they don't name them the same as a standard C math function and override the standard C function, especially when it doesn't work the same way or even has a bug in it. So, IMO, I think it is crazy for Arduino to silently override any standard C math function that has existed for 50 years with a version that works differently and has a bug in it. There are other examples of where Arduino has decided to go the proprietary route (which I also disagree with) but at least they did it without overriding standard functions, types, macros. Then there is all Arduino proprietary WCharacter functions where Arduino came up with new names for all the standard ctype functions. Seemed silly to me, but at least they didn't override any of the standard ctype functions to work differently. The goal for Arduino is to create a portable programming environment. In the future if Arduino wants a simpler easier to use solution that doesn't require different functions for different argument types, it should come up with its own functions that work on any type. |
To add a bit more complexity to the issue. As of C++ 17, the standard functions like abs(), min(), max() now support any type. So as soon as the platforms use newer compiler tools in their cores, they can remove these silly math macros and get multi type support from the standard functions (at least when using C++) |
as to C++17: when will this be a standard in the Arduino IDE? as to
actually nothing exists like an "Arduino Language". Arduino API code is nothing else than C++ wrapped by some "convenient" and "handy" C++ API libs, so it's still C++, including all language documentations, references, and standards, and so are expected to be compatible to each other. |
I'm not sure how to rewrite the page, so I rather write it here as issue instead of pull-request.
abs() does not work on floats, because it returns an int. It does not say so in the documentation at https://github.com/arduino/reference-en/blob/master/Language/Functions/Math/abs.adoc
Apparently for floats there exists fabs(), but this is not written in the arduino ref docs.
Should this be added, or is there some reason for this information missing?
The text was updated successfully, but these errors were encountered: