• Status: Solved
• Priority: Medium
• Security: Public
• Views: 411

# Puzzling atof...urgent!!! :(

hi guys,
I have a puzzling atof conversion here.. on Unix
I have a custom round up function :
char * round_up(value, noofdecimal)
char *value;   /* val to round up*/
int noofdecimal; /* no of decimals */
{
...... code......
return (&rtnval);
}
and I call the function by :
double var1;
var1 = atof( round_up(value, nodec));
printf ("%lf", var1);
THE PROBLEM IS:
Let us assign value = 2.55555 , nodec = 3
Right answer should be 2.55600 but it prints
2.55000 . Anything after the 2 decimal place is zeroes..  such as 2.33000, 1.99000, 4.56000
The problem is solved when :
char str[20];
strcpy (str, round_up(value,nodec));
var1 = atof(str);
printf ("%lf", var1)
...
By copying to another string and then only converting,
the answer is correct, ie. 2.55600 and not 2.55000
Why???
Thanks a lot!!!!

David
0
Haho
• 3
• 2
• 2
• +3
1 Solution

Commented:
Is rtnval local to round_up?  How is round_up implemented?
0

Commented:
Does your libc support  %lf  ?
Another problem could be your locale, see  man setlocale.
0

Author Commented:
rrtnval is local to the function and yes, %lf is supported by the library..
round_up is implemented by manipulating the value converted into a array of strings...  not very efficient, but I am experimenting with macros with functions from the <math.h> library.
0

Commented:
Just thinking because you say rrtnval is local:
{
char txt[20];
strcpy(txt,"Hello world");
return txt;
}
All seems to be fine but at the and of this function the variable txt goes out of scope.
txt is generated on the stack the stack pointer is moved but the string and the adress of the string is valid until some other function ocupies the same stack position.
so strcpy may work because the stack position where the string  is still  pseudo valid.
atof mybe needs a larger stack frame and destroys something of the string.

0

Commented:
I'd suggest making rtnval static.
0

Commented:
ozo I was going to make the same suggestion.  if you make it static, it won't get lost when the function becomes out of scope.   Usually (it varies by platform) when a program loads, static vars are loaded very close to the beginning of the stack and are available for everyone's use..

0

Commented:
Definitely looks like the variable is out of scope (the address you are referencing on the stack is invalid) by the time the strcpy occurs.
0

Commented:
Norbert is right. You are relying on the implementation of the libraries, and how the use the stack to get the answer you need. ozo's solution stops the function being re-entrant. This does not need to be a problem and it will work, and his solution can be useful.

I try to keep functions re-entrant so, the first thing I would consider is whether the data in *value* can be changed. If thats ok, then I would do all the work on *value* and then return it instead of having *rtnval*.

If thats not ok, then I would add another parameter to the function, and encapsulate the strcpy() inside round_up(). My guess is that this is what you are trying to avoid, I 've been there myself almost exactly.

Note: re-writing your function it is possible not to call strcpy()

char * round_up(value, noofdecimal)
char *value;   /* val to round up*/
int noofdecimal; /* no of decimals */
char *rtnval;
{
strcpy( rtnval, value)
...... code......
return (rtnval);
}

and call the function by :

double var1;
char str[20]

var1 = atof( round_up(value, nodec, str));
printf ("%lf", var1);

0

Commented:
I think Norbert should have posted the solution.  The danger to doing the strcpy inside your function is you could easily overwrite the boundary of your char * unless you also pass a parm that limits/defines the max length allowed....

Inside your function you simply have a ptr to a character for the return value.  What if it's NULL, or what if it only has space for 3 char (nULL included) and you copy in 6 chars?

0

Commented:
duneram good points.

Either you can make assumptions about how many chars are available - and tell everyone else who uses the funtction with comments in the header file, or you can add another parameter giving the size of the buffer going in.

As for a NULL pointer, that needs to be handled both inside and outside round_up. Inside it is handled to stop round_up() crashing, and outside to stop atof() crashing, though you might get lucky, depending on the implementation.
0

Author Commented:
Thanks !!! I have learned quite a bit from all of u :)
I haven't implemented the changes but I will soon do it....
to norbert, a special thanks for u and I would love to give u points but 50 points isn't hell of a lot and I am almost out of points.. :)
0

Commented:
If You want to give Points to me you can ask Linda (Expert Exchange Topic) for doing so. It then will not cost your own Points.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.