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.
7 2868
Is it possible that the string your passing out to sprintf is not large enough to hold the whole string?
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.
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.
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.
Whats EndString() supposed to do?
Sorry, maybe I'm not helping you much...
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
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: BigMan |
last post by:
How can I check if assignment of a float to a double (or vice versa)
will result in loss of precision?
|
by: atreide_son |
last post by:
hello all...
yes i'm a newbie and i need help.
i have an assignment due for class, but I don't know i'm stuck and
can't get out from under myself.
here's the focus of the program:
Write a C program that allows the user to make some simple banking
|
by: drose0927 |
last post by:
Please help! I can't get my program to exit if the user hits the
Escape button:
When I tried exit(EXIT_SUCCESS), it wouldn't compile and gave me this
error:
Parse Error, expecting `'}''
'else if (choice == 27) exit(0) } }'
Here is my program (Simple loop to display currency equivalencies based
|
by: michael.mcgarry |
last post by:
Hi,
I have a question about floating point precision in C.
What is the minimum distinguishable difference between 2 floating point
numbers? Does this differ for various computers?
Is this the EPSILON? I know in float.h a FLT_EPSILON is defined to be
10^-5. Does this mean that the computer cannot distinguish between 2
numbers that differ by less than this epsilon?
|
by: cj |
last post by:
Thanks to everyone that has helped me. Now I'm trying to write my first
program. I have an example of one that I need to write about. Any help
getting me started is appreciated. I'm having trouble getting my
compiler to work, access to my a drive is denied, anyone know why this
is? Ok here is the info for my program: The two laws of electricity are
used in this program.
Law #1 The first is Ohm's Law which relates voltage, curret, and...
| |
by: c_beginner |
last post by:
yes, this is my how work question. Since I am lack in getting
an assistance with my lab work I put this in this advance group.
Sorry for the trouble I am making.
Write a program to calculate the simple interest.
#include<stdio.h>
int intrest(int rate,float amount);
int main(void)
{
|
by: Keo932 |
last post by:
Hello all,
I am finishing up my program to simulate a tollbooth by using classes. What happens is cars passing by the booth are expected to pay a fifty cent toll. The program keeps track of the number of cars that have gone by (paid and unpaid), and the total money collected.
The two data items are of type int to hold the total number of cars and type float to hold the total amount of money collected.
A constructor initializes both these...
|
by: artifact.one |
last post by:
Hello.
Is there any reliable way to determine the number
of digits of precision given by the float/double
type? I read somewhere that the C99 standard
guarantees six digits of precision with the float
type, although I can't seem to find this
information now.
A cursory search through the C99 pdf doesn't bring
|
by: Matthew |
last post by:
Hi,
I want to change the precision level of floating point variables and
calculations which is done in php.ini.
However the server I rent for my domain does not give me access to
php.ini, they say 'for security reasons'.
Can the precision level be changed by PHP code as needed?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |