Solved

Puzzling atof...urgent!!! :(

Posted on 1998-09-18
12
396 Views
Last Modified: 2008-03-17
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
Comment
Question by:Haho
  • 3
  • 2
  • 2
  • +3
12 Comments
 
LVL 84

Expert Comment

by:ozo
Comment Utility
Is rtnval local to round_up?  How is round_up implemented?
0
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
Does your libc support  %lf  ?
Another problem could be your locale, see  man setlocale.
0
 
LVL 1

Author Comment

by:Haho
Comment Utility
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

by:Norbert
Comment Utility
Just thinking because you say rrtnval is local:
what about this function:
char* SomeBadFunc()
{
    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

by:ozo
Comment Utility
I'd suggest making rtnval static.
0
 
LVL 2

Expert Comment

by:duneram
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 2

Expert Comment

by:duneram
Comment Utility
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

by:
AnthonyJ earned 50 total points
Comment Utility
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

by:duneram
Comment Utility
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

by:AnthonyJ
Comment Utility
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

by:Haho
Comment Utility
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

by:Norbert
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now