The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Array of pointer initialization
Discuss Array of pointer initialization in the C Programming forum on Dev Shed. Array of pointer initialization C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

September 4th, 2012, 12:43 AM
|
|
|
|
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
|

September 4th, 2012, 12:59 AM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
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
|

September 4th, 2012, 01:34 AM
|
|
|
|
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?
|

September 4th, 2012, 02:13 AM
|
 |
Contributing User
|
|
|
|
|
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!
|

September 7th, 2012, 01:32 AM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: San Francisco Bay
|
|
Quote: | 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.
|

September 7th, 2012, 01:40 AM
|
|
|
Quote: | 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?
|

September 7th, 2012, 03:22 AM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: San Francisco Bay
|
|
Something like this?
c Code:
Original
- 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); } } 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; print_bits(&x, sizeof x); print_bits(&xf, sizeof xf); return 0; }
To interpret the output, this might be helpful: http://en.wikipedia.org/wiki/Floati...odern_computers
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|