Thread: Putting float numbers into a double array - strange problem

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    5

    Putting float numbers into a double array - strange problem

    So I have a double array, where I'm inputting float numbers to certain points in an array. Sometimes, the numbers that are printed out are completely different from what I put in.

    Here is the part of the code:

    Code:
    .
    .
    .
                           while( token != NULL )
                           {
    
                              num = atof(token);
                                 test[j][i] = num;
                                 printf( " \n%s, i is %d, j is %d\n", token,i,j );
                                printf( "number is %f \n value test of i,j is %f\n",num, test[i][j]);
                              token = strtok(NULL, ",");
                              i++;
                           }
     buffer[0] = 0; n = 0; i = 0; j++;
    .
    .
    .
    The test array is [8][17]
    The printouts are like so:

    0, i is 0, j is 0
    number is 0.000000
    value test of i,j is 0.000000

    0, i is 1, j is 0
    number is 0.000000
    value test of i,j is 0.000000

    0, i is 2, j is 0
    number is 0.000000
    value test of i,j is 0.000000

    0, i is 3, j is 0
    number is 0.000000
    value test of i,j is 2985268883875948100000000000000000.000000

    If someone could tell my why the float num prints out fine, but when put into an array becomes garbage?
    I'm taking string values from a csv file and turning them into floats, but no problems seem to crop up there.
    I reset i when appropriate and increment j when needed, so I don't think my problems are from incorrect array values (though they might be)

    Thanks.
    Last edited by Dogbert; 01-29-2014 at 05:11 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You'll have to decide whether it's test[i][j] or test[j][i] and change the other one to match.

  3. #3
    Registered User
    Join Date
    Jan 2014
    Posts
    5
    Will do thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array prints strange numbers?
    By vxs8122 in forum C Programming
    Replies: 5
    Last Post: 02-14-2013, 04:28 AM
  2. Strange Problem of double free corruption
    By newbinbin in forum C++ Programming
    Replies: 5
    Last Post: 09-28-2012, 06:28 AM
  3. double and float array type
    By a.mlw.walker in forum C Programming
    Replies: 1
    Last Post: 08-13-2011, 05:13 AM
  4. C Double Float and printf problem
    By SlyVixsky in forum C Programming
    Replies: 31
    Last Post: 08-01-2009, 01:28 AM
  5. Float and Double Numbers
    By luise.valencia in forum C Programming
    Replies: 7
    Last Post: 04-13-2005, 12:33 PM