科学记数法的类型
int s = 1e9;
1e9
的类型是什么?它的精确度如何? (它是否完全等于 1000000000?)
如果可能的话,将值/变量的类型打印到 stdout 会很有用。
int s = 1e9;
What is type of 1e9
and how precise is it? (Is it exactly equal to 1000000000?)
Printing type of a value/variable to stdout
would be useful if it's possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
1e9
是一个double
,在 IEEE 浮点表示中具有精确的表示形式。 C++ 标准不强制要求 IEEE 浮点。s
是一个int
,因此double
值将自动转换为int
。这如何发生在某种程度上取决于实施。在当今的大多数机器上,这种转换意味着s
将被赋予初始值 1000000000。1e9
is adouble
that has an exact representation in the IEEE floating point representation. The C++ standard doesn't mandate IEEE floating point.s
is anint
, so thedouble
value will be automatically converted to anint
. How this takes place is to some extent up to the implementation. On most machines nowadays, this conversion meanss
will be given an initial value of 1000000000.不管怎样,你可以编写代码来显示
1e9
的类型为double
:当然,如果你不知道它的类型,那就有点多了努力为每种可能的类型提供重载,但原理是相同的。
For what it's worth, you can write code that shows that
1e9
has typedouble
:Of course, if you don't know what type it has, it's quite a bit more work to provide an overload for every possible type, but the principle is the same nonetheless.
如果将其更改为
float
或double
,它将非常精确,但并非所有使用它的计算都会精确(感谢 Kerrek SB ),精度有限制。请注意,精度不是符号的属性,无论如何,符号本身就是精确性。
If you change it to
float
ordouble
it's going to be pretty precise, but not every computation with it will be exact (thnks to Kerrek SB), there are limits to precision.Note, that the precision is not the property of notation, anyway, the notation is the exactness itself.