• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Cup is 1/2 full, or empty?

 
Ranch Hand
Posts: 441
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 13069
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Grow your own food... or this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic