Skip to content
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

Merged
merged 9 commits into from
Jan 29, 2018
Merged

#532 AvgOf() bug on Double edge values fixed #543

merged 9 commits into from
Jan 29, 2018

Conversation

allanwsilva
Copy link
Contributor

@allanwsilva allanwsilva commented Jan 10, 2018

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).

@0crat
Copy link
Collaborator

0crat commented Jan 10, 2018

@yegor256 please, pay attention to this pull request

@0crat
Copy link
Collaborator

0crat commented Jan 10, 2018

Job #543 is now in scope, role is REV

@fabriciofx
Copy link
Contributor

@AllanWS Before submit your PR, please run an $ mvn clean install -Pqulice to check if everything is fine. And another tip: more explicit issue title and commit messages (What kind of bugfix? Bugfix who?) helps a lot! 😄

@allanwsilva allanwsilva changed the title #532 bugfix #532 AvgOf() Double.MAX_VALUE Double.MIN_VALUE bug fixed Jan 10, 2018
@codecov-io
Copy link

codecov-io commented Jan 10, 2018

Codecov Report

Merging #543 into master will increase coverage by 0.77%.
The diff coverage is 91.78%.

Impacted file tree graph

@@             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
Impacted Files Coverage Δ Complexity Δ
src/main/java/org/cactoos/scalar/AvgOf.java 92.5% <91.78%> (+27.82%) 43 <42> (+2) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c34b676...b74508d. Read the comment docs.

@allanwsilva allanwsilva changed the title #532 AvgOf() Double.MAX_VALUE Double.MIN_VALUE bug fixed #532 AvgOf() bug on Double edge values fixed Jan 10, 2018
@@ -178,4 +178,36 @@ public void withScalars() {
);
}

@Test
public void withDoubleMaxValue() {
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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)
Copy link
Contributor

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;
}
Copy link
Contributor

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?

Copy link
Contributor Author

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.

@svendiedrichsen
Copy link
Contributor

@AllanWS Could you test the average of new Double[]{100.0, 100.266} please? Should be 100.133 of course. But it isn't.

@0crat
Copy link
Collaborator

0crat commented Jan 11, 2018

This pull request #543 is assigned to @fabriciofx. The budget is 15 minutes, see §4. Please, read §27 and §10 and when you decide to accept the changes, inform @yegor256 right in this ticket.

int total = 0;
for (final int val : src) {
sum += val;
double sum = 0;
Copy link
Contributor

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;
Copy link
Contributor

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;
Copy link
Contributor

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() {
Copy link
Contributor

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)
Copy link
Contributor

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)
Copy link
Contributor

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)
Copy link
Contributor

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)
Copy link
Contributor

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) {
}
Copy link
Contributor

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;
Copy link
Contributor

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?

@fabriciofx
Copy link
Contributor

@AllanWS Please, check my comments above. I'll accept your PR when these problems will fixed, ok?

@allanwsilva
Copy link
Contributor Author

@fabriciofx I'm still working on it.

@allanwsilva
Copy link
Contributor Author

@fabriciofx please resume the code review.

return sum / total;
});
super(() -> avgArray(
number -> BigDecimal.valueOf(number.intValue()), src
Copy link
Contributor

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
Copy link
Contributor

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
Copy link
Contributor

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
Copy link
Contributor

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
Copy link
Contributor

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
Copy link
Contributor

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)
Copy link
Contributor

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?

Copy link
Contributor Author

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() {
Copy link
Contributor

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() {
Copy link
Contributor

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
Copy link
Contributor

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.

Copy link
Contributor Author

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.

@allanwsilva
Copy link
Contributor Author

allanwsilva commented Jan 19, 2018

@yegor256 is it mandatory the class org.cactoos.scalar.AvgOf to extend org.cactoos.scalar.NumberEnvelope?

I don't see a good design in those classes extending org.cactoos.scalar.NumberEnvelope, a lot of code duplication in the constructors (MinOf, SumOf, others) just to satisfy the super class constructor...

Is this open for discussion? Maybe this might help this discussion.

@allanwsilva
Copy link
Contributor Author

@0crat waiting (here)

@0crat
Copy link
Collaborator

0crat commented Jan 20, 2018

@0crat waiting (here) (here)

@AllanWS It's a code review job #543, can't put it on hold

@yegor256
Copy link
Owner

@0crat status

@0crat
Copy link
Collaborator

0crat commented Jan 22, 2018

@0crat status (here)

@yegor256 This is what I know about this job, as in §32:

@fabriciofx
Copy link
Contributor

@0crat status

@0crat
Copy link
Collaborator

0crat commented Jan 25, 2018

@0crat status (here)

@fabriciofx This is what I know about this job, as in §32:

  • The job #543 is in scope for 15days
  • The role is REV
  • The job is assigned to @fabriciofx/z for 13days
  • These users are banned: @AllanWS/z: This user reported the ticket

@rultor rultor merged commit b74508d into yegor256:master Jan 29, 2018
@0crat
Copy link
Collaborator

0crat commented Jan 29, 2018

Order was successfully finished: +15 points just awarded to @fabriciofx/z, total is +405

@0crat
Copy link
Collaborator

0crat commented Jan 29, 2018

Payment to ARC for a closed pull request, as in §28: +15 points just awarded to @yegor256/z, total is +8755

@0crat
Copy link
Collaborator

0crat commented Jan 29, 2018

The job #543 is now out of scope

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants