ArduinoCore-API icon indicating copy to clipboard operation
ArduinoCore-API copied to clipboard

floor() and ceil() return integer values, not double (float)

Open tofrnr opened this issue 7 years ago • 5 comments

floor() and ceil() return integer values, not double (float), other than ANSI C math.h http://www.cplusplus.com/reference/cmath/ceil/ http://www.cplusplus.com/reference/cmath/floor/

(of course this issue is crucial especially for floats which are (absolutely) larger than the integer ranges)

Please make Arduino math functions comply to C standards, there is no need for divergent definitions!

tofrnr avatar Sep 10 '17 08:09 tofrnr

Related with https://github.com/arduino/Arduino/issues/6098

facchinm avatar Sep 11 '17 17:09 facchinm

As a workaround, use (floor)(x) and (ceil)(x), which will not use the macro but the actual function.

Alternatively, you can create a new tab in your Arduino project with a name ending in .cpp and put your pure C++ code there, with none of the Arduino.h nonstandard magic.

cousteaulecommandant avatar Dec 25 '17 18:12 cousteaulecommandant

IMO that's stupid to have to use cast functions as worksarounds, because floor() and ceil() are standard C math functions which are simply messed up by Arduino gibberish. So finally the Aduino gibberish has to be adjusted to standard C definitions, that's it, nothing else.

tofrnr avatar Dec 26 '17 16:12 tofrnr

...well, I did say it was a workaround, didn't I? :) In any case I think Arduino.h will eventually have to be purged from macros and instead use standard C++ functions, or templated functions when no alternative exists.

cousteaulecommandant avatar Dec 26 '17 20:12 cousteaulecommandant

I believe this is already fixed in the ArduinoCoreAPI repo. It does not define any ceil or floor macros, and looking through the history, it never has. It does include <math.h>, so I think that means it effectively makes the standard library versions available.

matthijskooijman avatar Oct 01 '20 09:10 matthijskooijman