#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2010
    Posts
    121
    Rep Power
    21

    Array of pointer initialization


    Code:
        Fcomplex enhance_arg[half_FFT] = {{1.0,1.1},
                                          {2.0,2.1},
                                          {3.0,3.1},
                                          {4.0,4.1},
                                          {5.0,5.1},
                                          {6.0,6.1},
                                          {7.0,7.1},
                                          {8.0,8.1},
                                          {9.0,9.1},
                                          {10.0,10.1} };
    
        Fcomplex ubc_arg[OCTO_MAX_MIC_NUMBER-1][half_FFT] = {
                                                            { {1.0, 1.1},{2.0, 2.1},{3.0, 3.1},{4.0, 4.1},{5.0, 5.1},{6.0, 6.1},{7.0, 7.1},{8.0, 8.1},{9.0, 9.1},{10.0, 10.1}},
                                                            { {11.0, 11.1},{22.0, 22.1},{33.0, 33.1},{44.0, 44.1},{55.0, 55.1},{66.0, 66.1},{77.0, 77.1},{88.0, 88.1},{99.0, 99.1},{100.0, 100.1}},
                                                            { {111.0, 111.1},{222.0, 222.1},{333.0, 333.1},{444.0, 444.1},{555.0, 555.1},{666.0, 666.1},{777.0, 777.1},{888.0, 888.1},{999.0, 999.1},{1000.0, 1000.1}},
                                                            { {1111.0, 1111.1},{2222.0, 2222.1},{3333.0, 3333.1},{4444.0, 4444.1},{5555.0, 5555.1},{6666.0, 6666.1},{7777.0, 7777.1},{8888.0, 8888.1},{9999.0, 9999.1},{10000.0, 10000.1}}
                                                            };
    
    typedef struct FCOMPLEX
    {
    	float r,i;
    } Fcomplex;
    In this declaration, if i go into single step mode and view the value of ubc_arg and enhance_arg in the watch window, I can see that ubc_arg[0][0].r = 1 and ubc_arg[0][0].i = 1.10000002 while enhance_arg[5].r = 6 and enhance_arg[5].i = 6.0999999999

    This is true for any array index and not just these particular ones

    Why don't they have exact values in the watch window?

    Using gcc version 4.4.1 (TDM-2 mingw32), in CodeBlocks 10.05
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,595
    Rep Power
    4207
    This is actually a problem of representing floating point numbers and exists for all computers, irrespective of the programming language used. Remember that the computer stores everything in binary internally and certain numbers that are easily expressed in one base (e.g. decimal) cannot be exactly represented in another base (e.g. binary).

    See http://c-faq.com/fp/printfprec.html for more details.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2010
    Posts
    121
    Rep Power
    21
    I can understand why it is happening
    but I thought it took place only when we had to work on large precision numbers such as those with 8 or 10 digits after the decimal point
    I thought 1.1 was a fairly large number so would be displayed exactly
    So, would it be possible to list out (say, for eg, for numbers with 2 or 3 digits after decimal) those numbers which can be exactly represented?
    Can this is done using C code itself, or is there a table I can look up if I want?
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,710
    Rep Power
    480
    How, dude or dudette, please, would you exactly represent 1/10 (one tenth) (0.1) exactly in base 2 ?


    How would you represent 2/3 exactly in base 10?
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    San Francisco Bay
    Posts
    1,939
    Rep Power
    1313
    Originally Posted by mahaju
    I can understand why it is happening
    but I thought it took place only when we had to work on large precision numbers such as those with 8 or 10 digits after the decimal point
    I thought 1.1 was a fairly large number so would be displayed exactly
    So, would it be possible to list out (say, for eg, for numbers with 2 or 3 digits after decimal) those numbers which can be exactly represented?
    Can this is done using C code itself, or is there a table I can look up if I want?
    The only numbers that can be exactly represented as a float or double are of the form N/2^m, where N and m are integers. For example, 5/16 can be represented exactly because 16 is a power of two. However, using an example from your post, 1.1 = 11/10 cannot be written in the form N/2^m, so it cannot be represented exactly.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2010
    Posts
    121
    Rep Power
    21
    Originally Posted by Lux Perpetua
    The only numbers that can be exactly represented as a float or double are of the form N/2^m, where N and m are integers. For example, 5/16 can be represented exactly because 16 is a power of two. However, using an example from your post, 1.1 = 11/10 cannot be written in the form N/2^m, so it cannot be represented exactly.
    Thank you very much
    That was exactly what I was looking for

    Another question, is there a way I can view the exact contents of the memory register containing a float number? For example, if I have a variable float a = 5.1, how can I see the exact sequence of 0 and 1 in the 4 bytes of memory represented by 'a'? is there a way to printf the exact bit patterns to the screen?
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    San Francisco Bay
    Posts
    1,939
    Rep Power
    1313
    Something like this?
    c Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    /* Comment this for big-endian byte order */
    #define LITTLE_ENDIAN
     
    int binary_char(char *s, unsigned char c) {
        int k;
        for (k = 7; k >= 0; --k)
            *(s++) = "01"[(c>>k)&0x1];
        *s = '\0';
     
        return 8;
    }
     
    void print_bits(void *p, int n) {
        char buf[9];
        int k;
     
    #ifdef LITTLE_ENDIAN
        for (k = n - 1; k >= 0; --k)
    #else
        for (k = 0; k < n; ++k)
    #endif
        {
            unsigned char c = ((unsigned char *)p)[k];
            binary_char(buf, c);
            printf("%s ", buf);
        }
    }
     
    int main(int argc, char **argv) {
        double x;
        float xf;
     
        if (argc < 2) {
            fprintf(stderr, "Usage: %s <float>\n",
                    argv[0]);
            exit(2);
        }
     
        x = atof(argv[1]);
        xf = (float)x;
     
        printf("double: ");
        print_bits(&x, sizeof x);
        printf("\nfloat: ");
        print_bits(&xf, sizeof xf);
        printf("\n");
     
        return 0;
    }
    To interpret the output, this might be helpful: http://en.wikipedia.org/wiki/Floatin...dern_computers

IMN logo majestic logo threadwatch logo seochat tools logo