我试图用下面的代码循环打印标题中提到的数字:
#include <stdio.h>
int ceiling = 1;
float counter = 0;
int main()
{
while (counter < ceiling)
{
printf("%f\n", counter);
counter = counter + 0.000000001;
}
}但它只给了我7位数的精确度。有什么方法可以得到10位的精度吗?
发布于 2013-09-19 02:54:07
你不可能从float中获得这样的精度。即使使用double,您也可能在计数期间出现舍入错误,这些错误可能会跳过或重复某些数字。在必要情况下,您可以使用小数点9位的double,如下所示:
printf( "%.9f\n", counter );但是考虑使用int代替。这将处理所有你需要的9位数字。你只要用零填充打印出来:
int counter = 0;
int ceiling = 1000000000;
while( counter < ceiling ) {
printf( "0.%09d\n", counter );
counter++;
}当然,上面会打印尾随零。还不清楚你想不想那样。
https://stackoverflow.com/questions/18885488
复制相似问题