• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

variable declaration changes output

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in the program below I have initialised two variables with int values and decrement the variable f by one each repetition f eventually becomes 0 which creates a ArithmeticException and I get the desired result for the exercise the code is below

the resultant output is
120/5 is 24
120/4 is 30
120/3 is 40
120/2 is 60
120/1 is 120
cannot devide by zero ---which is what I expectd

I changed the variable f to a double :-

and got this
120/5.0 is 24.0 etc.....
120/1.0 is 120.0
120/0.0 is infinity

Would someone be kind enough to tell me what causes the difference in output when a varialbles initialiser is changed , please
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by daniel hyslop:

120/0.0 is infinity

Would someone be kind enough to tell me what causes the difference in output when a varialbles initialiser is changed , please



According to Horstman and Cornell in "Core Java", that is the normal behavior for types float and double:

". . . the result of dividing a positive number by 0 is positive infinity."

I think it's probably because floating point numbers always have a limited precision.
 
author
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You only get ArithmeticException when you divide an integer by integer zero.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by M. Gagnon:
I think it's probably because floating point numbers always have a limited precision.



It's actually part of the IEEE Standard 754 for floating point numbers.

http://stevehollasch.com/cgindex/coding/ieeefloat.html

Being able to denote infinity as a specific value is useful because it allows operations to continue past overflow situations. Operations with infinite values are well defined in IEEE floating point.

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic