Skip to content

Commit

Permalink
Workaround Fan PercentSetting float error (2nd)
Browse files Browse the repository at this point in the history
Use integer multiply & devide to workaround floating
point precision error which causes incorrect Fan PercentSetting
value after ceil calculation.
  • Loading branch information
erwinpan1 committed Jul 13, 2023
1 parent 110ef28 commit e54b98a
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/app/clusters/fan-control-server/fan-control-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,9 +346,9 @@ void MatterFanControlClusterServerAttributeChangedCallback(const app::ConcreteAt
VerifyOrReturn(EMBER_ZCL_STATUS_SUCCESS == status,
ChipLogError(Zcl, "Failed to get SpeedSetting with error: 0x%02x", status));

float percent = percentSetting.Value();
// Minus insignificant number 0.00000001 before ceil() to avoid floating point precision error
uint8_t speedSetting = static_cast<uint8_t>(ceil(speedMax * (percent * 0.01) - 0.00000001));
uint16_t percent = percentSetting.Value();
// Plus 99 then devide by 100 instead of multiplying 0.01 to workaround floating point precision error
uint8_t speedSetting = static_cast<uint8_t>(ceil((speedMax * percent + 99) / 100));

if (currentSpeedSetting.IsNull() || speedSetting != currentSpeedSetting.Value())
{
Expand Down

0 comments on commit e54b98a

Please sign in to comment.