When can I compare floating point numbers directly?

In the

Coursera algorithm class, there is a material (FAQ"s eighth question) that mentions that floating point numbers can be directly compared in this course project. The specific situation is as follows:
enter integers a and b in the range of [- 32 767, 32767] . Compare floating point numbers a1/b1 with a2/b2 directly to get the correct answer. The reason is that IEEE-754 specifies that floating-point budget results are represented by proximity values.

but I"m still not sure why the results can be compared directly by using proximity values. Although it is not a good habit to compare floating point numbers directly, under what circumstances can floating point numbers be compared directly?

Mar.31,2021

because of the result of dividing any two numbers in this range, the one with the least error is also more accurate than the floating point number.


floating-point numbers are also divided into direct comparison and indirect comparison.

for example, no matter how the ratio is equal:

        Float f1 = 0.01f;
        Float f2 = 0.09f;
        Float f3 = 0.1f;

        System.out.println(f3 == (f1 + f2)); // true
        System.out.println(f3.compareTo(f1 + f2)); // 0
        System.out.println(f3.equals(f1 + f2)); // true
The ratio of

to Double, is not equal in any way:

        Double d1 = 0.01d;
        Double d2 = 0.09d;
        Double d3 = 0.1d;

        System.out.println(d3 == (d1 + d2)); // false
        System.out.println(d3.compareTo(d1 + d2)); // -1
        System.out.println(d3.equals(d1 + d2)); // false
        System.out.println(Math.abs(d3 - (d1 + d2)) < Double.MIN_EXPONENT); // false
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b33bfc-34286.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b33bfc-34286.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?