Comparison of floating point numbers (2)

xiaoxiao2021-03-06  38

After writing the comparison of the upper floating point and two articles in the floating point memory structure

There is a new idea for floating point numbers.

We first look at the positive number

According to the memory structure of the IEEE, the index is high, the mantissa is low

When the floating point, the situation is also established when comparing its memory structure in an integer.

Therefore, if you compare them, as an integer calculation efficiency will be very high, such as

FLOAT F1 = 1.23;

Float F2 = 1.24

F1> F2 is established

(int & f1> (int &) F2 is also established

Moreover, carefully study the floating point structure of the IEEE, and it can be found that the problem of floating point numbers mentioned in the "Floating Distance Comparison" - not all floating point numbers can be accurately expressed

The number of floating points that can be accurately expressed is actually limited, that is, the various cases of IEEE have 2 ^ 32. Most of the accounts that cannot be expressed

IEEE In 32-bit cases, the mantissa is 23 (dark, the first number is 1)

For the floating point for accurate expression, if we make this 23 in an integer, it adds 1, it means that you can find the smallest floating point than the current corresponding floating point.

Conversely, we make two floating point numbers, corresponding integers, and get the integer indicate how many of the two floating point numbers can actually expose floating point number (very well understood in the same way as the corresponding index; When the index is different, it is also equally effective.

In this way, for two positive floating point numbers, their size can be compared with (int &) F1 - (int &) F2.

The difference between the difference should actually be relatively error.

This relative error, inequality relative error in universal meaning

It is expressed that there may be many floating point numbers between the two floating point numbers

This controls the two floating point numbers by specifying this threshold.

For two positive floating point numbers

Bool ISEqual (Float F1, Float F2, Int Absdelta)

{

IF (ABS (INT &) F1 - (INT &) F2)

}

It is also very large to use ABS instead of FABS. This is also very big.

The same is true for two negative numbers.

However, the integer plus the integer plus 1, the corresponding found is a smaller negative number.

However, direct comparisons are still not possible between negative numbers and integers, as the memory structure according to IEEE

Positive and negative numbers are different, the corresponding integers cannot be continuous

The minimum number of positive is 0, the corresponding integer is also 0x00000000

The minimum number of negative is -0, and the corresponding integer is 0x 80000000

Don't worry - 0

In the expression of IEEE, there are two 0, one is 0 one is -0

Interestingly, according to the judgment 0 and -0 of F1 == F2 is equal

By comparing we can find that

0 and positive floating point numbers can be directly compared according to the conversion into an integer

-0 and negative floating point numbers can be directly compared to the transition into an integer

If we can connect them, the direct comparison of the entire integer method is complete.

Compare the structure of a negative number, you can find a simple way:

Minute the integer corresponding to the negative memory-0, they will continue

And better results is that all negative numbers have been negative after this subtraction, and the corresponding integers are also negative.

This makes the entire integer become continuous, and it is effective in the entire floating point scale.

The final comparison algorithm is:

// Function: BOOL ISEQUAL (Float F1, Float F2, Int Absdelta)

// Features: Whether the two floating point numbers are approximately the same // input: F1, F2 Participate in two floating point numbers

// How many other floating point numbers that can be accurately expressed between the two floating point numbers are equivalent to relative errors.

// Output: True, two floating point numbers are equal; FALSE two floating point numbers are inequal

// Note: Just suitable for IEEE 32-bit floating point structure

Bool ISEqual (Float F1, Float F2, Int Absdelta)

{

INT I1, I2;

I1 = (f1> 0)? ((int &) F1): (INT &) F1 - 0x80000000);

I2 = (f2> 0)? (INT &) F2): (INT &) F2 - 0x80000000);

RETURN ((ABS (I1-I2))

}

转载请注明原文地址:https://www.9cbs.com/read-57877.html

New Post(0)