Is it possible to get 0 by subtracting two unequal floating point numbers?

后端 未结 12 1801
余生分开走
余生分开走 2021-01-30 07:50

Is it possible to get division by 0 (or infinity) in the following example?

public double calculation(double a, double          


        
12条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-30 08:27

    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.

提交回复
热议问题