Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将字符串转换为双精度时出现算术错误_C++_String_Double - Fatal编程技术网

C++ 将字符串转换为双精度时出现算术错误

C++ 将字符串转换为双精度时出现算术错误,c++,string,double,C++,String,Double,我正在编写一个函数,将用户提供的字符串转换为double。它对某些值非常有效,但对其他值无效。比如说 string_to_double("123.45") = 123.45 string_to_double(12345) = 12345 但是 我相当确定这是某种舍入误差,但我没有使用近似值,也没有使用非常小或非常大的值。我的问题有两个方面:为什么我会得到这些奇怪的结果?如何更改代码以获得更准确的结果?我这样做也是个人的挑战,所以建议使用诸如std::stod之类的方法是没有帮助的。我相信问题发

我正在编写一个函数,将用户提供的字符串转换为double。它对某些值非常有效,但对其他值无效。比如说

string_to_double("123.45") = 123.45
string_to_double(12345) = 12345
但是

我相当确定这是某种舍入误差,但我没有使用近似值,也没有使用非常小或非常大的值。我的问题有两个方面:为什么我会得到这些奇怪的结果?如何更改代码以获得更准确的结果?我这样做也是个人的挑战,所以建议使用诸如
std::stod
之类的方法是没有帮助的。我相信问题发生在第二个for循环中,但我觉得包含整个方法是明智的,因为如果我遗漏了一些内容,那么就不会有太多额外的代码需要阅读

我的代码

template <class T>
double numerical_descriptive_measures<T>::string_to_double(std::string user_input)
{
    double numeric_value = 0;//Stores numeric value of string. Return value.
    int user_input_size = user_input.size();
    int power = 0;
    /*This loop is for the characteristic portion of the input
    once this loop finishes, we know what to multiply the
    characterstic portion by(e.g. 1234 = 1*10^3 + 2*10^2 + 3*10^1 + 4)
    */
    for(int i = 0;i < user_input_size;i++)
    {
        if(user_input[i] == '.')
            break;
        else
            power++;
    }
    /*This loop is for the mantissa. If this portion is zero, 
    the loop doesn't execute because i will be greater than
    user_input_size.*/
    for(int i = 0;i < user_input_size;i++)
    {
        if(user_input[i] != '.')
        {
            numeric_value += ((double)user_input[i] - 48.0)*pow(10,power-i-1);
        }
        else
        {
            double power = -1.0;
            for(int j = i+1;j < user_input_size;j++)
            {
                numeric_value += ((double)user_input[j] - 48.0)*pow(10.0,power);
                power = power-1.0;
            }
            break;
        }
    }
    return numeric_value;
}
模板
双数值\描述性\度量::字符串\到\双(标准::字符串用户\输入)
{
double numeric_value=0;//存储字符串的数值。返回值。
int user_input_size=user_input.size();
整数幂=0;
/*该回路用于输入的特征部分
一旦这个循环完成,我们就知道要将
字符部分by(例如1234=1*10^3+2*10^2+3*10^1+4)
*/
对于(int i=0;i
 
  • String 在最多K次编辑中将N个字符串转换为公共目标字符串
  • String Pascal错误中的字符串
  • String 如何将此字符串的数字放入数据库?
  • String hitTestObject ActionScript3显示实例名称的通配符
  • String “中的复选框”;0“;至;1“;一串
  • String AWK文本文件比较(包含';b';文件和';文件中的字符串的次数)
  • String If字符串包含in-case语句
  • String JSF EL字符串比较
  • String 使用while循环将英语短语转换为拉丁语
  • String 第二次使用后跳过Cin.getline
  • String 如何访问存储在此对象中的数据?
  • String 为什么Cppcheck没有检测到以下格式字符串易受攻击的代码?
  • String 在较大字符串中查找起始点未知且长度不确定的子字符串
  • String 如何在Matlab对话框中输入序列和间隔
  • String t和*t之间的区别
  • String VB.net检查由引号封装的子字符串的字符串
  • String 版本和pkgname提取