Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
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
• Ron McLeod
• Jeanne Boyarsky
• Tim Cooke
Sheriffs:
• Liutauras Vilda
• paul wheaton
• Henry Wong
Saloon Keepers:
• Tim Moores
• Tim Holloway
• Stephan van Hulst
• Carey Brown
• Frits Walraven
Bartenders:
• Piet Souris
• Himai Minh

# variable declaration changes output

Ranch Hand
Posts: 55
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:

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
• Number of slices to send:
Optional 'thank-you' note:
You only get ArithmeticException when you divide an integer by integer zero.

author
Posts: 14112
• Number of slices to send:
Optional 'thank-you' note:

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.

 Do you pee on your compost? Does this tiny ad? Free, earth friendly heat - from the CodeRanch trailboss https://www.kickstarter.com/projects/paulwheaton/free-heat