program is getting stuck with precision of float variables

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • alphasahoo
    New Member
    • Jun 2008
    • 4

    program is getting stuck with precision of float variables

    Hi


    I am working on a program which writes the output a SQL select statements from number of source tables first to a load matrix and then writes to a load.dat file.

    But while writing to the load.dat file, the program gets stuck if I am putting float variables getting rounded to 8 or 9 decimal places.
    But runs fine if the float variable getting rounded to 6 decimal places.

    The code snippet is below:

    sprintf(outbuf, "%s/work/load.dat",SumHome);

    if((fp_tmp=
    fopen(outbuf,"w "))==NULL) {
    p_std_err("work file could not be opened for write\n");
    ErrorExit();



    case ORATYPE_FLOAT:
    if(p_CurCol->NullFlag) sprintf(EndStri ng(outbuf),"%c" ,Delim);
    else{

    sprintf(EndStri ng(outbuf),"%10.9f%c",
    *(double *)p_CurCol->DataLoc,Delim) ;
    }
    break;


    When I am putting %f ,the program is running fine.But when I put as 9 decimal places i,e %10.9f ,the program is getting stuck.

    Can anyone tell me if there is any relation between sprintf and precision of float variables. And why the program is getting stuck.

    Is there a limit on the size of the buffer.
  • jorba101
    New Member
    • Jun 2008
    • 86

    #2
    Is it possible that the string your passing out to sprintf is not large enough to hold the whole string?

    Comment

    • alphasahoo
      New Member
      • Jun 2008
      • 4

      #3
      May be because when I change the precison value of float to 6 the programs gets executed successfully.
      How to determine what is the mazimum length of the string that sprintf can take so that I will be sure this is the real problem.

      Comment

      • jorba101
        New Member
        • Jun 2008
        • 86

        #4
        No, I did not mean that.

        There is no maximum allowable length for sprintf (or at least, not to my knowledge). I think that sprintf can handle arbitrarily long strings, as long as the memory model supports it, which should be large enough for anything reasonable to fit in a string.

        The thing is that, I think that a possible reason for your failure could be that the string "EndString(outb uf)" maybe is not large enough to hold the data given back by sprintf.

        If that was the case, it would try to place the result in memory positions contiguous to your string, with unpredictable result (as getting stuck). Do a sizeof( EndString(outbu f) ) and check if it is large enough for what you're trying to fit in.

        Comment

        • alphasahoo
          New Member
          • Jun 2008
          • 4

          #5
          I calculated the size .

          The size of Endstring(outbu f) is 4 and the sizeof(outbuf) is 4000.
          Does it mean that the outbuf string can take a string of maximum 4000 bytes.
          If the float variable that I am trying to put into the outbuf string is more than 4000 bytes.

          Does the size of the float variable changes with precision.

          Comment

          • jorba101
            New Member
            • Jun 2008
            • 86

            #6
            Whats EndString() supposed to do?

            Sorry, maybe I'm not helping you much...

            Comment

            • alphasahoo
              New Member
              • Jun 2008
              • 4

              #7
              Hi

              Thanks so much for your help.
              Whatever reason you predicted for the program getting stuck is absolutely correct.

              The string outbuf is string of 4000 bytes but while the program was getting executed ,at some point of time the data coming into the outbuf string was more than 4000 bytes.
              Thats why the program was getting stuck.

              Increasing the precision value of float variables do increase the size occupied ,thats why when the precision of the float variable was 6 ,the program was successfully getting executed.

              And on increasing the precision to 9 ,the same data string is of larger size

              Comment

              • jorba101
                New Member
                • Jun 2008
                • 86

                #8
                Hi,

                Happy then that now it works.

                Just a brief remark: The size in memory needed to store a float never changes. The size of the string needed for representing it with an arbitrary precision does change with the precision.

                I mean, just to clarify that the problem had nothing to do with the float itself, but with the needed length of the string to represent it with the precision you require.

                Comment

                Working...