以编程方式确定符号整数类型的最大值 [英] Programmatically determining max value of a signed integer type

查看:169
本文介绍了以编程方式确定符号整数类型的最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此相关的问题是如何确定在编译时有符号类型的最大值:

This related question is about determining the max value of a signed type at compile-time:

<一个href=\"http://stackoverflow.com/questions/4514572/c-question-off-t-and-other-signed-integer-types-minimum-and-maximum-values\">C问题:off_t(和其它符号整型)最小和最大值

不过,我既然意识到,确定符号类型的最大值(如 time_t的 off_t )在运行似乎是一个非常艰巨的任务。

However, I've since realized that determining the max value of a signed type (e.g. time_t or off_t) at runtime seems to be a very difficult task.

最接近的解决方案,我能想到的是:

The closest thing to a solution I can think of is:

uintmax_t x = (uintmax_t)1<<CHAR_BIT*sizeof(type)-2;
while ((type)x<=0) x>>=1;

本可以避免,只要任何循环为键入没有填充位,但如果键入确实有填充比特,剧组调用实现定义的行为,这可能是一个信号或无意义的实现定义的转换(如剥离符号位)。

This avoids any looping as long as type has no padding bits, but if type does have padding bits, the cast invokes implementation-defined behavior, which could be a signal or a nonsensical implementation-defined conversion (e.g. stripping the sign bit).

我开始觉得问题无法解决,这是一个有点不安,并会在C标准的缺陷,在我看来。为了证明我是错的任何想法?

I'm beginning to think the problem is unsolvable, which is a bit unsettling and would be a defect in the C standard, in my opinion. Any ideas for proving me wrong?

推荐答案

更新:值得庆幸的是,我的previous下面是错误的,似乎有对这个问题的解决方案的答案

Update: Thankfully, my previous answer below was wrong, and there seems to be a solution to this question.

uintmax_t x;
for (x=-1; (T)x!=x; x/=2);

该计划无论是收益率 X 包含类型的最大可能值 T ,或者产生一个实现定义的信号

This program either yields x containing the max possible value of type T, or generates an implementation-defined signal.

周围的信号的情况下工作是可能的,但困难和计算上不可行(如必须安装每一个可能的信号数字信号处理器),所以我不认为这个答案完全令人满意。 POSIX信号语义可能给予足够的附加属性,使之可行的;我不知道。

Working around the signal case may be possible but difficult and computationally infeasible (as in having to install a signal handler for every possible signal number), so I don't think this answer is fully satisfactory. POSIX signal semantics may give enough additional properties to make it feasible; I'm not sure.

有趣的地方,特别是如果你舒服假设你是不是会产生信号的实现,是发生了什么的时候(T)X 结果实现定义的转换。上述循环的诀窍在于,它不依赖于对所有实施的首选转换价值。它所依靠的是(T)x == X 可能是当且仅当 X 适合类型 T ,因为否则的值 X 的前pression的可能值范围之外的类型 T

The interesting part, especially if you're comfortable assuming you're not on an implementation that will generate a signal, is what happens when (T)x results in an implementation-defined conversion. The trick of the above loop is that it does not rely at all on the implementation's choice of value for the conversion. All it relies upon is that (T)x==x is possible if and only if x fits in type T, since otherwise the value of x is outside the range of possible values of any expression of type T.

旧观念,错误的,因为它没有考虑上述(T)x == X 属性:

Old idea, wrong because it does not account for the above (T)x==x property:

我觉得我有证明的草图就是我正在寻找的是不可能的:

I think I have a sketch of a proof that what I'm looking for is impossible:


  1. 设X是一个符合标准的C语言实现,并假设 INT_MAX方式&gt; 32767

  2. 定义一个新的C实现Ÿ相同的X,但其中 INT_MAX INT_MIN 的值分别分割2。

  3. 证明Y是一个符合标准的C语言实现。

  1. Let X be a conforming C implementation and assume INT_MAX>32767.
  2. Define a new C implementation Y identical to X, but where the values of INT_MAX and INT_MIN are each divided by 2.
  3. Prove that Y is a conforming C implementation.

本纲要的基本思路是,由于这样的事实有关的一切超出限制的值与符号类型是实现定义的或不确定的行为,高的值位的任意号码可以被视为的填充位的无除了在 limits.h中实际进行到执行任何更改。

The essential idea of this outline is that, due to the fact that everything related to out-of-bound values with signed types is implementation-defined or undefined behavior, an arbitrary number of the high value bits of a signed integer type can be considered as padding bits without actually making any changes to the implementation except the limit macros in limits.h.

上,如果这听起来正确的或假的有什么想法?如果这是正确的,我很乐意授予赏金谁就可以做使它更加严格的最好的工作。

Any thoughts on if this sounds correct or bogus? If it's correct, I'd be happy to award the bounty to whoever can do the best job of making it more rigorous.

这篇关于以编程方式确定符号整数类型的最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆