By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,718 Members | 1,176 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,718 IT Pros & Developers. It's quick & easy.

program is getting stuck with precision of float variables

P: 4
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(EndString(outbuf),"%c",Delim);
else{

sprintf(EndString(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.
Jun 25 '08 #1
Share this Question
Share on Google+
7 Replies

P: 86
Is it possible that the string your passing out to sprintf is not large enough to hold the whole string?
Jun 25 '08 #2

P: 4
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.
Jun 25 '08 #3

P: 86
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(outbuf)" 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(outbuf) ) and check if it is large enough for what you're trying to fit in.
Jun 25 '08 #4

P: 4
I calculated the size .

The size of Endstring(outbuf) 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.
Jun 25 '08 #5

P: 86
Whats EndString() supposed to do?

Sorry, maybe I'm not helping you much...
Jun 25 '08 #6

P: 4
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
Jun 27 '08 #7

P: 86
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.
Jun 27 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.