float to double conversion

This is a discussion on float to double conversion within the C Programming forums, part of the General Programming Boards category; Given the following Code: include <stdio.h> int main(void) { int part1, part4; float part2; char part3[6]; char s[]="1234.56abc 903"; sscanf(s, ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195

    float to double conversion

    Given the following

    Code:
    include <stdio.h>
    
    int main(void)
    {
        int part1, part4;
        float part2;
        char part3[6];
        char s[]="1234.56abc  903";
    
    
        sscanf(s, "%2d%5e%5s%d", &part1, &part2, part3, &part4);
        printf("parse string: %s\n", s);
        printf("part1=%d\n", part1);
        printf("part2=%f\n", part2);
        printf("part3=%s\n", part3);
        printf("part4=%d\n", part4);
    
        return 0;
    
    }
    The corresponding output is:
    $./fl
    parse string: 1234.56abc 903
    part1=12
    part2=34.560001
    part3=abc
    part4=903

    The way I've always understood it, the 1 in part2 appears because the conversion from binary to decimal is only exact when dealing with a power of 2. This includes fractional parts.

    Now I change float to double

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int part1, part4;
        double part2;
        char part3[6];
        char s[]="1234.56abc  903";
    
    
        sscanf(s, "%2d%5le%5s%d", &part1, &part2, part3, &part4);
        printf("parse string: %s\n", s);
        printf("part1=%d\n", part1);
        printf("part2=%f\n", part2);
        printf("part3=%s\n", part3);
        printf("part4=%d\n", part4);
    
    
    
        return 0;
    
    
    }
    The corresponding output is now:
    $./fl
    parse string: 1234.56abc 903
    part1=12
    part2=34.560000
    part3=abc
    part4=903

    The question is why doesn't the 1 appear in part 2 anymore? Is this because double has a longer precision? If I would have the computer print out the decimal spot to say 16 spots, would the 1 appear again?

  2. #2
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    Never mind. I decided to try something really radical and have my computer print 17 decimal spots to see if the precision error would arise.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int part1, part4;
        double part2;
        char part3[6];
        char s[]="1234.56abc  903";
    
    
        sscanf(s, "%2d%20le%5s%d", &part1, &part2, part3, &part4);
        printf("parse string: %s\n", s);
        printf("part1=%d\n", part1);
        printf("part2=%.17f\n", part2);
        printf("part3=%s\n", part3);
        printf("part4=%d\n", part4);
    
    
    
        return 0;
    
    
    }
    The output:
    $./fl
    parse string: 1234.56abc 903
    part1=12
    part2=34.56000000000000227
    part3=abc
    part4=903

    It does.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    This is because something like 0.2 cannot be represented accurately in binary (ie. base 2), resulting in something like 0.199999999999527 or 0.20000000000105. Try representing 0.2 as a sequence of binary fractions (ie sum of 1/(2^k) ) and you'll find yourself with a troublesome infinite expansion. Don't count on floating-point numbers for total precision.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conversion From C++ To C
    By dicon in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 02:54 PM
  2. need some help with last part of arrays
    By Lince in forum C Programming
    Replies: 3
    Last Post: 11-18-2006, 08:13 AM
  3. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  4. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 05:54 PM
  5. Im stuck....
    By dAzed in forum C++ Programming
    Replies: 8
    Last Post: 10-11-2004, 04:50 PM

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