The comparison of floating point numbers is not a problem and cannot be compared directly. precision is required.

Source: Internet
Author: User

Floating Point Number comparison

1. An error occurred while directly comparing links (=)

Floating point numbers can be compared, but some errors may occur due to the difference in precision between different floating point types.

Example 1
:

# Include <iostream. h>

Void main ()

{

Float
F1 = 7.123456789;

Float F2 = 7.123456787;

Cout <(F1! = F2 ?" Not same/N ":" same/N ");

Float G = 1.0/3.0;

Double D = 1.0/3.0;

Cout <(G = D ?" Same/N ":" not same/N ");

}

Running result:

Same // F1 is the same as F2

Not same // G is different from D

The first six valid digits of F1 and F2 are equal, while the numbers behind them are different. They may be expressed as the same number in the computer. Because Float Type Precision is limited, the difference cannot be distinguished, resulting in incorrect judgment (think it is the same number ). The solution is to use double data.

Example 2:

# Include <iostream. h>

# Include <math. h>

# Include <iomanip. h>

Void main ()

{

Double d1 = 123456789.9*9;

Double D2 = 1111111109.1;

Cout <(d1 = d2? "
Same/N ":" Not same/N ");

Cout <
(FABS (d1-d2) <1e-6 )? "Same/N": "Not same/N ");

Cout <setprecision (9 );

Cout <setiosflags (IOs: fixed) <d1 <"/N" <D2 <"/N ";

}

Running result:

Not same

Same

1111111109.100000143

1111111109.099999905

2. Use floating point numbers for equal (=) and not equal (! =) The comparison operation is usually problematic. The equal comparison of floating point numbers is generally determined by whether the values minus the two fall in the neighborhood of 0.

3. There is also an error in the relationship between a floating point value and zero.

As mentioned in Lin Rui's  
If (x> =-epsinon )&&
(X <= epsinon ))

 
Epsinon indicates the allowable error (accuracy ).

Comparison of floating point numbers with 0:

The reason for this is that if (FABS (I) <= 1e-6) is that for a number, in mathematics, as long as the number is smaller than any small positive number, we can say that this number is equal to zero. This is a concept related to the limit. If you want to learn more about the cause, you can look at the mathematical analysis.
Therefore, in computer programming, we use this method to compare the actual type, because this can achieve the desired precision, that is, FABS (I) it can be smaller than a larger or smaller number to confirm whether it is equal to zero.

Comparison of floating point and zero:

Equal to 0: FABS (I) <= 1e-6

Relationship greater than 0: I> 1e-6

Relationship less than 0: I <1e-6

For example, solving the quadratic equation of a single element

L
A = 0
It's not a quadratic equation, it's a single equation

L
B2-4ac = 0
There are two equal solid roots

L
B2-4ac> 0
There are two unequal real-roots

L
B2-4ac
There are two composite roots.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

// C code

# Include <stdio. h>

# Include <math. h>

Main ()

{

Double
A, B, C, disc, x1, x2, realpart, imagpart;


Printf ("enter a, B, c :");


Scanf ("% lf", & A, & B, & C );


If (FABS (a) <= 1e-6)

Printf ("not two equations, one equation/N ");

Else

{

Disc = B * B-4 * a * C;

If (FABS (disc) <= 1e-6)

Printf ("two equal solid roots: % F", (-B/(2 * )));

Else if (disc> 1e-6)

{

X1 = (-B + SQRT (disc)/(2 * );

X2 = (-B-SQRT (disc)/(2 * );

Printf ("two unequal Real Roots:/N ");

Printf ("X1 = % F/N", X1 );

Printf ("X2 = % F/N", X2 );

}

Else

{

Realpart =-B/(2 * );

Imagpart = SQRT (-disc)/(2 * );

Printf ("Two roots exist:/N ");

Printf ("% F + % fi/N", realpart, imagpart );

Printf ("% F-% fi/N", realpart, imagpart );

}

}

}

 

 

 

 

 

 

 

 

 

 

 

// C ++ code

# Include <iostream. h>

# Include <math. h>

# Include <iomanip. h>

Void main ()

{

Double
A, B, C, disc, x1, x2, realpart, imagpart;


Cout <"enter a, B, c :";


Cin> A> B> C;


If (FABS (a) <= 1e-6)

Cout <"not two equations,
Is 1 equation/N ";


Else


{

Disc = B * B-4 * a * C;

If (FABS (disc) <= 1e-6)

Cout <"has two equal solid roots:" <(-B/(2 * ));

Else if (disc> 1e-6)

{

X1 = (-B + SQRT (disc)/(2 * );

X2 = (-B-SQRT (disc)/(2 * );

Cout <"two unequal Real Roots:/N ";

Cout <setprecision (5) <"X1 =" <X1 <Endl;

Cout <setprecision (5) <"X2 =" <X2 <Endl;

}

Else

{

Realpart =-B/(2 * );

Imagpart = SQRT (-disc)/(2 * );

Cout <"Two Compound Roots:/N ";

Cout <setprecision (5) <realpart <"+" <imagpart <"I" <Endl;

Cout <setprecision (5) <realpart <"-" <imagpart <"I" <Endl;

}

}

}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.