# Cup is 1/2 full, or empty?

posted 5 years ago

Have just spent 2 frustrating hours debugging a bouncing ball program that wasn't behaving correctly.

It turned out it was because I had a formula that was something like

So if b is 2 and c is 8, a = ...

yes, that's right, 2.

Seriously, why is Java this stupid?

It turned out it was because I had a formula that was something like

So if b is 2 and c is 8, a = ...

yes, that's right, 2.

Seriously, why is Java this stupid?

Matthew Brown

Bartender

Posts: 4566

8

posted 5 years ago

Well, a, b, and c were doubles.

Looking at it from a beginner's perspective, it's definitely a bit strange that 1/2 == 0.

I just checked in VB, and it returns 0.5 there, even if you explicitly define two variables as integers and divide one by the other.

+ the whole of mathematics since the dawn of civilization, 1/2 has always been... well, 1/2, not 0.

At least throw a compiler warning or something.

Looking at it from a beginner's perspective, it's definitely a bit strange that 1/2 == 0.

I just checked in VB, and it returns 0.5 there, even if you explicitly define two variables as integers and divide one by the other.

+ the whole of mathematics since the dawn of civilization, 1/2 has always been... well, 1/2, not 0.

At least throw a compiler warning or something.

posted 5 years ago

1/2 with integers always has been 0 since the beginning of computers. That is the way it's implemented in the hardware and there is also a variant for floating point calculation. And throwing a warning will not help. What would that warning be: Warning you're using integer to calculate stuff

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler

Please correct my English.

posted 5 years ago

ummm...I'm not sure you know your math history. The concept of fractions is a relatively new concept, as compared to some others. Think about it...when you were first learning how do divide, did you say that 7 / 3 = 2.333333...., or did you say it was 2 with a remainder of 1?

- 1

Luigi Plinge wrote: the whole of mathematics since the dawn of civilization, 1/2 has always been... well, 1/2, not 0.

ummm...I'm not sure you know your math history. The concept of fractions is a relatively new concept, as compared to some others. Think about it...when you were first learning how do divide, did you say that 7 / 3 = 2.333333...., or did you say it was 2 with a remainder of 1?

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors