This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Svelte and Sapper in Action and have Mark Volkmann on-line!
See this thread for details.
Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Cup is 1/2 full, or empty?

 
Ranch Hand
Posts: 441
Scala IntelliJ IDE 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?
 
Bartender
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).
 
Bartender
Posts: 4568
9
  • 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
Scala IntelliJ IDE 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
Bartender
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
 
lowercase baba
Posts: 12893
63
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?
 
I'm just a poor boy, I need no sympathy, because I'm easy come, easy go, little high, little low, little ad
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic