• 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

Rules Roundup - answer wrong?

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One of the questions on Rules Roundup was
double x;
x=24.0/0;
what is the value of x? I tested this out and it came to Infinity, although, the answer given was NaN.
Which is the correct answer?
Thanks
Sharda
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sounds like Rules Roundup has an incorrect answer. A floating-point x/0.0 should be Infinity if x is positive, -Infinity if x is negative, and NaN only if x is 0.
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sounds like Rules Roundup has an incorrect answer. A floating-point x/0.0 should be Infinity if x is positive, -Infinity if x is negative, and NaN only if x is 0.


Ang guess what x is 0 in the question....
 
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well isn't that 0 promoted to a double before the evaluation? If so wouldn't that result in a NaN since division by zero using double or float doesn't give a ArithmeticException but instead returns Double.NaN or Float.NaN?
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do you think it should return a NaN rather than an Infinity?
 
Matt Kidd
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ron Newman:
Why do you think it should return a NaN rather than an Infinity?



Because for some reason I have it stuck in my head that division by zero is not a number. However double checking my books I see that division by zero is infinity when dealing with floating point numbers, the sign determined by the original sign of the dividend (numerator if a fraction).
I honestly can't think of where I learned division by zero is not a number to keep me making that mistake.
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sounds like Rules Roundup has an incorrect answer. A floating-point x/0.0 should be Infinity if x is positive, -Infinity if x is negative, and NaN only if x is 0.


If 0 is always going to be converted to double before division occurs, is it fair to say that NaN is never going to be output of x/0 where x is double.
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, because 0.0/0.0 should still be a NaN, not an Infinity.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try this,

System.out.println(0.0/0);//NaN
System.out.println(-0/0.0);//NaN
System.out.println(0.0/0.0);//NaN
System.out.println(-0.0/0.0);//NaN
System.out.println(0.0/-0.0);//NaN
System.out.println(0.0/-0);//NaN
System.out.println(0.0/+0);//NaN
System.out.println(0.0f/0);//NaN
System.out.println(0.0f/-0);//NaN
System.out.println(0.0f/-0.0);//NaN
System.out.println(0.0f/0.0);//NaN
System.out.println(5.0f/0.0);// Infinity
System.out.println(5.0/0.0);// Infinity
System.out.println(-5.0/0.0);// -Infinity
System.out.println(5.0/-0.0);// -Infinity
System.out.println(-5.0/-0.0);// Infinity
 
Water proof donuts! Eat them while reading this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic