Skip to content

Question/poll stats calculation flawed #66

Description

@timkurvers

The current implementation for calculating the percentage for a given option for a question is flawed:

percentage = ( 100 / question.votes.count ) * option.votes.count

This will break when rendering the statistics without any votes cast:

percentage = ( 100 / 0 ) * 0
# => ZeroDivisionError: divided by 0

As soon as the amount of votes surpasses 100 the result is always 0 due to integer division:

percentage = ( 100 / 2 ) * 1
# => 50
percentage = ( 100 / 100 ) * 50
# => 50
percentage = ( 100 / 101 ) * 100
# => 0
percentage = ( 100 / 200 ) * 100
# => 0

The quick'n'dirty fix is:

percentage = (( 100.0 / question.votes.count ) * option.votes.count).round rescue 0

This also applies to polls.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions