- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我想知道标准中是否有一些东西,比如 #define
或 numeric_limits
中的东西,它会告诉我 base-10 数字的最大数量浮点类型的组成部分。
例如,如果我有一些浮点类型,其最大值是:1234.567。我想要标准中定义的东西,它会告诉我该类型为 4。
我可以选择这样做吗?
template <typename T>
constexpr auto integral_digits10 = static_cast<int>(log10(numeric_limits<T>::max())) + 1;
最佳答案
正如 Nathan Oliver 在评论中指出的那样,C++ 提供了 std::numeric_limits<T>::digits10
.
the number of base-10 digits that can be represented by the type
T
without change, that is, any number with this many decimal digits can be converted to a value of typeT
and back to decimal form, without change due to rounding or overflow. For base-radix
types, it is the value ofdigits
(digits
-1 for floating-point types) multiplied by log10(radix
) and rounded down.
对此的解释是explained by Rick Regan here .总而言之,如果您的二进制浮点格式可以在有效数中存储 b 位,那么您保证能够往返最多 d 个十进制数字,其中 < em>d 是满足以下条件的最大整数
10d < 2b-1
在 IEEE754 binary64 的情况下(如今大多数系统上 C++ 中的标准 double
),则 b = 53,并且 2b-1 = 4,503,599,627,370,496,因此格式只保证能够表示 d = 15 位数字。
然而,这个结果适用于所有 位,而您只询问整数部分。然而,我们可以通过选择 x = 2b+1 轻松找到反例,这是不能用以下格式表示的最小整数:for binary64这是 9,007,199,254,740,993,它也恰好有 16 位数字,因此 will need to be rounded .
关于c++ - float 的整数部分中的 10 进制数字的最大位数是多少,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39776532/
谁能推荐一个好的成员(member)结构? 例如,用户支付了 1 个月的成员(member)费,从 2012 年 2 月 1 日到 2012 年 3 月 1 日结束。 何时何地检查用户是否仍是成员(m
我想在我的 JTextPane 中实现查找机制(如文本编辑器或 word)。我希望它有下一个/上一个选项(向上/向下箭头)并突出显示它找到的所有单词。有没有简单的方法可以做到这一点? 最佳答案 我不是
Android 上是否有任何机制和/或编程实践来执行一次性安装/更新脚本?这似乎是一个非常基本的问题,但谷歌搜索没有帮助。 假设我的应用程序使用了一个数据库,并且需要一个常量值表(例如国家名称、城市名
是否可以将 SimpleMembership 与 ASP.NET WebForms 一起使用? John Galloway 的文章似乎表明 WebForms“在 ASP.NET 成员资格之上实现 OA
我是一名优秀的程序员,十分优秀!