• 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

Weird Output !!

 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Members,
I do not know why the output of the following code is unexpected ?



The Output is : Infinity while I was Expecting : Exception

Help !
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you apply an arithmetic operator to two different primitive types, the "narrower" one undergoes a "widening conversion." You can read all about the different types of conversion in the Java Language Specification (look for §5.1.2, but it is by no means easy reading).

Another way to look at it is that when you try

double / int

the compiler alters it to

double / double

and double arithmetic permits division by zero.

Try it with k = 0.0 and see what happens
 
Fidel Edwards
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Campbell ,
As you solved my problem in seconds.

Campbell Ritchie wrote:

Try it with k = 0.0 and see what happens



As you said i tried also the same k=0.0 this time I am getting NaN message in console.Where these messages come from ?

Campbell Ritchie wrote:
You can read all about the different types of conversion in the Java Language Specification (look for §5.1.2, but it is by no means easy reading ).



Any other short note or tutorial related to this topic will be really appreciated !
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can't think of a simple tutorial at the moment, but you will find descriptions of NaN etc in the Double class documentation. Look at DETAIL FIELDS and the toString method. See whether those help. NaN and infinity are regarded as mathematical values rather than computing constructs.

Then try this code snippet.
 
Fidel Edwards
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Compbell,

It would be definately helpful for me !!

Thanks a lot !
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome

And what output did you get from the == statements?
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you do an integer divide by zero, Java throws an exception. But floating point numbers have values that represent positive and negative infinity, and "not a number". In other words, you can store the "positive infinity" value in a floating point variable. Therefore, a floating point divide by zero succeeds, and the positive infinity value is stored in the variable. (And you can check for it using constants defined in class java.lang.Double.) Here's a Wikipedia article on the IEEE standard -- IEEE 754. Here's a link to the Java spec on division: Java Language Specification, Division Operator.
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Useful links about division and IEEE754. Thank you.
 
Fidel Edwards
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

And what output did you get from the == statements?



Sorry Campbell for late reply

In your previous post for i also tried == but got something different I could not figure it out that why i am getting

 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Back to the Java™ Language Specification where it tells you more about the strange behaviour of NaN. Again, not easy to read.
 
Fidel Edwards
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Back to the Java™ Language Specification where it tells you more about the strange behaviour of NaN. Again, not easy to read.




Again Thanks !!
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
reply
    Bookmark Topic Watch Topic
  • New Topic