views:

287

answers:

5

////////

UPDATE: Christ! I had another look at the program, to see if I was being silly. And indeed I am. The error was in fact not a divide by zero error but 'ArrayIndexOutOfBoundsException:0' This is because because damageTaken is actually an array of values that stores many different 'damages'. So the equation I have is probably correct, and the problem I have now is entirely different from the initial title or question.

I'm giving the point to Thomas O for providing the apples analogy that perfectly answers the question of why.

Thank you so much for your input.

////////

Rest assured I have searched for an answer before asking this question:

In java I'm trying to create a progress bar. Our example: damage incurred, in a racing game, by setting the value for height as a percentage of maxmimum damage allowed before gameover.

At the start of the program damageTaken = 0;

(damageTaken / maximumDamage)

will give numbers between 0 - 1.

Then I just multiply that by the height of the progress bar, to create a fill bar of the appropriate height.

But of course, when damageTaken = 0 we are dividing by zero!!! So program crashes. And I want the progress bar to be of zero height!

why can't It just spit out 0 as the sum? And how do I get around this?

+5  A: 
(damageTaken / maximumDamage)

is dividing by zero ONLY if maximumDamage is zero.

If damageTaken is zero, there is no problem.

abelenky
congratulations on being the only answerer to know the difference between a numerator and a denominator.
anthony
@anthony, congrats on not being able to read all of the answers..
jjnguy
+4  A: 

Just add a special case for 0;

private int getProgress()
    if (damageTaken == 0) {
        return 0;
    } else {
        return (damageTaken / maximumDamage) * progress.getHeight();
    }
}

However, (and it's a big however) the reason you are getting divide by 0 is because maximumDamage is 0, not damageTaken. So, what you probably really want is:

private int getProgress()
    if (maximumDamage== 0) {
        return 0;
    } else {
        return (damageTaken / maximumDamage) * progress.getHeight();
    }
}
jjnguy
Why are you multiplicating with the progress bar's height? I understood it the way that he wants to set the progress bar's height based on the percental value (so it's a vertical progress bar).
poke
@poke, he wants to find out how far the progress needs to be based on the percent of damage and the current height of the entire bar.
jjnguy
+7  A: 

You are not dividing by zero, you are dividing zero by something.

It is completely allowed to take two halves of zero. The answer is zero. Say you have zero apples. You split your zero apples between Alice and Bob. Alice and Bob now both have zero apples.

But, you cannot divide by zero. Say you have two apples. Now, you want to give these apples to zero people. How many apples does each person get? The answer is undefined, and so division by zero is impossible.

Thomas O
Much simpler is, suppose if there existed an x such that 0*x = 1, then we have an immediate contradiction since 0 = 0*x = 1.
ldog
Yes. If division by zero was allowed then a multiplicative inverse must exist. For example if you claim 6 divided by 2 equals 3, you can prove it because 3*2 = 6. But you cannot say 6 divided by 0 equals 3 because 3*0 = 6 is never true. In calculus you might regard the limit of 1/x as going to infinity but this doesn't work in the real world.
Thomas O
Even in calculus it doesn't work unless you're very particular about the limit you take. Every direction of approach in the complex plane yields a different limit, assuming your topology can distinguish them. Which opens a whole new can of worms.
mokus
A: 

[ignore me, sorry]

Arif Driessen
You should be able to delete your own question, I believe...
djacobson
well I can't. Thanks for the -1
Arif Driessen
I didn't downvote you, someone else did. I was just trying to be informative.
djacobson
A: 

Let's stop talking about illegality, as though uniformed mathematics police were about to storm the room. ;) For the mathematical reasoning behind this, this related question is useful.

It is fair to ask why, after all this time, modern programming platforms don't handle division-by-zero errors for us in some standard way - but to answer the question: Java, like most any other platform, throws an error when division by zero occurs. In general, you can either:

  • Handle before: check the variable to be used as your denominator for zero before doing the division
  • Handle after: you can catch the exception thrown when the division by zero occurs and handle it gracefully.

Note that, as good practice, the second method is only appropriate when you wouldn't expect the variable to ever be zero under normal system operation.

So yes, you need a special case to handle this situation. Welcome to programming. ;)

djacobson