float number is not the expected number after subtraction

前端 未结 4 603
傲寒
傲寒 2020-12-19 15:53

I have the following statement:

float diff = tempVal - m_constraint.getMinVal();

tempVal is declared as a float and the

4条回答
  •  暖寄归人
    2020-12-19 16:41

    Because of the way they store values internally, floats and doubles can only store completely accurately numbers which can be decomposed into a sum of powers of 2 (and then, within certain constraints relating to their absolute and relative magnitude).

    So as soon as you attempt to store, or perform a calculating involving, a number which cannot be stored exactly, you are going to get an error in the final digit.

    Usually this isn't a problem provided you use floats and doubles with some precaution:

    • use a size of floating point primitive which has "spare" digits of precision beyond what you need;

    • for many applications, this probably means don't use float at all (use double instead): it has very poor precision and, with the exception of division, has no performance benefit on many processors;

    • when printing FP numbers, only actually print and consider the number of digits of precision that you need, and certainly don't include the final digit (use String.format to help you);

    • if you need arbitrary number of digits of precision, use BigDecimal instead.

提交回复
热议问题