返回无穷大的字符串到双重函数导致嵌入式编译器 [英] String to double function returning infinity result in embedded compiler

查看:364
本文介绍了返回无穷大的字符串到双重函数导致嵌入式编译器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试执行字符串双重转换。在gnu c编译器中,我得到正确的值。但是如果我在嵌入式编译器(renesas CS +)中使用它,它会给出未定义的行为,如返回无穷大的结果。

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.

这是我的代码:

    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]!='\0';loop++)
    {
    str[loop] = a[loop];    
    }
    str[loop] = '\0';

        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");

    }

我在CS +编译器中使用的相同代码renesas微控制器)没有printf语句。当我在模拟器中运行这个代码,它返回无限值(d =无限值)。

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).

现在我更改了我的代码

double str_to_double_func(char str[])
{
  double result = 0.0;
  int len = 0;
  int pos = 0, n;
while(str[len]!='\0'){
    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. 

模拟器输出为
result = result 1.234567800000000E + 001 double(8)R6: REG,R7:REG

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

推荐答案

您必须初始化结果。当您定义

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;

您应该将变量初始化为

double result = 0.0;

内存检查器(如Valgrind)可以帮助您找到未初始化值的操作。

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

还有Sourav Ghosh指出的问题: sizeof 不会给你一个字符串的长度。您应该从< string.h> 中使用 strlen 。但是在您的情况下,您并不真的需要它,因为您已经确定在循环中复制字符串的长度。

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.

当然,您也应该将 pos 显式地初始化为零。否则,没有小数点的字符串可能无法正确解析。

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

(最后,复制字符串不会买任何东西,但会引入覆盖临时缓冲区的危险的30个字符。)

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

这篇关于返回无穷大的字符串到双重函数导致嵌入式编译器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆