From 401215243d86af217fc53edc2374e17219342571 Mon Sep 17 00:00:00 2001 From: Erwin Pan Date: Fri, 14 Jul 2023 00:19:49 +0800 Subject: [PATCH] Workaround Fan PercentSetting floating point precision error after "ceil()" (#27912) * Workaround Fan Percent Setting floating point err 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. * Restyled by clang-format * Workaround Fan PercentSetting float error (2nd) Use integer multiply & devide to workaround floating point precision error which causes incorrect Fan PercentSetting value after ceil calculation. * Update src/app/clusters/fan-control-server/fan-control-server.cpp Co-authored-by: Boris Zbarsky --------- Co-authored-by: Restyled.io Co-authored-by: Andrei Litvin Co-authored-by: Boris Zbarsky --- src/app/clusters/fan-control-server/fan-control-server.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/app/clusters/fan-control-server/fan-control-server.cpp b/src/app/clusters/fan-control-server/fan-control-server.cpp index 4c70a4a0f02073..eed33820e0a947 100644 --- a/src/app/clusters/fan-control-server/fan-control-server.cpp +++ b/src/app/clusters/fan-control-server/fan-control-server.cpp @@ -346,8 +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(); - uint8_t speedSetting = static_cast(ceil(speedMax * (percent * 0.01))); + uint16_t percent = percentSetting.Value(); + // Plus 99 then integer divide by 100 instead of multiplying 0.01 to avoid floating point precision error + uint8_t speedSetting = static_cast((speedMax * percent + 99) / 100); if (currentSpeedSetting.IsNull() || speedSetting != currentSpeedSetting.Value()) {