Search code examples
javafloating-accuracyfloating-point-precision

Is it possible to get floating point error in this case?


I know that float arithmetic is tricky, but I am not sure if it is possible to get a mistake if you have a division followed by the inverse multiplication. Written in code, is it possible, that this method will return false:

public boolean calculate(float a, float b) {
        float c = a / b;
        return (a == (c * b));
}

Solution

  • Simple answer is - yes. This example definitely returns false:

    public boolean alwaysFalse(){
            float a=Float.MIN_VALUE;
            float b=Float.MAX_VALUE;
            float c = a / b;
            return a == c * b;
    }
    

    Updated
    More general answer is that there are two cases when false happens in your method:
    1) when significand if overflowed (i.e. division evaluates to more digits than significand can hold)
    2) after exponent gets to its minimum so you can't divide further without loosing least significant bits of significand

    You may construct examples for 'a' that guarantee false - like when significand's most and least significant bits are 1 (binary): 10000...000001e10 etc