gpt4 book ai didi

Setting minimum number of decimal places for std::ostream precision(设置std::ostream精度的最小小数位数)

转载 作者:bug小助手 更新时间:2023-10-24 19:33:17 29 4
gpt4 key购买 nike



Is there a way to set the "minimum" number of decimal places that a std::ostream will output?

有没有办法设置std::ostream将输出的“最小”小数位数?



For example, say I have two unknown double variables that I want to print (values added here for the sake of illustration):

例如,假设我有两个要打印的未知双精度变量(出于说明目的,在此处添加了值):



double a = 0;
double b = 0.123456789;


I can set my maximum decimal precision so that I output b exactly

我可以设置我的最大小数精度,这样我可以准确地输出b



std::cout << std::setprecision(9) << b << std::endl;
>>> 0.123456789


Is there a way to set a "minimum" precision (a minimum number of decimal places), while retaining the "maximum" precision, so that

有没有办法设置“最小”精度(最小小数位数),同时保留“最大”精度,以便



std::cout << a << std::endl << b << std::endl;


yields

收益率



0.0
0.123456789


not



0
0.123456789


?



Thanks!
Phil

谢谢!菲尔






the short answer to this is "No". The stream has only one precision setting, with no facility to differentiate between maximum and minimum precision. Thanks all for your generous advice!

对此的简短回答是“不”。流只有一个精度设置,没有区分最大和最小精度的工具。感谢大家的慷慨建议!


更多回答

Read the question carefully. Phil wants a MINIMUM of 1 digit of precision after the decimal point, but more if there is non-zero digits.

仔细阅读问题。Phil希望小数点后的精度至少为1位,但如果有非零位数,则精度更高。

@Phil: You might find stackoverflow.com/questions/2475642/… helpful.

@菲尔:你可能会找到Stackoverflow.com/Questions/2475642/…很有帮助。

Thanks Roger. That's very helpful. Your formatter example looks like the right way to go.

谢谢,罗杰。这很有帮助。您的格式化程序示例看起来是正确的方法。

优秀答案推荐

cout << setprecision(1) << fixed << d << endl;


Used fixed after setprecision.

在设置精度后使用FIXED。



Edit: This is what you want. It would change precision based on d.

编辑:这就是你想要的。它会改变基于d的精度。



cout << setprecision(d?9:1) << fixed << d << endl;


I do not think there is a way to achieve what you are asking without turning the number into a string (with high precision), and stripping off the trailing zeros.

我认为,如果不将数字转换为字符串(以高精度)并去掉尾随的零,就不可能实现您所要求的结果。



This is appropriate, because just because there are trailing zeros doesn't mean there isn't precision there, and the run-time can't tell that.

这是恰当的,因为后面有零并不意味着没有精度,运行时不能说明这一点。



For example, if I measure the weight of an object with a cheap scale, it may be 1.0 kgs.

例如,如果我用便宜的秤测量一个物体的重量,它可能是1.0公斤。



If I weigh it with a high-accuracy scale, it may be 1.00000 kgs. Just because there are trailing zeros, doesn't mean that accuracy should be discarded.

如果我用高精度的秤称重,可能是1.00000公斤。仅仅因为有尾随的零,并不意味着应该丢弃准确性。


更多回答

Thanks Sameer. However that assumes that I know beforehand that d=0. Is there a solution for an arbitrary d that would print "0.0" if d=0, but "0.123456789" if d=0.123456789?

谢谢你,萨米尔。但这是假设我事先知道d=0。对于任意的d,如果d=0,则打印“0.123456789”,如果d=0.123456789,则打印“0.123456789”,是否有解决方案?

@Phil: The stream only has one precision setting. You could check the value before you output it, and output your "0.0" then (note that is a string literal, not a double).

@Phil:流只有一个精度设置。您可以在输出之前检查该值,然后输出您的“0.0”(请注意,这是一个字符串文字,而不是双精度)。

You can do something like this. cout << setprecision(d?9:1) << fixed << d << endl;

你可以做这样的事情。Cout<<设置精度(d?9:1)<<固定<

Nice shortcut. Might be a bit cumbersome in implementation as I'd need to have a setprecision call in every output stream.

很好的捷径。在实现中可能有点麻烦,因为我需要在每个输出流中都有一个设置精度调用。

@Phil: if (d) cout << d; else cout << "0.0"; is better (and you'd keep the stream with the desired fixed/precision settings, you don't have to repeat those unless they changed since the last output). Also realize you might want something like if (fabs(d) > some_tolerance) because floating point math is inexact.

@Phil:如果(D)cout<Some_Terance)的内容,因为浮点数学是不精确的。

+1, though it's still unclear to me what he wants exactly, beyond zero being exactly "0.0".

+1,尽管我仍然不清楚他到底想要什么,超过零正好是“0.0”。

Nothing more than that, Roger. I have a requirement that floats or double are represented in the output stream with a decimal place and I'm just looking for a way to do this elegantly without implying the kind of accuracy precision that Oddthinking refers to above. Hence 0 should be represented as 0.0, not 0.0000...

仅此而已,罗杰。我有一个要求,浮点数或双精度数在输出流中用小数位表示,我只是在寻找一种方法来优雅地实现这一点,而不暗示OddThink上面提到的那种精确度。因此,0应表示为0.0,而不是0.0000...

@Phil: What about 0.12, is output as "0.120000" okay? Why isn't "0.000000" fine? (Exactly what requirement does it fail?)

@Phil:如果是0.12,输出为“0.120000”可以吗?为什么“0.000000”不行?(它到底不能满足什么要求?)

If I had the option of setting a minimum precision then I could just set a maximum precision (setprecision(9)) and have the best of both worlds. 0.12 output as "0.12" while 0.123456789 is output as "0.123456789". As to why 0.000000 isn't fine? Unfortunately that's a convoluted story involving legacy implementations and historic data.

如果我可以选择设置一个最小精度,那么我只需要设置一个最大精度(设置精度(9)),就可以两全其美了。0.12输出为“0.12”,而0.123456789输出为“0.123456789”。为什么0.000000不好呢?不幸的是,这是一个涉及遗留实现和历史数据的复杂故事。

@Phil: If you merely need 0.0 treated specially, then do only that, but make sure you know why using fabs (as I showed in another comment) is useful. If you need to strip all trailing zeros, the question I linked (in the comment on the question above) will work. (And maybe you could edit the question with the clearer requirements and someone can add/edit an answer to match them.)

@Phil:如果你只需要特别处理0.0,那么就只做这件事,但要确保你知道为什么使用FABS(正如我在另一条评论中所展示的)是有用的。如果您需要去掉所有尾随的零,我链接的问题(在上面问题的评论中)将起作用。(也许您可以编辑具有更明确要求的问题,然后有人可以添加/编辑答案以匹配这些要求。)

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com