Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 20th, 2008, 7:34 AM   #1
Lakrids
Newbie
 
Join Date: Dec 2007
Posts: 28
Rep Power: 0 Lakrids is on a distinguished road
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)
  1. #include <iostream>
  2.  
  3. int main() {
  4.  
  5. for (double C=0.0; C<100.0; C+=0.1)
  6. if (C==1.0)
  7. std::cout << C << std::endl;
  8.  
  9. return 0;
  10. }

The program always evaluates (C==1.0) as false. Why?
Lakrids is offline   Reply With Quote
Old Mar 20th, 2008, 8:13 AM   #2
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 550
Rep Power: 4 Ancient Dragon is on a distinguished road
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;
}
__________________
True Terror is to wake up one morning and discover that your high school class is running the country - Kurt Vonnegut Jr.
Ancient Dragon is offline   Reply With Quote
Old Mar 20th, 2008, 1:32 PM   #3
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,039
Rep Power: 5 lectricpharaoh will become famous soon enough
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).
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote
Old Mar 20th, 2008, 2:51 PM   #4
Lakrids
Newbie
 
Join Date: Dec 2007
Posts: 28
Rep Power: 0 Lakrids is on a distinguished road
Re: Extremely weird program behavior

Thank you!
Lakrids is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:12 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC