• 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

why 0/0 is an EXCEPTION and 0.0/0.0 is NaN ?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why 0/0 is an EXCEPTION and 0.0/0.0 is NaN ?
Probably this is due to precision in float.
Can someone clarify the reason?
 
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An operation on two integers should produce an integer. NaN rarely makes sense for integers, and dividing by zero is often a sign of a mistake.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java doubles follow the IEEE specification. The IEEE specification for floating point defines special values for Infinity, Negative infinity, Negative zero, and NaN (not a number). These are actually defined numbers, along with operations that generate them.

Java integers follow the twos complement format. There are no special definitions for any of the above values. In this case, there is no representation of a NaN for an integer.

Henry
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:Java doubles follow the IEEE specification. The IEEE specification for floating point defines special values for Infinity, Negative infinity, Negative zero, and NaN (not a number). These are actually defined numbers, along with operations that generate them.

Java integers follow the twos complement format. There are no special definitions for any of the above values. In this case, there is no representation of a NaN for an integer.

Henry


Java follow the IEEE specification on double/float, but no specification on int.
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The above answers are not satisfactory,so please give the correct reason
 
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

samir vasani wrote:The above answers are not satisfactory,so please give the correct reason


Are you saying that you don't understand the answers, or that you think they are incorrect? If the former, what *do* you understand about them? If the latter, *why* do you think they're incorrect? (And please don't post in all-bold; it's annoying to read, and does nothing to make people want to answer.)
 
samir vasani
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lester Burnham wrote:

samir vasani wrote:The above answers are not satisfactory,so please give the correct reason


Are you saying that you don't understand the answers, or that you think they are incorrect? If the former, what *do* you understand about them? If the latter, *why* do you think they're incorrect? (And please don't post in all-bold; it's annoying to read, and does nothing to make people want to answer.)



Eleborate answers properly
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

samir vasani wrote:

Lester Burnham wrote:

samir vasani wrote:The above answers are not satisfactory,so please give the correct reason


Are you saying that you don't understand the answers, or that you think they are incorrect? If the former, what *do* you understand about them? If the latter, *why* do you think they're incorrect? (And please don't post in all-bold; it's annoying to read, and does nothing to make people want to answer.)



Eleborate answers properly



Lester's response wasn't to bust your chops. "Twos complement" is not something that can be explain in the paragraph. And IEEE 754 is even more complex -- much much much more complex. You are not going to get a "teach you from square one" response here. It is just not feasible.

However, if you get very very specific, maybe we can give you a one paragraph response, on one small detail. Otherwise, the best we can do is to tell you to google "twos complement" and "IEEE 754", the details for the integer and floating point format respectively.

Henry
 
Stephan van Hulst
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The simple answer is that the way the developers designed integers, there is no room for values such as NaN.

Here's an example: ints can holds values -2.147.483.648 to 2.147.483.647. Then all the possible combinations of bits are exhausted. There is no way to fit a value like NaN in there. The designers could have chosen to make the value -2.147.483.648 mean the same as NaN, but they chose not to, because it makes integer values unnecessarily complex (consider what values they would have to use to define NaN in short, byte and long as well, and how these values have to be converted when casting to another type).

Simply put, it's too complex and it's barely useful.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:The simple answer is that the way the developers designed integers, there is no room for values such as NaN.

Here's an example: ints can holds values -2.147.483.648 to 2.147.483.647. Then all the possible combinations of bits are exhausted. There is no way to fit a value like NaN in there. The designers could have chosen to make the value -2.147.483.648 mean the same as NaN, but they chose not to, because it makes integer values unnecessarily complex (consider what values they would have to use to define NaN in short, byte and long as well, and how these values have to be converted when casting to another type).

Simply put, it's too complex and it's barely useful.



There is also another really good reason why twos complement is designed the way it is.

At a logic level, operations are identical to its unsigned counterparts.... meaning if you take two signed numbers, and look at the bit pattens (using twos complement); then treating those bit patterns as unsigned numbers, and performing operations on them; the resultant bit pattern based on operations of those unsigned numbers are the same bit patterns that would have resulted, if you had did the operations using signed operations (using twos complement representation).

This is very important to a CPU, as it means that it can have a ALU that can process both signed and unsigned numbers, using a lot less die space -- as the same logic can be shared by both set of operations. This is very cool -- at least to a geek such as myself...

Henry
 
Stephan van Hulst
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Haha, yeah, it is. While in the back of my head I know it works like that, I never stood still to appreciate it.
reply
    Bookmark Topic Watch Topic
  • New Topic