File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Float numbers division

 
santhosh.R gowda
Ranch Hand
Posts: 296
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20368
43
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
santhosh.R gowda
Ranch Hand
Posts: 296
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Pie
Posts: 47222
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 296
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot
 
Campbell Ritchie
Sheriff
Pie
Posts: 47222
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47222
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic