type limits

Discuss Programming

type limits

Postby worker201 » Tue Nov 09, 2004 4:49 pm

What is the best way to find out the maximum precision of a data type on my machine? Specifically double and float.

I mean, how many decimal places will each one calculate? I thought double was pretty high, but a program that is outputting doubles only gives 6 decimal places to the right of the decimal point, and I need like 8 or 9.

Of course I will be asking the maintainer of the program about this, but I wanted to know how to find this information if I ever need it. Using FC2, kernel 2.6.6-1.424, and glibc 2.3.3-27.1

Thanks for the help
worker201
guru
guru
 
Posts: 668
Joined: Sun Jun 13, 2004 6:38 pm
Location: Hawaii

Postby Void Main » Tue Nov 09, 2004 5:11 pm

What architecture? Can I assume i686?

[EDIT]

Sorry I was just leaving work when I asked the above question. Just got home and wrote a little program to show you the size of each type:

Code: Select all
int main() {
  printf("Integer Type sizes:\n\n");
  printf("       signed char: %d\n",sizeof(signed char));
  printf("     unsigned char: %d\n",sizeof(unsigned char));
  printf("         short int: %d\n",sizeof(short int));
  printf("unsigned short int: %d\n",sizeof(unsigned short int));
  printf("      unsigned int: %d\n",sizeof(unsigned int));
  printf("               int: %d\n",sizeof(int));
  printf("          long int: %d\n",sizeof(long int));
  printf("     long long int: %d\n",sizeof(long long int));
  printf("\n   Real Type sizes:\n\n");
  printf("             float: %d\n",sizeof(float));
  printf("            double: %d\n",sizeof(double));
  printf("       long double: %d\n",sizeof(long double));
}


Which when run on my i686 laptop yields these results:

Code: Select all
Integer Type sizes:
 
       signed char: 1
     unsigned char: 1
         short int: 2
unsigned short int: 2
      unsigned int: 4
               int: 4
          long int: 4
     long long int: 8
 
   Real Type sizes:
 
             float: 4
            double: 8
       long double: 12


These sizes are different depending on your architecture. Integer is pretty straight forward as far as precision vs size but floating point is not quite as easy:

http://www.astro.gla.ac.uk/users/norman ... 2b3c2.html

One other thing to check is the "output format" in the printf statements. Precision can also be altered at display output time in the printf statements.
See "man 3 printf":

http://voidmain.is-a-geek.net/man?mode= ... &section=3
User avatar
Void Main
Site Admin
Site Admin
 
Posts: 5705
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA

Postby worker201 » Wed Nov 10, 2004 2:38 pm

Neato - the first time I ever used gcc, sadly enough. My results (x86) were the same as yours.

Turns out that the problem was not in the precision, but a bug in the code, which the maintainer promptly rewrote. But the program you wrote is pretty cool anyway. Thanks for your help.
worker201
guru
guru
 
Posts: 668
Joined: Sun Jun 13, 2004 6:38 pm
Location: Hawaii


Return to Programming

Who is online

Users browsing this forum: No registered users and 2 guests