![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Dec 2007
Posts: 28
Rep Power: 0
![]() |
Extremely weird program behavior
Hello,
I have been programming for quite a while now, but the following behavior is one of the most weird ones I ever encountered. It really is annoying me, and I just can't get to the bottom of it. So, in short, the other day I was writing a very small program which calculates all possible solutions to a given problem. The program didn't work, and after some debugging, I was astonished that the following code just doesn't seem to work for some obscure reason. The problem is that nothing is written to the screen in the loop : cplusplus Syntax (Toggle Plain Text)
The program always evaluates (C==1.0) as false. Why? |
|
|
|
|
|
#2 |
|
PFO God In Training
![]() Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 489
Rep Power: 4
![]() |
Re: Extremely weird program behavior
Never use equality == when working with doubles because as you have found out it will frequently not evaluate correctly due to the way floating point values are stored in memory. More than likely what is happening in your case is that there are some very small decimal places that you did not intend to be there.
Here's how to make it work int main() {
for (double C=0.0; C<100.0; C+=0.1)
if( C > 1.0 && C < 1.2)
std::cout << C << "\n";
return 0;
}
__________________
I Like Ike. Vote for Dwight Eisenhower this November. --This message brought to you by the the Procrastinators Club Of America. |
|
|
|
|
|
#3 |
|
Caffeinated Neural Net
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 889
Rep Power: 4
![]() |
Re: Extremely weird program behavior
To expand on what Ancient Dragon said, you need to remember that it's binary floating point. There are some numbers that cannot be exactly represented, and 0.1 is one of those (much like one-third cannot be exactly represented in decimal).
__________________
A man's knowledge is like an expanding sphere, the surface corresponding to the boundary between the known and the unknown. As the sphere grows, so does its surface; the more a man learns, the more he realizes how much he does not know. Hence, the most ignorant man thinks he knows it all. - L. Sprague de Camp |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Dec 2007
Posts: 28
Rep Power: 0
![]() |
Re: Extremely weird program behavior
Thank you!
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| hello, I'd like to write a program for my work. | blake_jl | Community Introductions | 13 | Nov 23rd, 2007 4:31 PM |
| Language display in program | Prm753 | C++ | 3 | May 30th, 2006 5:45 PM |
| Creating a program to test a program | sixstringartist | C | 8 | Jan 21st, 2006 1:15 PM |
| weird output in my server program | nindoja | C++ | 21 | Nov 24th, 2005 9:31 AM |
| airport Log program using 3D linked List : problem reading from file | gemini_shooter | C++ | 0 | Mar 2nd, 2005 4:12 PM |