-
Notifications
You must be signed in to change notification settings - Fork 170
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
#532 AvgOf() bug on Double edge values fixed #543
Conversation
@yegor256 please, pay attention to this pull request |
Job #543 is now in scope, role is |
@AllanWS Before submit your PR, please run an |
Codecov Report
@@ Coverage Diff @@
## master #543 +/- ##
============================================
+ Coverage 66.27% 67.04% +0.77%
- Complexity 931 933 +2
============================================
Files 194 194
Lines 3208 3104 -104
Branches 245 207 -38
============================================
- Hits 2126 2081 -45
+ Misses 1013 970 -43
+ Partials 69 53 -16
Continue to review full report at Codecov.
|
@@ -178,4 +178,36 @@ public void withScalars() { | |||
); | |||
} | |||
|
|||
@Test | |||
public void withDoubleMaxValue() { |
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.
@AllanWS I'm sorry, but your PR don't fix the same problem with Integer
, Float
and Long
.
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.
@fabriciofx , it's now fixed. Thanks for your "quick" review.
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.
@AllanWS You're welcome.
…or all Java Number types (Float.MAX_VALUE, Integer.MAX_VALUE, Long.MAX_VALUE)
public void withMaxValueCollections() { | ||
MatcherAssert.assertThat( | ||
new AvgOf( | ||
new ListOf<>(Double.MAX_VALUE, Double.MAX_VALUE) |
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.
@AllanWS It would be nice if you test to just one, two and three MAX_VALUE
(same to Double, Float, Long and Integer). Also change with odd and even values (max values you can). And to negative values? Any tests?
double sum = 0.0d; | ||
double total = 1.0d; | ||
for (final double val : src) { | ||
sum += (val - sum) / total; | ||
total += 1.0f; | ||
} | ||
if (total == 0.0f) { | ||
total = 1.0f; | ||
} |
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.
Aren't these IFs obsolete?
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.
@svendiedrichsen yes, you're right! Thanks.
@AllanWS Could you test the average of |
int total = 0; | ||
for (final int val : src) { | ||
sum += val; | ||
double sum = 0; |
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.
@AllanWS You should be explicit with double values. So, use 0.0d
instead of 0
.
for (final int val : src) { | ||
sum += val; | ||
double sum = 0; | ||
double total = 1; |
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.
@AllanWS The same here (1.0d
).
long total = 0L; | ||
for (final long val : src) { | ||
sum += val; | ||
double sum = 0d; |
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.
@AllanWS The same here. See above.
@@ -178,4 +178,86 @@ public void withScalars() { | |||
); | |||
} | |||
|
|||
@Test | |||
public void withMaxValueCollections() { |
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.
@AllanWS I think interesting include more tests, like 100.0
and 100.266
as suggested by @svendiedrichsen, and Double.MAX_VALUE
and 0
, the last three and four Double.MAX_VALUE
numbers (Double.MAX_VALUE
, Double.MAX_VALUE - 1
and Double.MAX_VALUE - 2
for example.
); | ||
MatcherAssert.assertThat( | ||
new AvgOf( | ||
new ListOf<>(Integer.MAX_VALUE, Integer.MAX_VALUE) |
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.
@AllanWS The same here. See above.
); | ||
MatcherAssert.assertThat( | ||
new AvgOf( | ||
new ListOf<>(Float.MAX_VALUE, Float.MAX_VALUE) |
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.
@AllanWS The same here. See above.
public void withMinValueCollections() { | ||
MatcherAssert.assertThat( | ||
new AvgOf( | ||
new ListOf<>(Double.MIN_VALUE, Double.MIN_VALUE) |
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.
@AllanWS And Double.MAX_VALUE
and Double.MIN_VALUE
? See above.
|
||
@Test | ||
public void withEvenOddValues() { | ||
final Float[] array = new ListOf<>(12345.0f, -12345.0f) |
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.
@AllanWS Prefer more clear names. Instead of array
use numbers
.
@@ -83,16 +83,13 @@ public AvgOf(final Integer... src) { | |||
} |
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.
@AllanWS First of all, I think it a good idea look at the @svendiedrichsen suggestion and comments about this issue.
@@ -83,16 +83,13 @@ public AvgOf(final Integer... src) { | |||
} | |||
return sum / total; | |||
}, () -> { | |||
int sum = 0; |
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.
@AllanWS I don't know if is a good idea change int
, long
and float
calculations for double
. Is not possible use int
, long
and float
? If not, why not?
@AllanWS Please, check my comments above. I'll accept your PR when these problems will fixed, ok? |
@fabriciofx I'm still working on it. |
…chsen code polish (nice and clean) and added more test cases according @fabriciofx comments. Decimal precision problem solved as well.
@fabriciofx please resume the code review. |
return sum / total; | ||
}); | ||
super(() -> avgArray( | ||
number -> BigDecimal.valueOf(number.intValue()), src |
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.
@AllanWS Needs indent.
return sum / total; | ||
}); | ||
super(() -> avgArray( | ||
number -> BigDecimal.valueOf(number.longValue()), src |
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.
@AllanWS Needs indent.
return sum; | ||
}); | ||
super(() -> avgArray( | ||
number -> BigDecimal.valueOf(number), src |
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.
@AllanWS Needs indent.
return sum / total; | ||
}); | ||
super(() -> avgArray( | ||
number -> BigDecimal.valueOf(number.floatValue()), src |
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.
@AllanWS Needs indent.
return sum / total; | ||
}); | ||
super(() -> avgIterator( | ||
number -> BigDecimal.valueOf(number.longValue()), src |
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.
@AllanWS Needs indent.
new ListOf<>(1.0f, 2.0f, 3.0f, 4.0f).toArray(new Float[4]) | ||
).doubleValue(), | ||
Matchers.equalTo(2.5d) | ||
100.0, 100.666, 100.0 |
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.
@AllanWS These values are double and you're checking if the result is float. Is is right?
).floatValue(), | ||
Matchers.equalTo(2.5f) | ||
Matchers.equalTo(100.2665f) |
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.
@AllanWS These values are double and you're checking if the result is float. Is is right?
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.
you can down/up cast numbers, this is "usual"...
MatcherAssert.assertThat( | ||
new AvgOf(array).intValue(), Matchers.equalTo(expected) | ||
); | ||
public void withIterableOfScalars() { |
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.
@AllanWS Please, add more tests, including using composition with other Scalar
classes, for example:
new AvgOf(
new MaxOf(2, 3),
new MinOf(5, 9),
new SumOf(1, 2, 3)
).intValue()
And don't forget the Ternary
class too.
@@ -51,213 +51,313 @@ public void withEmptyCollection() { | |||
} | |||
|
|||
@Test | |||
public void withIntegerCollection() { | |||
public void withIntCollectionIntValue() { |
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.
@AllanWS Have you looked at other frameworks that work which values they used to test how calculate an average? Almost all tests are using only 1, 2, 3 and 4. It's important check with other numbers.
new ListOf<>(1.0d, 2.0d, 3.0d, 4.0d).toArray(new Double[4]) | ||
).floatValue(), | ||
Matchers.equalTo(2.5f) | ||
Double.MAX_VALUE, Double.MAX_VALUE |
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.
@AllanWS Add tests using positive and negative numbers. And positive or negative and with NaN 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.
They are already there, have a look again.
@yegor256 is it mandatory the class I don't see a good design in those classes extending Is this open for discussion? Maybe this might help this discussion. |
@0crat status |
@0crat status |
@fabriciofx This is what I know about this job, as in §32:
|
Order was successfully finished: +15 points just awarded to @fabriciofx/z, total is +405 |
The job #543 is now out of scope |
Fixing Issue #532
After reading @vmotsak comments and also considering @svendiedrichsen input and reading a few more things on the theme, like here, here and here, I decided to go on the simplest solution for now, implementing what @vmotsak suggested.
Maybe in the future we can consider adding more number types of avg. calculation (as @svendiedrichsen suggested).