This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Float numbers division Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Float numbers division" Watch "Float numbers division" New topic
Author

Float numbers division

santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
Dear all as we know when a number divided by 0 the value will be infinity but in java when we divide a number by 0 we will get Exception in thread "main" java.lang.ArithmeticException: / by zero, but when we divide it by 0.0 we will get infinity why it is so like that any how 0.0 ==0 will be true.


Creativity is nothing but Breaking Rules
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

When you divide a byte, char, short, int or long by 0, integer division is used, and that does not allow dividing by 0.
When you divide a byte, char, short, int or long by 0.0 (which is a double), double division is used, and that does allow dividing by 0.

With all arithmetic operators in Java, the result has the type of the largest operator. For instance:
- float operator long => float
- int operator int => int
- int operator long => long

However, the result is never byte, char or short; in those cases it will be int. So:
- byte operator short => int


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14117
    
  16

It works like that, because that's how it is defined in the Java Language Specification...

If you divide and integer by 0, you are doing an integer division. This leads to an exception. If you divide a number by 0.0, you are doing a floating-point division, and this results in the special value Infinity.

Note that floating-point numbers have a special value to indicate infinity, while integers do not. Since the value "infinity" cannot be represented by an integer, the only sensible way is to throw an exception when you try to divide by 0.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296

Since the value "infinity" cannot be represented by an integer, the only sensible way is to throw an exception when you try to divide by 0


Dear all as you told in the above statement that infinity cannot be represented by an integer is there any reason for this..?
Because in mathematics if we divide any number by 0 or 0.0 the value will tends to infinity
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38513
    
  23
It has to do with the format of integral numbers. If you have a capacity of 32 bits, then the most efficient way to fill them is two's complement. It also matches the way computer chips carry out arithmetic. So there is no capacity for an infinite value.

Actually the behaviour of ints is better than that of doubles. The mathematicians do not say that 1 / 0 = infinity. They say 1 / 0 is undefined. So the integer behaviour, throwing an Exception, makes good mathematical sense.
santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
Thanks a lot
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38513
    
  23
You're welcome
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14117
    
  16

Campbell Ritchie wrote:Actually the behaviour of ints is better than that of doubles. The mathematicians do not say that 1 / 0 = infinity. They say 1 / 0 is undefined. So the integer behaviour, throwing an Exception, makes good mathematical sense.

A while ago I read a book about the history of scientific discoveries, and one of the subjects was how the number 0 was discovered, and what the meaning of dividing by zero was exactly. It took a surprisingly long time before mathematicians worked out what dividing by zero means.

Wikipedia has some information on it: http://en.wikipedia.org/wiki/Division_by_zero
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38513
    
  23
That's a nice article about division by 0. It tells us that "undefined" is the official result and why ∞ is the common approximation used.
 
wood burning stoves
 
subject: Float numbers division