科学记数法的类型

发布于 2024-12-18 12:19:37 字数 140 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

虫児飞 2024-12-25 12:19:37

1e9 是一个 double,在 IEEE 浮点表示中具有精确的表示形式。 C++ 标准不强制要求 IEEE 浮点。

s 是一个 int,因此 double 值将自动转换为 int。这如何发生在某种程度上取决于实施。在当今的大多数机器上,这种转换意味着 s 将被赋予初始值 1000000000。

1e9 is a double that has an exact representation in the IEEE floating point representation. The C++ standard doesn't mandate IEEE floating point.

s is an int, so the double value will be automatically converted to an int. How this takes place is to some extent up to the implementation. On most machines nowadays, this conversion means s will be given an initial value of 1000000000.

囚我心虐我身 2024-12-25 12:19:37

不管怎样,你可以编写代码来显示 1e9 的类型为 double

#include <iostream>

void show_type(double value) { 
    std::cout << "type = double\n"; 
}
void show_type(...) {
    std::cout << "Type != double\n";
}

int main() { 
    show_type(1e9);
    return 0;
}

当然,如果你不知道它的类型,那就有点多了努力为每种可能的类型提供重载,但原理是相同的。

For what it's worth, you can write code that shows that 1e9 has type double:

#include <iostream>

void show_type(double value) { 
    std::cout << "type = double\n"; 
}
void show_type(...) {
    std::cout << "Type != double\n";
}

int main() { 
    show_type(1e9);
    return 0;
}

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.

ぇ气 2024-12-25 12:19:37

如果将其更改为 floatdouble,它将非常精确,但并非所有使用它的计算都会精确(感谢 Kerrek SB ),精度有限制。

请注意,精度不是符号的属性,无论如何,符号本身就是精确性。

If you change it to float or double 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文