From 93ab639c8a53a898f212b9b25752e310d751d8e0 Mon Sep 17 00:00:00 2001
From: patrick96
Date: Wed, 3 Mar 2021 22:38:05 +0100
Subject: [PATCH] fix: unbounded_percentage always rounded down
For example if lower / upper = 0.019 (1.9%), it would return 1% instead
of 2%
Fixes #2399
---
CHANGELOG.md | 5 ++++-
include/utils/math.hpp | 17 ++++++++++-------
tests/unit_tests/utils/math.cpp | 5 +++--
3 files changed, 17 insertions(+), 10 deletions(-)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 55a30bd3..2f136205 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -66,7 +66,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `internal/fs`: `format-warn`, `label-warn`, `warn-percentage = 90`
- `internal/memory`: `format-warn`, `label-warn`, `warn-percentage = 90`
- `radius` now affects the bar border as well
- ([`#1566`](https://github.com/polybar/polybar/issues/1566))
+ ([`#1566`](https://github.com/polybar/polybar/issues/1566))
- Per-corner corner radius with `radius-{bottom,top}-{left,right}`
([`#2294`](https://github.com/polybar/polybar/issues/2294))
- `internal/network`: `speed-unit = B/s` can be used to customize how network
@@ -109,6 +109,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([`#2292`](https://github.com/polybar/polybar/issues/2292))
- Parser error if click command contained `}`
([`#2040`](https://github.com/polybar/polybar/issues/2040))
+- Slight imprecision when calculating percentages. This caused the volume
+ reported by alsa to be off by one.
+ ([`#2399`](https://github.com/polybar/polybar/issues/2399))
- `internal/backlight`: With amdgpu backlights, the brightness indicator was slightly behind.
([`#2367](https://github.com/polybar/polybar/issues/2367))
diff --git a/include/utils/math.hpp b/include/utils/math.hpp
index d76b218a..7bf7b276 100644
--- a/include/utils/math.hpp
+++ b/include/utils/math.hpp
@@ -1,6 +1,7 @@
#pragma once
#include
+#include
#include "common.hpp"
@@ -37,14 +38,16 @@ namespace math_util {
* Calculate the percentage for a value
* between min_value and max_value
*/
- template
- ReturnType unbounded_percentage(ValueType value, ValueType min_value, ValueType max_value){
+ template
+ ReturnType unbounded_percentage(ValueType value, ValueType min_value, ValueType max_value) {
auto upper = (max_value - min_value);
auto lower = static_cast(value - min_value);
- ValueType percentage = (lower / upper) * 100.0f;
- if (std::is_integral())
- percentage += 0.5f;
- return percentage;
+ float percentage = (lower / upper) * 100.0f;
+ if (std::is_integral()) {
+ return static_cast(std::round(percentage));
+ } else {
+ return percentage;
+ }
}
/**
@@ -113,6 +116,6 @@ namespace math_util {
inline int ceil(double value, int step = 1) {
return static_cast((value * 10 + step * 10 - 1) / (step * 10)) * step;
}
-}
+} // namespace math_util
POLYBAR_NS_END
diff --git a/tests/unit_tests/utils/math.cpp b/tests/unit_tests/utils/math.cpp
index c02d8b02..ab44e2b2 100644
--- a/tests/unit_tests/utils/math.cpp
+++ b/tests/unit_tests/utils/math.cpp
@@ -1,6 +1,7 @@
-#include "common/test.hpp"
#include "utils/math.hpp"
+#include "common/test.hpp"
+
using namespace polybar;
TEST(Math, min) {
@@ -38,7 +39,7 @@ TEST(Math, percentage) {
EXPECT_EQ(55.0f, (math_util::percentage(5.5f, 0.0f, 10.0f)));
EXPECT_EQ(56, (math_util::percentage(5.55f, 0.0f, 10.0f)));
EXPECT_EQ(43.75f, (math_util::percentage(5.25f, 0.0f, 12.0f)));
- EXPECT_EQ(41, (math_util::percentage(5, 0, 12)));
+ EXPECT_EQ(42, (math_util::percentage(5, 0, 12)));
EXPECT_EQ(20.5f, (math_util::percentage(20.5f, 0.0f, 100.0f)));
EXPECT_EQ(70.0f, (math_util::percentage(4.5f, 1.0f, 6.0f)));
EXPECT_EQ(21, (math_util::percentage(20.5f, 0.0f, 100.0f)));