回答
std::numeric_limits<float>::digits10
从http://en.cppreference.com/w/cpp/types/numeric_limits/digits10
标准32位IEEE 754浮点类型都有一个24位小数部分(23个比特写入,一个隐含的),这可能表明,它可以代表7位小数点(24 * std :: log10(2)为7.22),但相对舍入误差不均匀,一些具有7个小数位的浮点值不能转换为32位浮点数并返回:最小的正数示例为8.589973e9,往返后变为8.589974e9。这些舍入误差不能超过表示中的一位,并且数字10计算为(24-1)* std :: log10(2),即6.92。四舍五入的结果值6
EDIT2: 这表明数量为任何浮动不是7,但只有6位数,就像std::numeric_limits<float>::digits10
电话中获知。
float orgF = 8.589973e9;
int i = orgF;
float f = i;
assert(f == orgF);
这将失败,因为往返改变值。
所以,如果我们只是在1.0和0.0之间寻找数字,那么肯定的答案是7,因为具有问题的最低正数是8.589973e9。
@我听不懂这句话,你能解释一下吗? “但是相对的舍入误差是不均匀的,一些具有7个十进制数字的浮点值在转换为32位浮点数和返回值后仍然存在。”我认为这是指先前的陈述:“任何具有这么多十进制数字的数字都可以被转换为T类型的值并返回十进制形式,而不会因四舍五入或溢出而改变。” – 2014-09-12 10:54:58
往返'float orgF = 8.589973e9; int i = orgF; float f = i; assert(f == orgF);'可能出错 – Surt 2014-09-12 10:59:23
您添加的示例不直接映射到OP的问题。 – 2014-09-13 12:48:11
如果我理解正确你的问题,the answer是6
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
using namespace std;
int main() {
int i = 10; /* Number of distinct number after 1 place of decimal */
int k = 1; /* Number of decimal places */
for(;/* ever */;)
{
float prev = 0.0f;
for(int j = 1; j <= i; ++j)
{
stringstream ss;
ss << "0."; /* Prepare stream with 0. followed by k digit number with leading zeroes */
ss << setfill('0') << setw(k) << j;
float next; /* Read the number */
ss >> next;
if(prev == next) return 0; /* If previous number and current number can not be distinguished */
prev = next;
}
cout << "Works for " << k << " places" << endl;
i *= 10; /* 10 times more tests to be conducted for 1 more decimal places */
k++; /* Try for more decimal places */
}
return 0;
}
什么代码做
1. Set precision to 1 place after decimal 2. Compare 0.0 with 0.1, 0.1 with 0.2 .... 0.8 with 0.9 and 0.9 with 1.0, if any of these are equal (not distinguish), exit. Otherwise print Works for 1 place. 3. Set precision to 2 places after decimal 4. Compare 0.00 with 0.01, 0.01 with 0.02 .... 0.98 with 0.99 and 0.99 with 1.00, if any of these are equal (not distinguish), exit. Otherwise print Works for 2 places. 5. Repeat similar steps for 3 and more digits unless you exit
+1这是一个非常实用的方式来回答这个问题。我希望我能接受这个答案,但看起来@Surt的答案更多一些,正如你所说的“写得好”。 – 2014-09-12 10:59:26
@JonathanMee谢谢 – 2014-09-12 12:29:57
- 1. Python的float精度约小数
- 2. 调整小数精度,.NET
- 3. 固定小数精度
- 4. tensorflow FLOAT32小数精度
- 5. C#的DataTable小数精度
- 6. C#小数精度功能
- 7. SQLAlchemy的小数精度
- 8. Python设置小数精度
- 9. switch语句;小数精度
- 10. 小数除法精度
- 11. python qt float boost boost-python submodule的精度
- 12. c#float []平均丢失精度
- 13. 在Perl中获取float的精度?
- 14. 以编程方式指定字符串格式化float的小数精度
- 15. Python的小数精度与C的小数精度相比如何?
- 16. 星火数据框中小数精度
- 17. Z3精度为实数和小数值
- 18. 在hdf5数据集中设置float精度
- 19. Python“float”和PostgreSQL“双精度”的浮点数
- 20. 如何确定双精度小数?
- 21. 分解双精度小数部分
- 22. 带小数点后精度的struct.unpack
- 23. 以逗号掩盖小数点精度
- 24. 获取双精度值的小数位
- 25. Python:允许精度很小的数字?
- 26. Lua的小数精度损失
- 27. MySQL小数列精度与Servicestack ORMLite
- 28. 高精度GWT的NumberFormat问题小数
- 29. Java - MySQL |双精度和小数点
- 30. MySQL的精度只有小数
你的第一句话表明你正在寻找独特的表现,而你第二个建议你正在寻找一个精确的表示。这是什么?我不能完全表示0.1,但我可以唯一地表示它。 – 2014-09-11 11:25:51
阅读http://floating-point-gui.de/ – 2014-09-11 11:54:24
@JosephMansfield 0.0到1.0之间有无限多的实数。可以将每个精度为2的数字转换为“int”并返回;这样原来的“浮动”被保留下来了?所以在示例问题中,我是否可以表示:“0.00F”,“0.01F”,“0.02F”,“0.03F”,“0.04F”,“0.05F”,“0.06F”,“0.07F” 0.08F“,”0.09F“,”0.10F“,”0.11F“,”0.12F“和”0.14F“,我不能唯一地表示”0.13F“。这意味着如果我这样做了:'float foo = 0.13F;''foo'将具有与'0.12F'或'0.14F'相同的表示。 – 2015-08-24 11:13:07