aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why 0/0 is an EXCEPTION and 0.0/0.0 is NaN ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why 0/0 is an EXCEPTION and 0.0/0.0 is NaN ?" Watch "why 0/0 is an EXCEPTION and 0.0/0.0 is NaN ?" New topic
Author

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

Nutan Choubey
Greenhorn

Joined: May 30, 2006
Posts: 2
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?
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

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.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Colin Wang
Greenhorn

Joined: Nov 24, 2010
Posts: 4
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.
samir vasani
Ranch Hand

Joined: Nov 24, 2010
Posts: 62
The above answers are not satisfactory,so please give the correct reason
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
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

Joined: Nov 24, 2010
Posts: 62
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
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

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
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

Haha, yeah, it is. While in the back of my head I know it works like that, I never stood still to appreciate it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why 0/0 is an EXCEPTION and 0.0/0.0 is NaN ?