Originally posted by James Christian:
public void shareOutSurplus(int surplus, int dividedBy) {
int bonus = surplus/dividedBy;
int remainder = surplus%dividedBy;
}
But in a world were everything is dictated by intellectuals who give more weight to unproven theoretical maths than common sense the code actually needs this:
if (surplus != 0)
I've heard it takes forever to grow a woman from the ground
"I'm not back." - Bill Harding, Twister
No, surplus can be zero, you have to check if dividedBy is zero. The problem is that you see division by zero where there is no division.
posted April 05, 2005 02:22 PM Profile for Steven Bell Edit/Delete Post Reply With Quote Yes Nick, In that case I (as the manager/owner) would be the 1. The point I was trying to make to James is that I don't attempt to divide by zero. James seems to think it is perfectly ok to divide by zero.
Although while I'm thinking about it.
James,
Knowing that computers are only capable of addition devise a way to divide by zero using only addition (in this case we'll allow adding negative numbers without the 2's complement longhand). There can be no check for zero of the inputs and it must work for all integers (within computer range). If you can do that I'll buy that division by zero is not a special case.
Originally posted by Warren Dew:
Gosh, I never realized that real people got confused between dividing by zero and dividing by one.
James, the reason processors check for division by zero as a special case is because they normally perform division by repeated subtraction. Without the special case, division by zero would cause a hardware level infinite loop (until the computer got shut off, as you point out). Some people seem to think having the option of handling a hardware exception is better than having to manually pull the plug each time.
posted April 05, 2005 05:04 PM Profile for Steven Bell Edit/Delete Post Reply With Quote Adding and subtracting zero have a known, quantifiable, result. Dividing by zero does not.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
I've heard it takes forever to grow a woman from the ground
Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Originally posted by Joel McNary:
Hmm... I was always taught that x/0 is undefined, not infinity. That is, there is no solution, not even a non-existant number.
"I'm not back." - Bill Harding, Twister
I've heard it takes forever to grow a woman from the ground
"I'm not back." - Bill Harding, Twister
I've heard it takes forever to grow a woman from the ground
Two rational numbers a/b and c/d are equal iff ad = bc
I've heard it takes forever to grow a woman from the ground
code:
int percentCorrect = (int) ((100.0 * numCorrect) / numQuestions + 0.5);
I note that in most cases you've been careful to specify that division by zero should result in zero, with a remainder. Are you perhaps suggesting that integer division should always return two numbers? A quotient and a remainder? (Which in Java would presumably be wrapped in an object of some sort?) If so, that wouldn't be a bad idea necessarily; I'd have to think about it more. If we force (or at least strongly encourage) the programmer to be aware that the result consists of more than just the quotient, I think that's probably a good thing. And returning a quotient of zero (when dividing by zero) might be be OK.
Provided there's also a remainder.
code:
public void divvy_up_profits(department)
{ double surplus = department.get_surplus();
department.decrement_surplus(surplus);
double share = surplus / department.get_employees().size();
Iterator employees = department.get_employees().iterator();
while (employees.hasNext())
{employees.next().add_to_paycheck(share);}
}
This department has recently had all its employees laid off. Without an exception, the money disappears silently. With a warning, one at least has a chance to fix things.