Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
137 views
in Technique[技术] by (71.8m points)

c - String to double function returning infinity result in embedded compiler

I'm trying to perform string to double conversion. In gnu c compiler I'm getting correct values. But if I use it in my embedded compiler (renesas CS+) it is giving undefined behavior like returning infinity result.

Here is my code:

    double str_to_double_func(char a[])
    {
    char str[30] = {'0'};   
    int loop ;
    double result;
    int len ;
    int pos,n;
    for(loop = 0;a[loop]!='';loop++)
    {
    str[loop] = a[loop];    
    }
    str[loop] = '';

        pos = 0;

      len = sizeof(str)-1;
       for (n = 0; n < len; n++)
       {
         if (str[n] == '.')
         {
           pos = len - n  - 1;
         }
         else
         {
           result = result * 10.0f + (str[n]-'0');
         }
       }
       while ( pos--)
       {
         result = result/10.0f;
       }
       return result;
     }
   void geo_app_main()
    {
    double d,i;

    d = str_to_double_func("12.345678");

    }

This same code I'm using in my CS+ compiler (renesas microcontroller) without printf statements. When i run this code in simulator it is returning infinite value (d = infinite value).

Now i changed my code

double str_to_double_func(char str[])
{
  double result = 0.0;
  int len = 0;
  int pos = 0, n;
while(str[len]!=''){
    len++;
}

  for (n = 0; n < len; n++)
  {
    if (str[n] == '.')
    {
      pos = len - n  - 1;
    }
    else
    {
      result = result * 10.0f + (str[n]-'0');
    }
  }

  while ( pos--)
  {
    result = result/1.0f;
  }
  return result;   

}

 Here the problem is getting 1.234567800000000E+001 instead of 12.345678 in my CS+(renesasa micro) compiler.i checked this code with cigwin compiler.There i'm getting correct output. 

the simulator output is result=result 1.234567800000000E+001 double(8)R6:REG, R7:REG

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You must initialise your result. When you define

double result;

it is not guaranteed to be zero. It can have any value and will probably be garbage. Especially if every subsequent operation relies on result having a valid value:

result = result * 10.0f + (str[n]-'0');
...
result = result/10.0f;

You should initialise your variable to

double result = 0.0;

A memory checker such as Valgrind can help you to find operations on uninitialised values.

There's also the issue Sourav Ghosh pointed out: sizeof does not give you then length of a string. You should use strlen from <string.h> for that. But in your case, you don't really need it, because you already determine the length when you copy the string in loop.

And, of, course, you should explicitly initialise pos to zero as well. Otherwise, strings without a decimal point may not be parsed correctly.

(Finally, copying the string doesn't buy you anything, but introduces the danger of overwriting the temporary buffer of 30 chars.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...