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

AvgOf produces overflow for large values #532

Closed
vmotsak opened this issue Jan 8, 2018 · 32 comments · Fixed by #581
Closed

AvgOf produces overflow for large values #532

vmotsak opened this issue Jan 8, 2018 · 32 comments · Fixed by #581
Labels

Comments

@vmotsak
Copy link

vmotsak commented Jan 8, 2018

new AvgOf(
          new ListOf<>(Double.MAX_VALUE, Double.MAX_VALUE).toArray(new Double[2])
).doubleValue();

returns an Infinity but should be Double.MAX_VALUE

@0crat
Copy link
Collaborator

0crat commented Jan 8, 2018

@yegor256 please, pay attention to this issue

@yegor256 yegor256 added the bug label Jan 9, 2018
@yegor256
Copy link
Owner

yegor256 commented Jan 9, 2018

@vmotsak definitely a bug, thanks for reporting (although, I have no idea how this can be fixed)

@0crat
Copy link
Collaborator

0crat commented Jan 9, 2018

Job gh:yegor256/cactoos#532 is now in scope, role is DEV.

@0crat
Copy link
Collaborator

0crat commented Jan 9, 2018

Job gh:yegor256/cactoos#532 assigned to @allanws (DEV). The budget is fixed and it is 30 minutes. Please, read §4, §8, and §9. If the task is not clear, read this and this.

@svendiedrichsen
Copy link
Contributor

svendiedrichsen commented Jan 9, 2018

@yegor256 i suppose you will have to use BigInteger/BigDecimal for your computations. Having the usual performance impact surely. But if performance is not key here this would have the highest accuracy.

@vmotsak
Copy link
Author

vmotsak commented Jan 9, 2018

This can be fixed by calculating the mean value, something like

double mean(Double[] ary) {
        double avg = 0;
        int t = 1;
        for (double x : ary) {
            avg += (x - avg) / t;
            ++t;
        }
        return avg;
    }

@svendiedrichsen
Copy link
Contributor

svendiedrichsen commented Jan 9, 2018

Mathematically correct but you might run into strange double precision problems though. Probably not with your example but to have AvgOf work consistently correct you have to go with the highest precision.
See for i.e.

about this. There is way more mathematically advanced stuff about this out there.

@svendiedrichsen
Copy link
Contributor

svendiedrichsen commented Jan 10, 2018

Please see the average of new Double[]{100.0, 100.266} using the above mentioned mean method. Should be 100.133 of course. But it isn't. It is 100.13300000000001.

Another one: System.out.println(100.266 - 100.0);
Output: 0.26600000000000534

@svendiedrichsen
Copy link
Contributor

svendiedrichsen commented Jan 11, 2018

@yegor256 : I have created an example of what IMO could be done with the AvgOf code to fix number overflow, fix precisions problem and cut some code repetitions. Please have a look here: svendiedrichsen@358b7f1

It is still not the most elegant piece of code and surely deserves some more refactoring. But i think it is better than before.

@0crat
Copy link
Collaborator

0crat commented Jan 14, 2018

@allanws this job was assigned to you 5 days ago. It will be taken away from you soon, unless you close it, see §8. Read this and this, please.

@0crat
Copy link
Collaborator

0crat commented Jan 17, 2018

@AllanWS/z this job was assigned to you 8 days ago. It will be taken away from you soon, unless you close it, see §8. Read this and this, please.

@allanwsilva
Copy link
Contributor

@yegor256 please have a look at my latest comment on this issue.

@allanwsilva
Copy link
Contributor

@0crat waiting

@0crat
Copy link
Collaborator

0crat commented Jan 20, 2018

@0crat waiting (here)

@AllanWS The impediment was registered successfully

@yegor256
Copy link
Owner

@0crat waiting

@0crat
Copy link
Collaborator

0crat commented Jan 22, 2018

@0crat waiting (here)

@yegor256 Job #532 is not assigned, can't put it on hold

@yegor256
Copy link
Owner

@0crat status

@0crat
Copy link
Collaborator

0crat commented Jan 22, 2018

Are you speaking to me or about me? You must always start your message with my name if you want to address it to me, see §1.

@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:

  • The job #532 is in scope
  • The job is not assigned to anyone
  • These users are banned: @vmotsak/z: This user reported the ticket; @AllanWS/z: User was resigned from the ticket

@yegor256
Copy link
Owner

@0crat assign @AllanWS

@0crat
Copy link
Collaborator

0crat commented Jan 22, 2018

@0crat assign @AllanWS (here)

@yegor256 The job #532 assigned to @AllanWS/z. The budget is 30 minutes, see §4. Please, read §8 and §9. If the task is not clear, read this and this. @AllanWS/z is not a member of this project yet, but they can request to join. Check your profile and follow the instructions.

@0crat
Copy link
Collaborator

0crat commented Jan 22, 2018

Manual assignment of issues is discouraged, see §19: -5 points just awarded to @yegor256/z, total is +7780

@yegor256
Copy link
Owner

@0crat waiting

@0crat
Copy link
Collaborator

0crat commented Jan 22, 2018

@0crat waiting (here)

@yegor256 The impediment for #532 was registered successfully by @yegor256/z

@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 #532 is in scope for 16days
  • The role is DEV
  • The job is not assigned to anyone
  • These users are banned: @vmotsak/z: This user reported the ticket; @AllanWS/z: User was resigned from the ticket; @AllanWS/z: User was resigned from the ticket

@0crat
Copy link
Collaborator

0crat commented Jan 25, 2018

The job #532 assigned to @rexim/z. The budget is 30 minutes, see §4. Please, read §8 and §9. If the task is not clear, read this and this.

@0crat
Copy link
Collaborator

0crat commented Jan 29, 2018

Order was successfully finished: +30 points just awarded to @rexim/z, total is +45

@0crat
Copy link
Collaborator

0crat commented Jan 29, 2018

The job #532 is now out of scope

@g4s8
Copy link
Contributor

g4s8 commented Feb 21, 2018

@0crat status

@0crat
Copy link
Collaborator

0crat commented Feb 21, 2018

@0crat status (here)

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

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

Successfully merging a pull request may close this issue.

7 participants