float number is not the expected number after subtraction

前端 未结 4 584
傲寒
傲寒 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

提交回复
热议问题