Solved

Puzzling atof...urgent!!! :(

Posted on 1998-09-18
Medium Priority
406 Views
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
Question by:Haho
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 3
• 2
• 2
• +3

LVL 84

Expert Comment

ID: 1252855
Is rtnval local to round_up?  How is round_up implemented?
0

LVL 51

Expert Comment

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

LVL 1

Author Comment

ID: 1252857
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

LVL 3

Expert Comment

ID: 1252858
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

LVL 84

Expert Comment

ID: 1252859
I'd suggest making rtnval static.
0

LVL 2

Expert Comment

ID: 1252860
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

LVL 2

Expert Comment

ID: 1252861
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

Accepted Solution

AnthonyJ earned 200 total points
ID: 1252862
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

LVL 2

Expert Comment

ID: 1252863
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

Expert Comment

ID: 1252864
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

LVL 1

Author Comment

ID: 1252865
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

LVL 3

Expert Comment

ID: 1252866
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

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to useâ€¦
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and infâ€¦
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
Suggested Courses
Course of the Month15 days, 2 hours left to enroll