From bab29581aca957f487408532706ae19586aa234b Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Mon, 16 Sep 2024 15:39:59 +0300 Subject: [PATCH 1/2] [mathutil] Code refactoring --- CHANGELOG.md | 4 ++++ mathutil/mathutil.go | 6 +++--- version.go | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5fcb405b..b4be6835 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ ## Changelog +### [13.5.1](https://kaos.sh/ek/13.5.1) + +- `[mathutil]` Code refactoring + ### [13.5.0](https://kaos.sh/ek/13.5.0) - `[support/resources]` Added package for collecting info about CPU and memory diff --git a/mathutil/mathutil.go b/mathutil/mathutil.go index 6631ee96..09dea125 100644 --- a/mathutil/mathutil.go +++ b/mathutil/mathutil.go @@ -122,12 +122,12 @@ func Abs[N NumericNeg](val N) N { } // Perc calculates percentage -func Perc[N Numeric](val1, val2 N) float64 { - if val2 == 0 { +func Perc[N Numeric](current, total N) float64 { + if current == 0 || total == 0 { return 0 } - return float64(val1) / float64(val2) * 100.0 + return (float64(current) / float64(total)) * 100.0 } // Round returns rounded value diff --git a/version.go b/version.go index 3df16829..9efe2a5a 100644 --- a/version.go +++ b/version.go @@ -8,4 +8,4 @@ package ek // ////////////////////////////////////////////////////////////////////////////////// // // VERSION is current ek package version -const VERSION = "13.5.0" +const VERSION = "13.5.1" From 56954b653bae6c0e7af8dd91f5e107eb44bb6eeb Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Mon, 16 Sep 2024 15:46:36 +0300 Subject: [PATCH 2/2] [mathutil] Add method 'FromPerc' --- CHANGELOG.md | 1 + mathutil/example_test.go | 6 ++++++ mathutil/mathutil.go | 9 +++++++++ mathutil/mathutil_test.go | 8 ++++++++ 4 files changed, 24 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b4be6835..dbadec37 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ### [13.5.1](https://kaos.sh/ek/13.5.1) +- `[mathutil]` Added method `FromPerc` - `[mathutil]` Code refactoring ### [13.5.0](https://kaos.sh/ek/13.5.0) diff --git a/mathutil/example_test.go b/mathutil/example_test.go index 9cb5cd74..e0c8285d 100644 --- a/mathutil/example_test.go +++ b/mathutil/example_test.go @@ -83,6 +83,12 @@ func ExamplePerc() { // 30% } +func ExampleFromPerc() { + fmt.Printf("%g\n", FromPerc(15.0, 2860)) + // Output: + // 429 +} + func ExampleRound() { fmt.Println(Round(3.14159, 2)) // Output: diff --git a/mathutil/mathutil.go b/mathutil/mathutil.go index 09dea125..ff2795bd 100644 --- a/mathutil/mathutil.go +++ b/mathutil/mathutil.go @@ -130,6 +130,15 @@ func Perc[N Numeric](current, total N) float64 { return (float64(current) / float64(total)) * 100.0 } +// FromPerc calculates value from percentage +func FromPerc(perc float64, total float64) float64 { + if perc <= 0 || total == 0 { + return 0 + } + + return (total / 100.0) * perc +} + // Round returns rounded value func Round(v float64, p int) float64 { pow := math.Pow(10, float64(p)) diff --git a/mathutil/mathutil_test.go b/mathutil/mathutil_test.go index e8da32a0..818192df 100644 --- a/mathutil/mathutil_test.go +++ b/mathutil/mathutil_test.go @@ -68,3 +68,11 @@ func (s *MathUtilSuite) TestPerc(c *C) { c.Assert(Perc(100, 100), Equals, 100.0) c.Assert(Perc(200, 100), Equals, 200.0) } + +func (s *MathUtilSuite) TestFromPerc(c *C) { + c.Assert(FromPerc(0, 0), Equals, 0.0) + c.Assert(FromPerc(100, 0), Equals, 0.0) + c.Assert(FromPerc(-1, 1000), Equals, 0.0) + c.Assert(FromPerc(250, 100), Equals, 250.0) + c.Assert(FromPerc(25.55, -100), Equals, -25.55) +}