Is it possible to get division by 0 (or infinity) in the following example?
public double calculation(double a, double
I can think of a case where you might be able to cause this to happen. Here's an analogous sample in base 10 - really, this would happen in base 2, of course.
Floating point numbers are stored more or less in scientific notation - that is, instead of seeing 35.2, the number being stored would be more like 3.52e2.
Imagine for the sake of convenience that we have a floating point unit that operates in base 10 and has 3 digits of accuracy. What happens when you subtract 9.99 from 10.0?
1.00e2-9.99e1
Shift to give each value the same exponent
1.00e2-0.999e2
Round to 3 digits
1.00e2-1.00e2
Uh oh!
Whether this can happen ultimately depends on the FPU design. Since the range of exponents for a double is very large, the hardware has to round internally at some point, but in the case above, just 1 extra digit internally will prevent any problem.