C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 4th, 2012, 12:43 AM
mahaju mahaju is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2010
Posts: 120 mahaju User rank is Sergeant (500 - 2000 Reputation Level)mahaju User rank is Sergeant (500 - 2000 Reputation Level)mahaju User rank is Sergeant (500 - 2000 Reputation Level)mahaju User rank is Sergeant (500 - 2000 Reputation Level)mahaju User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Day 2 h 17 m 3 sec
Reputation Power: 20
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

Reply With Quote
  #2  
Old September 4th, 2012, 12:59 AM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,385 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 21 h 30 m 25 sec
Reputation Power: 4080
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

Reply With Quote
  #3  
Old September 4th, 2012, 01:34 AM
mahaju mahaju is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2010
Posts: 120 mahaju User rank is Sergeant (500 - 2000 Reputation Level)mahaju User rank is Sergeant (500 - 2000 Reputation Level)mahaju User rank is Sergeant (500 - 2000 Reputation Level)mahaju User rank is Sergeant (500 - 2000 Reputation Level)mahaju User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Day 2 h 17 m 3 sec
Reputation Power: 20
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?

Reply With Quote
  #4  
Old September 4th, 2012, 02:13 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,360 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 9 h 55 m 30 sec
Reputation Power: 383
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!

Reply With Quote
  #5  
Old September 7th, 2012, 01:32 AM
Lux Perpetua Lux Perpetua is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: San Francisco Bay
Posts: 1,936 Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 2 h 12 m 42 sec
Reputation Power: 1312
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.

Reply With Quote
  #6  
Old September 7th, 2012, 01:40 AM
mahaju mahaju is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2010
Posts: 120 mahaju User rank is Sergeant (500 - 2000 Reputation Level)mahaju User rank is Sergeant (500 - 2000 Reputation Level)mahaju User rank is Sergeant (500 - 2000 Reputation Level)mahaju User rank is Sergeant (500 - 2000 Reputation Level)mahaju User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Day 2 h 17 m 3 sec
Reputation Power: 20
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?

Reply With Quote
  #7  
Old September 7th, 2012, 03:22 AM
Lux Perpetua Lux Perpetua is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: San Francisco Bay
Posts: 1,936 Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 2 h 12 m 42 sec
Reputation Power: 1312
Something like this?
c Code:
Original - c Code
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. /* Comment this for big-endian byte order */
  5. #define LITTLE_ENDIAN
  6.  
  7. int binary_char(char *s, unsigned char c) {
  8.     int k;
  9.     for (k = 7; k >= 0; --k)
  10.         *(s++) = "01"[(c>>k)&0x1];
  11.     *s = '\0';
  12.  
  13.     return 8;
  14. }
  15.  
  16. void print_bits(void *p, int n) {
  17.     char buf[9];
  18.     int k;
  19.  
  20. #ifdef LITTLE_ENDIAN
  21.     for (k = n - 1; k >= 0; --k)
  22. #else
  23.     for (k = 0; k < n; ++k)
  24. #endif
  25.     {
  26.         unsigned char c = ((unsigned char *)p)[k];
  27.         binary_char(buf, c);
  28.         printf("%s ", buf);
  29.     }
  30. }
  31.  
  32. int main(int argc, char **argv) {
  33.     double x;
  34.     float xf;
  35.  
  36.     if (argc < 2) {
  37.         fprintf(stderr, "Usage: %s <float>\n",
  38.                 argv[0]);
  39.         exit(2);
  40.     }
  41.  
  42.     x = atof(argv[1]);
  43.     xf = (float)x;
  44.  
  45.     printf("double: ");
  46.     print_bits(&x, sizeof x);
  47.     printf("\nfloat: ");
  48.     print_bits(&xf, sizeof xf);
  49.     printf("\n");
  50.  
  51.     return 0;
  52. }
To interpret the output, this might be helpful: http://en.wikipedia.org/wiki/Floati...odern_computers

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Array of pointer initialization

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap