missing output with for loop?!?!?

This is a discussion on missing output with for loop?!?!? within the C Programming forums, part of the General Programming Boards category; hello again. this is a real n00b question but why doesn't the "1.0" show up with this code? Code: #include ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    19

    Question missing output with for loop?!?!?

    hello again. this is a real n00b question but why doesn't the "1.0" show up with this code?
    Code:
    #include <stdio.h>
    
    main()
    {
      float x;
    
      for( x = 0; x <= 1.0; x += .1 )
        printf( "%.1f  ", x );
    
      printf("\n");
    }
    all i get is this:
    Code:
    0.0  0.1  0.2  0.3  0.4  0.5  0.6  0.7  0.8  0.9
    I can't think of why it doesn't work if it satisfies all the conditions (i think).

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    134
    try this .....

    Code:
    #include <stdio.h>
    
    main()
    {
      float x;
    
      for( x = 0; x <= 1.1; x += .1 )
        printf( "%.1f  ", x );
    
      printf("\n");
    }

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    19
    no, i want to know why it does that. why it doesn't print out the 1.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Ahh, the inaccuracies of floating point arithmatic.
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      float x;
    
      for( x = 0; x <= 1.0; x += .1 )
        printf( "%.20f\n", x );
    
      printf("%.20f\n", x);
      
      return 0;
    }
    
    /*
    
    Output
    
    0.00000000000000000000
    0.10000000149011611938
    0.20000000298023223880
    0.30000001192092895510
    0.40000000596046447760
    0.50000000000000000000
    0.60000002384185791020
    0.70000004768371582030
    0.80000007152557373040
    0.90000009536743164060
    1.00000011920928955080
    
    */
    http://docs.sun.com/source/806-3568/ncg_goldberg.html
    http://www.google.com/search?ie=utf-...t+inaccuracies
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Quote Originally Posted by n00by
    hello again. this is a real n00b question but why doesn't the "1.0" show up with this code?
    Code:
    #include <stdio.h>
    
    main()
    {
      float x;
    
      for( x = 0; x <= 1.0; x += .1 )
        printf( "%.1f  ", x );
    
      printf("\n");
    }
    all i get is this:
    Code:
    0.0  0.1  0.2  0.3  0.4  0.5  0.6  0.7  0.8  0.9
    I can't think of why it doesn't work if it satisfies all the conditions (i think).
    Try changing it from float to double. But before you do, if you have a debugger, step through the program and each time x is incremented, check what the value is. You will find that by the time it gets to 1, it is actually 1.000012 or something like that - it is more than one anyway.

    [edit] - ah, Hammer you type too fast.. Beaten yet again.

  6. #6
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    To fix the problem, change it to x < 1.05

  7. #7
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Quote Originally Posted by sean_mackrory
    To fix the problem, change it to x < 1.05
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        double x = 0.0;
    
        for (x = 0.0; x <= 1.0; x += 0.1)
            printf("%0.1f\n", x);
        return 0;
    }
    
    output:
    
    0.0
    0.1
    0.2
    0.3
    0.4
    0.5
    0.6
    0.7
    0.8
    0.9
    1.0
    What's wrong with that?

    ~/

  8. #8
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Oh... I never saw your post suggesting the double change. I'm downloading Dev-C++ over a 56k line (not to mention AOL) and it's taking several minutes to load pages.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 03:08 PM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 02:04 PM
  3. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 01:17 AM
  4. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  5. odd errors from msvc std library files
    By blight2c in forum C++ Programming
    Replies: 6
    Last Post: 04-30-2002, 01:06 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21