Win a copy of TDD for a Shopping Website LiveProject this week in the Testing 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 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

division by zero

 
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I have a question about division by zero. RHE has an example that
double d = 10.0d/0.0d;
if(d==double.POSITIVE_INFINITY)
//whatever...
Why is ArithmaticException not thrown? Is it because it's a floating type and not integer type?
 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's exactly right, Cameron. I believe that values for infinity and negative infinity are a part of the floating-point standard that Java uses, IEEE 754. Other aspects of java.lang.Double might jog your imagination on this topic.
Also, I thought I'd point out that in the code sample you gave, you could also use java.lang.Double's static method isInfinite():

(N.B., isInfinite() returns true if "d" equals Double.NEGATIVE_INFINITY or Double.POSITIVE_INFINITY.)
HTH,
Art
[This message has been edited by Art Metzer (edited May 04, 2001).]
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Cameron,
I f you do division by Zero with integral type (i.e byte, short,int,long)then Arithmatic Exception will be thrown.But if you do division by zero with floating point it will not throw Arithmatic Exception.Because Java makers trust that +infinity & -infinity exist.So they put facility of showing +ve & -ve infinity in Java but they select float & double to show infinity.Because float & double have big range then integral types.
This is what i think.May be i am wrong.Confirm it before accepting.
Hassan.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
u have said double.POSITIVE_INFINITY
i think u should write d in caps i.e. Double.POSITIVE_INFINITY
abhijit lodha :-)
 
Cameron Park
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks! double doesn't have any fields. Hehehe
 
What do you have to say for yourself? Hmmm? Anything? And you call yourself a tiny ad.
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic