简体   繁体   English

C ++要精确浮动的字符串

[英]C++ String to float with precision

I have a file with number readings (example 5.513208E-05 / 1.146383E-05) I read the file and store the entries in a temporary string. 我有一个带有数字读数的文件(例如5.513208E-05 / 1.146383E-05)我读取文件并将条目存储在一个临时字符串中。 After that I convert the temporary string into float variable (which I store in an Multi Dimensional Array). 之后我将临时字符串转换为float变量(我存储在多维数组中)。 I use the code below to convert. 我使用下面的代码进行转换。

getline(infile, temporary_string, ',');

Array[i][0] = ::atof(temporary_string.c_str());

getline(infile, temporary_string);

Array[i][1] = ::atof(temporary_string.c_str());

The problem is that when I print the floats to the screen 问题是当我将花车打印到屏幕上时

5.51321e-05 1.14638e-05 instead of 5.513208E-05 1.146383E-05 5.51321e-05 1.14638e-05代替5.513208E-05 1.146383E-05

How can I get the precise numbers stored ??? 如何获得存储的精确数字???

You don't specify precision when you read or convert the string. 读取或转换字符串时,不指定精度。 Instead you set the precision when you output the value: 而是在输出值时设置精度:

std::cout << std::setprecision(3) << 1.2345 << '\n';

The above will produce the following oputput: 以上将产生以下oputput:

1.23

See eg this reference . 参见例如此参考文献

Ensure you have double Array[][] , not float . 确保你有double Array[][] ,而不是float A text presentation (base 10) is always approximated by the binary floating point number (base 2), but with luck approximated number of atof has the same presentation, when using the same format. 文本表示(基数10)总是由二进制浮点数(基数2)近似,但是当使用相同的格式时,运气近似的atof数具有相同的表示。 In general one does not do much calculation, and on output uses a reduced precision with setprecision or formats. 通常,一个人不会进行太多计算,并且在输出中使用精确度或格式的精度降低。

Every floating-point representation of numbers has limited precision. 数字的每个浮点表示都具有有限的精度。 In particular, float has 24 bits (1 fixed+23 variable) for its binary mantissa, thus implying a precision of roughly seven decimal digits. 特别是, float的二进制尾数有24位(1个固定+ 23个变量),因此意味着大约七位小数的精度。

If you need more precision for the stored number, you may wish to consider using double instead of float . 如果存储的数字需要更高的精度,您可能希望考虑使用double而不是float On normal PCs, double has 53 bits (1+52) for the binary mantissa, thus allowing a 15-decimal digit precision. 在普通PC上, double对于二进制尾数有53位(1 + 52),因此允许15位十进制数字精度。

But remember that there's also a problem when those numbers are output. 但请记住,输出这些数字时也存在问题。 I think the default precision for both printf () and std::ostream is only 6 digits, both for float and for double , unless you specify otherwise. 我认为printf ()和std::ostream的默认精度只有6位数,包括floatdouble ,除非另有说明。 There is no point, however, in demanding a higher precision during output than what the data type provides. 但是,在输出期间要求比数据类型提供的更高的精度没有意义。 So, even though you can say printf("%0.30g", some_float) , the extra 23 digits beyond the seven actually supported by the data type might not really produce useful information. 因此,即使您可以printf("%0.30g", some_float) ,超出数据类型实际支持的七个数字的额外23位数可能不会真正产生有用的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM