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

Fix float literals and implicit conversions #213

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 30 additions & 30 deletions DHT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,18 @@ float DHT::readTemperature(bool S, bool force) {
case DHT11:
f = data[2];
if (data[3] & 0x80) {
f = -1 - f;
f = -1.F - f;
}
f += (data[3] & 0x0f) * 0.1;
f += (data[3] & 0x0f) * 0.1F;
if (S) {
f = convertCtoF(f);
}
break;
case DHT12:
f = data[2];
f += (data[3] & 0x0f) * 0.1;
f += (data[3] & 0x0f) * 0.1F;
if (data[2] & 0x80) {
f *= -1;
f *= -1.F;
}
if (S) {
f = convertCtoF(f);
Expand All @@ -110,9 +110,9 @@ float DHT::readTemperature(bool S, bool force) {
case DHT22:
case DHT21:
f = ((word)(data[2] & 0x7F)) << 8 | data[3];
f *= 0.1;
f *= 0.1F;
if (data[2] & 0x80) {
f *= -1;
f *= -1.F;
}
if (S) {
f = convertCtoF(f);
Expand All @@ -129,15 +129,15 @@ float DHT::readTemperature(bool S, bool force) {
* value in Celcius
* @return float value in Fahrenheit
*/
float DHT::convertCtoF(float c) { return c * 1.8 + 32; }
float DHT::convertCtoF(float c) { return c * 1.8F + 32.F; }

/*!
* @brief Converts Fahrenheit to Celcius
* @param f
* value in Fahrenheit
* @return float value in Celcius
*/
float DHT::convertFtoC(float f) { return (f - 32) * 0.55555; }
float DHT::convertFtoC(float f) { return (f - 32.F) * 0.55555F; }

/*!
* @brief Read Humidity
Expand All @@ -151,12 +151,12 @@ float DHT::readHumidity(bool force) {
switch (_type) {
case DHT11:
case DHT12:
f = data[0] + data[1] * 0.1;
f = data[0] + data[1] * 0.1F;
break;
case DHT22:
case DHT21:
f = ((word)data[0]) << 8 | data[1];
f *= 0.1;
f *= 0.1F;
break;
}
}
Expand Down Expand Up @@ -196,26 +196,26 @@ float DHT::computeHeatIndex(float temperature, float percentHumidity,
if (!isFahrenheit)
temperature = convertCtoF(temperature);

hi = 0.5 * (temperature + 61.0 + ((temperature - 68.0) * 1.2) +
(percentHumidity * 0.094));

if (hi > 79) {
hi = -42.379 + 2.04901523 * temperature + 10.14333127 * percentHumidity +
-0.22475541 * temperature * percentHumidity +
-0.00683783 * pow(temperature, 2) +
-0.05481717 * pow(percentHumidity, 2) +
0.00122874 * pow(temperature, 2) * percentHumidity +
0.00085282 * temperature * pow(percentHumidity, 2) +
-0.00000199 * pow(temperature, 2) * pow(percentHumidity, 2);

if ((percentHumidity < 13) && (temperature >= 80.0) &&
(temperature <= 112.0))
hi -= ((13.0 - percentHumidity) * 0.25) *
sqrt((17.0 - abs(temperature - 95.0)) * 0.05882);

else if ((percentHumidity > 85.0) && (temperature >= 80.0) &&
(temperature <= 87.0))
hi += ((percentHumidity - 85.0) * 0.1) * ((87.0 - temperature) * 0.2);
hi = 0.5F * (temperature + 61.0F + ((temperature - 68.0F) * 1.2F) +
(percentHumidity * 0.094F));

if (hi > 79.F) {
hi = -42.379F + 2.04901523F * temperature + 10.14333127F * percentHumidity +
-0.22475541F * temperature * percentHumidity +
-0.00683783F * powf(temperature, 2.F) +
-0.05481717F * powf(percentHumidity, 2.F) +
0.00122874F * powf(temperature, 2.F) * percentHumidity +
0.00085282F * temperature * powf(percentHumidity, 2.F) +
-0.00000199F * powf(temperature, 2.F) * powf(percentHumidity, 2.F);

if ((percentHumidity < 13.F) && (temperature >= 80.0F) &&
(temperature <= 112.0F))
hi -= ((13.0F - percentHumidity) * 0.25F) *
sqrtf((17.0F - abs(temperature - 95.0F)) * 0.05882F);

else if ((percentHumidity > 85.0F) && (temperature >= 80.0F) &&
(temperature <= 87.0F))
hi += ((percentHumidity - 85.0F) * 0.1F) * ((87.0F - temperature) * 0.2F);
}

return isFahrenheit ? hi : convertFtoC(hi);
Expand Down
Loading