Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Output is Giving me "Infinity"

The output for a Java program I wrote is giving me "Infinity" when I should be getting a number. I'm using floats, and I don't think my numbers are going out of the range of what floats can handle.

My question is would I ever get "Infinity" as the output if Java computes a value that is bigger than what the data type in use can handle? Or is "Infinity" only returned if I have accidentally divided by zero somewhere.

like image 588
Inertial Ignorance Avatar asked Jul 04 '26 19:07

Inertial Ignorance


1 Answers

The JLS answers this pretty explicitly.

A floating-point operation that overflows produces a signed infinity.

You are probably dividing by zero somewhere but you could check.

double b = 1.1*Double.MAX_VALUE;
System.out.println(b);

That shows up as infinity.

like image 148
matt Avatar answered Jul 07 '26 07:07

matt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!