Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cup is 1/2 full, or empty?

 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is not stupid. That is just how integer calculations work (also on a lot of other languages).
 
Matthew Brown
Bartender
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's just how integer arithmetic works. If you don't want it to work like that, don't use integers!
 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
fred rosenberger
lowercase baba
Bartender
Posts: 12086
29
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic