简体   繁体   English

为什么 cout 删除浮点数的尾随零

[英]why does cout remove the trailing zeroes for floating point numbers

So i have this code所以我有这个代码

template <typename T, typename T2>
void print (const T& a, const T2& b)
{
cout << a<<endl<<b<<endl ;
}

int main(){
float i=1.002;
float j=1.000;
print(i,j);

return 0;
}

the output is输出是

 1.002
 1

what i dont get is why does cout remove the zeroes although i have specified them during initailisation , i know it wont make any difference but im just curious why does it happen.我不明白的是为什么 cout 删除零,尽管我在初始化过程中指定了它们,我知道它不会有任何区别,但我只是好奇它为什么会发生。

  • Because it's by design...?因为它是设计使...?
  • Usually, we don't like useless information in this world.通常,我们不喜欢这个世界上无用的信息。
  • And because 1.000 == 1 .因为1.000 == 1

Because of the way the number is stored.因为号码的存储方式。

TheIEEE floating point representation does not have a notion of significant digits. IEEE浮点表示没有有效数字的概念。 The numbers 1.0 or 1.000 all look the same in the binary notation.数字 1.0 或 1.000 在二进制表示法中看起来都一样。

You have to specify a precision when printing.您必须在打印时指定精度。

Python for instance does a type Decimal that behaves like you want.例如,Python 会执行一个Decimal类型,它的行为与您想要的一样。

Expecting your code to print those trailing zeros is no different than expecting it to print 5.0 - 4.0 just because you initialized the variable as float j = 5.0 - 4.0;期望您的代码打印那些尾随零与期望它打印5.0 - 4.0没有什么不同,因为您将变量初始化为float j = 5.0 - 4.0; . .

From the floating-point representation perspective, there's no such thing as "I specified trailing zeros during initialization".从浮点表示的角度来看,没有“我在初始化期间指定了尾随零”这样的事情。 Floating-point numbers are not strings.浮点数不是字符串。 They are numbers.他们是数字。 And there are infinite different ways to express the same number as a sequence of human-readable characters in your program's text.并且有无数种不同的方式可以将相同的数字表示为程序文本中一系列人类可读的字符。 In the source code of the program 1.000 is the same as 1.00 and is the same as 0.1e1 - they all represent the same number.在程序的源代码中, 1.0001.000.1e1相同 - 它们都代表相同的数字。

This means that when the time comes to convert a floating-point number back to the textual representation for cout , the library will not know (and will not care) what kind of text you used originally.这意味着当需要将浮点数转换回cout的文本表示时,库将不知道(也不会关心)您最初使用的文本类型。 You can affect the textual output by changing the precision, width and other format details (like use scientific format etc.), but there's no such thing as "output it the same way I wrote it in the source code".您可以通过更改精度、宽度和其他格式详细信息(如使用科学格式等)来影响文本输出,但没有“按照我在源代码中编写的方式输出”这样的东西。

Why would it print 1.000 instead of 1.0000000000000000 ?为什么会打印1.000而不是1.0000000000000000 Both of those numbers are the same exact bits in memory.这两个数字在内存中是完全相同的位。 (In other words, a float value is stored as 32-bits in a very specific format. No matter if it is 1.0 or -59.12391 it is 32-bits of data in memory somewhere.) (换句话说,浮点值以非常特定的格式存储为 32 位。无论是 1.0 还是 -59.12391,它都是内存中某处的 32 位数据。)

If you want it to be printed in a certain way, use formatting specifiers such as setw() (don't forget to #include<iomanip> ) to format your cout output a certain way.如果您希望以某种方式打印它,请使用诸如setw() (不要忘记#include<iomanip> )之类的格式说明符以某种方式格式化您的cout输出。

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

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