# Float numbers division

santhosh.R gowda

Ranch Hand

Posts: 296

posted 6 years ago

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

posted 6 years ago

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

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 - OCEJPAD 6

How To Ask Questions How To Answer Questions

posted 6 years ago

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.

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

posted 6 years ago

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

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

Creativity is nothing but Breaking Rules

Campbell Ritchie

Sheriff

Posts: 48381

56

posted 6 years ago

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

Actually the behaviour of

`int`s is better than that of`double`s. 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

posted 6 years ago

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 wrote:Actually the behaviour ofints is better than that ofdoubles. 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