What is the error in this code?

I am trying to make a IdeaSpark weather station work. I have installed all libraries and etc per instructions. When I try to compile, I get the following error. I have included the code that causes the error. Can someone point out the error and how to fix it?

Here is the error

C:\Users\hamil\Documents\Arduino\libraries\esp8266-weather-station-master\src\TimeClient.cpp: In member function 'long int TimeClient::getCurrentEpochWithUtcOffset()':

C:\Users\hamil\Documents\Arduino\libraries\esp8266-weather-station-master\src\TimeClient.cpp:124:67: error: invalid operands of types 'double' and 'long int' to binary 'operator%'

return round(getCurrentEpoch() + 3600 * myUtcOffset + 86400L) % 86400L;

Here is the erroneour routine

long TimeClient::getCurrentEpochWithUtcOffset() {
return round(getCurrentEpoch() + 3600 * myUtcOffset + 86400L) % 86400L;
}

Thanks for the help.

The error is saying you are using a type of 'double' with the modulus operator, but since you didn't show the entire sketch, only you will know.

Possibilities are:

  • that the function getCurrentEpoch() returns a double [probably not]
  • myUtcOffset is a constant that is of type double
  • the function round() returns a type of double [most likely cause]

MyUtcOffset is a float
GetCurrentEpoch is a Long
Round is an integer (per google)

So it looks like I am adding a float to an long, rounding to an integer, doing the mod thing, and returning a long. The function returns a long.

Seems OK to me. What say you??

MyUtcOffset is a float

This causes the other variables to be promoted to float, which round also returns, and the result is incompatible with mod.

But round returns integer, per google.

OK, float Plus integer = float. Right
Round (float) = integer
integer %integer = integer

integer is returned.

I guess I will have to do the change things and try method to figure out the issue.

In C/C++ round() returns a float; the closest integral value to the input argument (i.e. the result has no fractional component).

However, I cast the term myUtcOffset to be a long as shown below and it compiles. I don't get it.

long TimeClient::getCurrentEpochWithUtcOffset() {
return round(getCurrentEpoch() + 3600 * (long)myUtcOffset + 86400L) % 86400L;
}

The above seems a bit risky so I cast the round function as a long. It compiles but I have other issues.