Round(int) is ambiguous when including math.h

Sorry for asking such a basic question, but I needed to use pow(), which means I had to #include math.h.

That seemed to work fine, however now many places I am getting the following error:

“more than one instance of overloaded function “round” matches the argument list”.

I see that cmath and SPARK_WIRING_CONSTANTS both define several math functions. I am rather new to c++ and am not sure how to address this.

Thank you!

What is the exact error you are getting, and what is the variable type of the input parameter?

#include <math.h> is the correct thing to do to get pow() and fpow().

spark_wiring_constants.h does not define pow(). And it only defines min, max, constrain, and round when Arduino compatibility mode is enabled, and that defaults to off.

Code can be as simple as:
Count = round(3 / 2);

call of overloaded ‘round(int)’ is ambiguous gcc

more than one instance of overloaded function “round” matches the argument list: – function template “T round(T x)” (declared at line 57 of “/Users/Ryan/.particle/toolchains/deviceOS/3.1.0/wiring/inc/spark_wiring_constants.h”) – function template “__gnu_cxx::__enable_if<std::__is_integer<_Tp>::__value, double>::__type std::round(_Tp __x)” (declared at line 1768 of “/Users/Ryan/.particle/toolchains/gcc-arm/10.2.1/arm-none-eabi/include/c++/10.2.1/cmath”) – argument types are: (int)C/C++(308)

What is the rationale of trying to round a number that already is rounded by definition?
Round only ever makes sense when your parameter is a floating point type.
Hence there is no definition of round() that takes any integer type.

Just a hint on that 3 / 2: This will always be calculated as 1. No matter whether you use round() or not.

A pure int division will always render an int result.

At least one of your parameters of the division operator needs to be a floating point type to render a floating point result.
Since a pure int can be treated as float, double or long double the compiler is unsure what to use.

1 Like

Sorry, I was just trying to make it a simple example that also breaks.

The actual line is Count = round(PixelCount() / 2). But to your point, PixelCount() also returns an Int.

If I do this: Count = round(float(PixelCount() / 2), it now compiles fine. But, also to your point, it is unnecessary, as it will just round anyway because they are both ints correct?

Not quite.

A division 3.0 / 2 (float / int) will produce a floating point 1.5 and round(1.5) will give you 2.0, but a 3 / 2 (int / int) will produce an integer 1.

Mainly referring to PixelCount() and 2 being both ints. I am good to go now, thank you!

I gathered that, but (float)PixelCount() (i.e. (float)3 == 3.0) will be a float and for that rounding makes sense.

Count = round((float)PixelCount() / 2);
// or
Count = round(PixelCount() / 2.0);

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.