-1

I tired printing Array and the end I got Infinity at last few values. What does Infinity means in Java. I am coding java in android studio.

The array is as follows aList [132.74362, 142.6408, 143.6408, 131.62733, 132.62733, 127.61245, 128.61246, 124.76465, 125.76465, Infinity, Infinity, Infinity, Infinity]

What does Infinity mean?

1 Answers1

2

Did you divide by zero?

When you divide by zero ...

In case of double/float division, the output is Infinity. The basic reason behind it is that it implements the floating point arithmetic algorithm, which specifies special values like “Not a number” OR “infinity” for “divided by zero cases” as per IEEE 754 standards.

In case of integer division, it throws ArithmeticException.

This, will print "Infinity"

    double p = 1; 
    System.out.println(p/0); 
Lotsa
  • 412
  • 4
  • 11
  • when you divide by zero, the output isn't infinity..its undefined. – Md Golam Rahman Tushar Mar 11 '20 at 06:23
  • 2
    Interesting. When I run those two lines, it prints "Infinity" – Lotsa Mar 11 '20 at 06:24
  • Sometimes when i print it prints as this : aList [132.74362, 142.6408, 143.6408, 131.62733, 132.62733, 127.61245, 128.61246, 124.76465, 125.76465 ,1.76433968E14, 1.45923158E14, 1.76433968E14, 1.45923158E14] Maybe it is not able to print precision value upto 14 – ambika gowda Mar 11 '20 at 07:37
  • @ambikagowda see https://stackoverflow.com/questions/588004/is-floating-point-math-broken – Scary Wombat Mar 11 '20 at 07:42
  • also note that "A floating-point operation that overflows produces a signed infinity." (JLS 4.2.4) - @MdGolamRahmanTushar you mean zero divided by zero, and it results in so called `NaN` (Not a Number) [JLS 4.2.3. Floating-Point Types, Formats, and Values](https://docs.oracle.com/javase/specs/jls/se13/html/jls-4.html#jls-4.2.3) – user85421 Mar 11 '20 at 07:44
  • @ambika largest float is `3.4028235e38f`, largest double `1.7976931348623157e308` – user85421 Mar 11 '20 at 07:52
  • @user85421 I am not sure what is the reason to print infinity – ambika gowda Mar 11 '20 at 08:40
  • @ambika see the answers in this question, they offer some insight. https://stackoverflow.com/questions/18542536/why-is-number-divided-by-zero-infinity-in-java – Lotsa Mar 11 '20 at 08:53
  • @ambika reason to **print** infinity: the values are infinity! According posted links and comments, reasons to get such a value: division by zero or exceeding the largest float/double - since we don't see how these values are being calculated, we cannot say why they result in inifinity – user85421 Mar 11 '20 at 11:22