-
Notifications
You must be signed in to change notification settings - Fork 806
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
common/math: Add math.Abs to PercentageDifference calculation #1617
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wouldn't call it a bug to have a negative percentage difference. I would suggest you add CalculateAbsPercentageDifference
and DecimalAbsPercentageDifference
Co-authored-by: Scott <[email protected]>
Yeah, I guess they are both valid so it's not a bug, but I would caution adding the difference though as you can get values above 100% in the negative direction, all we are achieving is finding the degree of magnitude, I would say directionality can be applied to the value after if needed? I see that I am starting to furiously agree with you as I type this. So I can add the distinction and people can choose their own destiny. 😆 |
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #1617 +/- ##
==========================================
+ Coverage 36.37% 37.11% +0.74%
==========================================
Files 422 414 -8
Lines 183113 180203 -2910
==========================================
+ Hits 66602 66880 +278
+ Misses 108466 105462 -3004
+ Partials 8045 7861 -184
|
CalculatePercentageDifference
and add DecimalPercentageDifference
CalculateAbsPercentageDifference
and add DecimalAbsPercentageDifference
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
itotallydidntforgetabouthitsonepleasedontbemadtACK!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added questions and naming suggestions.
Nothing is a deal breaker, so it's Approved.
My biggest question is with the maths here.
When you say "percentage difference" this doesn't feel like something you can average.
This takes the change as a percentage difference between the average of two numbers.
Normally I'd want to know percentage increase or decrease, and I'd expect it to be the difference as a percentage of the smaller or larger number, respectively.
So I'm just noting that this feels like a subjective implementation, and without further context I just assume it suits the context.
Certainly "PercentageDifference" isn't "PercentageIncrease" so ... that's good 🤷
common/math/math.go
Outdated
return (amount - secondAmount) / ((amount + secondAmount) / 2) * 100 | ||
// CalculatePercentageDifference returns the percentage difference between two | ||
// numbers | ||
func CalculatePercentageDifference(a, b float64) float64 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Naming:
Admitting this was already here, but "Calculate" feels redundant.
I'd be supportive of an aggressive rename.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GetPercentageDifferenceAggressively()
? 😆 What do you suggest for the name?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generally Get or Fetch should be for something you need to go and find, where it might not be obvious that you're going external to get it. Most of the time I feel they should be left out as well though.
They all talk too much about the methodology, which isn't relevant (and in some cases might change, when caching is introduced).
So just PercentageDifference
or PctDiff
.
I'd probably say PercentageDelta
.
Note: I got saner in the main comment. DifferenceAsPercentageOfAverage
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PercentageDifference
is good 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Part of the rename can change CalculatePercentageGainOrLoss
to PercentageChange
to make it less specific. Then rename all funcs to have their function's purpose first. So:
PercentageChange
> native
PercentageChangeDecimal
> decimal etc.
I think this would be more useful for people using autocomplete features in their IDE since ATM we have a lot of prefixed DecimalX
's which could spam the user with options
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The average is the point of reference rather than a percentage change from point A (as reference) -> point B. I think that's the main |
Alternative maths for what I'd expect this to be:
So ... I guess nail on the head is terminology.
|
Let me retract: The majority of calculators and definitions accept |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One comment change. Soz.
common/math/math.go
Outdated
return (v2 - v1) / v1 * 100 | ||
} | ||
|
||
// PercentageDifference returns the percentage difference between two numbers |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This definitely needs to say that it's of the average. All the calculators were at pains to point that out too.
Suggest:
// PercentageDifference returns difference between two numbers as a percentage of their average
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CalculateAbsPercentageDifference
and add DecimalAbsPercentageDifference
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good work 🎉
PR Description
CalculatePercentageGainOrLoss
->PercentageChange
Fixes # (issue)
Type of change
Please delete options that are not relevant and add an
x
in[]
as item is complete.How has this been tested
Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration and
also consider improving test coverage whilst working on a certain feature or package.
Checklist