Thread: Data Types

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    208

    Data Types

    Hey all,

    So I am writing a scientific code and thus precision of the numbers I am dealing with is very important. I have a text file as follows containing coordinate data:

    Code:
       15.874010519682   15.874010519682   15.874010519682      0.77500000000000
         5.0000000000000D-03
        0.67184753150321   -9.0984212637474  -0.12308571224722
        -7.5391320707008   -5.4421377882969   -3.6434474261884
        -2.8072699927977  -0.27226324050591   0.83267141127818
        -3.4804472086739    1.7404956860775    3.6298663735869
        -6.1997156299320   -8.4300686752649    3.5753823495533
    Now I wrote a function to read this data (ignoring the first two lines) and print it out, just so I knew that the code was working correctly. Upon read out I only get:

    Code:
     First Line Header: 15.874011 15.874011 15.874011 0.775000 
     Second Line Header: 5.0000000000000D-03
    
    Coords:
    0.671848 -9.098421 -0.123086 
    -7.539132 -5.442138 -3.643447 
    -2.807270 -0.272263 0.832671
    Now this is substantial rounding that I need to get rid of. I am using long doubles and I thought the right format identifiers but nothing seems to be working (i.e. no matter what I change I keep getting the rounded numbers). Here is my code:

    Code:
    void getpoints(void)
    {
    	if((fp=fopen("daugh.1001","r"))==NULL)
    	 {
    	 printf("Cannot open file \n");
    	 exit(0);
    	 }
    	
    	fscanf(fp,"%Lf %Lf %Lf %Lf\n", &tmp1, &tmp2, &tmp3, &tmp4);
    	printf(" First Line Header: %Lf %Lf %Lf %Lf \n",tmp1,tmp2,tmp3,tmp4);
    	fgets(tmp, 25, fp);
    	printf(" Second Line Header: %s\n",tmp);
    	
    	for (k=0;k<3;k++)
        {
    		fscanf(fp,"%Lf %Lf %Lf \n", &x[k], &y[k], &z[k]);
        }
    	
    	printf("Coords:\n");
    	
    	for (k=0;k<3;k++){
    		printf("%Lf %Lf %Lf \n", x[k], y[k], z[k]);
    	}
    
    }
    It's probably something simple... any help you could lend would be greatly appreciated!

    Thanks!,
    Paddon
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by kas2002 View Post
    It's probably something simple...
    Yep. printf() by default rounds to six places. You can make it less this way:
    Code:
    printf("%.2lf",tmp1);
    I myself have never tried making it more than the default, but I assume it will work.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extending basic data types.
    By nempo in forum C++ Programming
    Replies: 23
    Last Post: 09-25-2007, 03:28 PM
  2. Replies: 4
    Last Post: 06-14-2005, 05:45 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM
  5. Using enumerated data types
    By SXO in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2001, 06:26 PM