English 中文(简体)
1. 与浮动点数合并
原标题:Confusion with floating point numbers
int main()
{
  float x=3.4e2;
  printf("%f",x);
  return 0;
}

产出:

340.000000  // It s ok.

但是,如果写了<条码>x=3.1234e2。 产出如下:312.339996;x=3.12345678e2。 页: 1

为什么这些产出一样? 我认为,如果我写上<条码>x=3.1234e2,产出应为<条码>312.340000,但实际产出为<条码>312.3399<96>,使用海合会汇编者。

最佳回答

并非所有分数都有精确的双倍当量,因此四舍五入到最接近值。

简化实例

如果你有3个分数点,你可以:

0
0.125
0.25
0.375
...

0.5%有确切的代表性,但0.1将显示为0.125。

当然,实际差异要小得多。

问题回答

平均点数通常以双倍数表示,以提高效率。 这与基数-10代表情况一样准确,但有些小数不能准确作为双亲分数。 相反,它们是近似的。

此外,<代码>float通常为32个比值,这意味着它没有所有这么多的重要数字。 你可以在其例子中看到,他们准确到大约8位重要数字。

然而,你正在印刷这些编号,使之略高于其重要性,因此,你看到了这一差异。 看看f格式,说明如何打印数字。

也许需要准确反映数字;这往往发生在金融申请中。 在这种情况下,你需要利用专门的图书馆来代表数字,或简单地计算所有数字作为分类(例如,代表数额是百分数而不是美元和美元分数)。

http://docs.sun.com/source/806-3568/ncg_goldberg.html” rel=“nofollow” 每个计算机科学家都应了解Floating-Point Arithmetic





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签