| |

VerySource

 Forgot password?
 Register
Search
View: 1411|Reply: 10

The problem of using double type variables in if. . .

[Copy link]

1

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-3-12 18:30:01
| Show all posts |Read mode
double ff = 1.0;
if (ff == 1) // precision
{
cout << "ok" << endl;
}
else
{
cout << "cancel" << endl;
}
Why is the output ok instead of cancel?
Thank you!
Reply

Use magic Report

0

Threads

9

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

Post time: 2020-6-7 10:00:02
| Show all posts
Generally, it is compared with 0.9999999999 1.0000001 for size, no == comparison
Reply

Use magic Report

0

Threads

63

Posts

43.00

Credits

Newbie

Rank: 1

Credits
43.00

 China

Post time: 2020-6-7 13:30:01
| Show all posts
Example of comparison with zero:

const float EPSINON = 0.00001; // here is determined according to the accuracy
if ((x >=-EPSINON)&&(x <= EPSINON))
Reply

Use magic Report

1

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-6-30 11:30:01
| Show all posts
You misunderstood what I meant, I know that due to accuracy issues,
if ((x >=-EPSINON)&&(x <= EPSINON))

double ff=1.0;
if(ff==1)//Precision, but due to accuracy problems, this should not be true here! , But the procedure is considered to be established
{
cout<<"ok"<<endl;
}
else
{
cout<<" cancel"<<endl;
}
//Why is the output ok instead of cancel?
Thank you!
Reply

Use magic Report

0

Threads

5

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-6-30 23:15:01
| Show all posts
It is normal to be established, and it is normal to not be established, so we should avoid that.
Reply

Use magic Report

0

Threads

6

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-7-20 16:15:02
| Show all posts
Double type data cannot be directly compared in this way. I also encountered this problem when writing programs.
Reply

Use magic Report

0

Threads

57

Posts

27.00

Credits

Newbie

Rank: 1

Credits
27.00

 Argentina

Post time: 2020-7-28 13:30:01
| Show all posts
You are like writing directly
if(0.1f == 0.1f)
This comparison can be true. The floating-point number is actually a binary data string, and of course it can be equal. It is said that its imprecision is reflected in
1/3 is such a numerical representation
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 United States

Post time: 2020-7-28 20:30:01
| Show all posts
if(ff==1)
Comparison can only be performed between variables of the same type, so the compiler will first convert 1 to double, and then compare.
Reply

Use magic Report

1

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-8-7 03:45:01
| Show all posts
But I think if (ff==1) this sentence is definitely not true, because double can only hold 10 significant digits, how can all the digits after 10 be guaranteed to be 0?
However, the compiler thinks that if (ff==1) is valid after 100 cycles of comparison.
Reply

Use magic Report

0

Threads

57

Posts

27.00

Credits

Newbie

Rank: 1

Credits
27.00

 China

Post time: 2020-8-7 09:45:02
| Show all posts
1.0f is expressed in memory as
00 00 80 3F Four bytes of data, of course, can have the same value, as long as the representation of another floating point number is also such four bytes.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list