Skip to content

Commit

Permalink
Workaround Fan Percent Setting floating point err
Browse files Browse the repository at this point in the history
Workaround floating point precision error which will cause invalid value
after ceil()

For example, the current value:
   speedMax: 10
   percent: 70
   speedMax * (percent * 0.01) = 7.000000000000001 (floating point
precision error)
   ceil(speedMax * (percent * 0.01)) = 8 => The error propagate to ceil
and cause the final result error.
  • Loading branch information
erwinpan1 committed Jul 12, 2023
1 parent a85c19f commit 42bbc9f
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/app/clusters/fan-control-server/fan-control-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,8 @@ void MatterFanControlClusterServerAttributeChangedCallback(const app::ConcreteAt
ChipLogError(Zcl, "Failed to get SpeedSetting with error: 0x%02x", status));

float percent = percentSetting.Value();
uint8_t speedSetting = static_cast<uint8_t>(ceil(speedMax * (percent * 0.01)));
// 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));

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

0 comments on commit 42bbc9f

Please sign in to comment.