?
Solved

Puzzling atof...urgent!!! :(

Posted on 1998-09-18
12
Medium Priority
?
403 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
[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
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +3
12 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1252855
Is rtnval local to round_up?  How is round_up implemented?
0
 
LVL 51

Expert Comment

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

Author Comment

by:Haho
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 3

Expert Comment

by:Norbert
ID: 1252858
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
ID: 1252859
I'd suggest making rtnval static.
0
 
LVL 2

Expert Comment

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

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

by:
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

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

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

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

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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 and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

771 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