float number is not the expected number after subtraction

前端 未结 4 579
傲寒
傲寒 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:34

    Floats use the IEEE754 to represent numbers, and that system has some rounding errors.

    Floating point guide

    What Every Computer Scientist Should Know About Floating-Point Arithmetic

    Wikipedia on IEE754

    Bottom-line if you are doing arithmetic and it needs to be exact don't use float or double but us BigDecimal

    0 讨论(0)
  • 2020-12-19 16:38

    Java encodes real numbers using binary floating point representations defined in IEEE 754. Like all finite representations it cannot accurately represent all real numbers because there is far more real numbers than potential representations. Numbers which cannot be represented exactly (like 0.1 in your case) are rounded to the nearest representable number.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-19 16:48

    You cannot get exact results with floating point numbers. You might need to use a FixedPoint library for that. See : http://sourceforge.net/projects/jmfp/

    0 讨论(0)
提交回复
热议问题