C ++精度:字符串到双精度 [英] C++ Precision: String to Double

查看:166
本文介绍了C ++精度:字符串到双精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在对转换后的字符串执行一些操作之后遇到double的精度问题。

  #include < iostream> 
#include< sstream>
#include< math.h>

using namespace std;

//转换函数
void convert(const char * a,const int i,double& out)
{

double val;

istringstream(a);
在>> val;

cout<< char a - << a<< endl;
cout<< val -----< val<< endl;

val * = i;

cout<< modified val ---<< val<< endl;
cout<< FMOD -----<< fmod(val,1)< endl;

out = val;

return 0;

}

字符串,因此错误不是常数。
它只影响一些数字(34.38似乎是不变的)。



在分钟,当我传入a = 34.38和i = 100 :

  char a  -  34.38 
Val ----- 34.38
modified val --- 3438
FMOD ----- 4.54747e-13

因为有较低的精度,但我需要一个双。



这也是重现,当我使用atof,sscanf和strtod而不是sstream。 / p>

在C ++中,将字符串正确转换为double的最佳方法是什么,并返回一个准确的值?



感谢。

解决方案

这几乎与这么多问题完全重复 - 基本上没有34.38在二进制浮点中,所以你的34 + 19/50被表示为34 + k / n,其中n是2的幂,并且不存在具有50作为因子的二的精确幂,因此没有精确值的k可能。



如果设置了输出精度,您可以看到最好的双精度表示法不完全:

  cout<<固定<设定精度(20); 

提供

  char a  -  34.38 
val ----- 34.38000000000000255795
modified val --- 3438.00000000000045474735
FMOD ----- 0.00000000000045474735

所以在回答你的问题,你已经使用最好的方式将字符串转换为双精度(虽然 boost lexical cast 将您的两行或三行合并为一行,因此可能会节省您编写自己的函数) 。结果是由于双精度使用的表示,并且将应用于基于二进制浮点的任何有限表示。



对于浮点数,乘法恰好被向下舍入,比上来,所以你碰巧得到一个确切的结果。这不是你可以依赖的行为。


I am having a problem with precision of a double after performing some operations on a converted string to double.

#include <iostream>   
#include <sstream>
#include <math.h>

using namespace std;

// conversion function
void convert(const char * a, const int i, double &out)
{

   double val;

   istringstream in(a);
   in >> val;

   cout << "char a -- " << a << endl;
   cout << "val ----- " << val << endl;

   val *= i;

   cout << "modified val --- " << val << endl;
   cout << "FMOD ----- " << fmod(val, 1) << endl;

   out = val;

   return 0;

}

This isn't the case for all numbers entered as a string, so the error isn't constant. It only affects some numbers (34.38 seems to be constant).

At the minute, it returns this when i pass in a = 34.38 and i=100:

char a -- 34.38
Val ----- 34.38
modified val --- 3438
FMOD ----- 4.54747e-13

This will work if I change the Val to a float, as there is lower precision, but I need a double.

This also is repro when i use atof, sscanf and strtod instead of sstream.

In C++, what is the best way to correctly convert a string to a double, and actually return an accurate value?

Thanks.

解决方案

This is almost an exact duplicate of so many questions here - basically there is no exact representation of 34.38 in binary floating point, so your 34 + 19/50 is represented as a 34 + k/n where n is a power of two, and there is no exact power of two which has 50 as a factor, so there is no exact value of k possible.

If you set the output precision, you can see that the best double representation is not exact:

cout << fixed << setprecision ( 20 );

gives

char a -- 34.38
val ----- 34.38000000000000255795
modified val --- 3438.00000000000045474735
FMOD ----- 0.00000000000045474735

So in answer to your question, you are already using the best way to convert a string to a double (though boost lexical cast wraps up your two or three lines into one line, so might save you writing your own function). The result is due to the representation used by doubles, and would apply to any finite representation based on binary floating point.

With floats, the multiplication happens to be rounded down rather than up, so you happen to get an exact result. This is not behaviour you can depend on.

这篇关于C ++精度:字符串到双精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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