可表示的结果()和ceil() [英] Representable result of floor() and ceil()

查看:246
本文介绍了可表示的结果()和ceil()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于浮点类型(float / double / long double)的任意值v,C89是否保证floor(v)和ceil(v)的数学上精确的整数结果是类型的可表示值'v'?



以后的C或C ++标准是否可以保证这一点?

IEEE 754保证这一点?

解决方案

这是由IEEE-754号码的结构保证的。 (要清楚的是:C并不保证IEEE-754,但下面的分析适用于我熟悉的所有其他浮点格式;关键属性是格式中所有足够大的数字都是整数)。 / p>




回想一下,一个普通的IEEE-754号码的形式是±1.xxx ... xxx * 2 ^ n ,其中有效字段的宽度( xxx ... xxx 部分)由数字的类型单精度23个二进制数字,双精度52个二进制数字)。所有这些在指定范围内的指数( n )都可以表示。



假设WLOG v 是正数(如果 v 是负数,我们可以交换 ceil floor 在下面的分析中。)

v k 有效位,写 v out作为二进制定点数;有三种可能:

情况1:所有有效位都是整数。当我们写出 v ,看起来像这样

  xxxxxxxxxxxxxxxxxxxxxxxx000000 0.00000.0 

然后 v 是一个整数,所以 ceil(v)= floor(v)= v ,所以两者都是可以代表的。

2:所有有效位都是小数。当我们写出 v 时,它看起来像

  0.000000 ... 00000xxxxxxxxxxxxxxxxxxxxxxxx 

然后 v 0,1),所以 floor(v)= 0 ,可以表示, ceil(v)是可以是0或1,两者都是可表示的。

案例3: v 包含整数和小数位数:

  xxxxxxxxxxxxxx.xxxxxxxxxx 

然后 floor(v)只是:

  XXXXXXXXXXXXXX。 


$ b $ p
$ b $ p $因为我们已经扔掉了至少一个小数位 floor(v) 最多有 k-1 有效位,与 v 相同的指数如果 v 是一个整数,那么 ceil(v)= floor(v)= v ,所以 ceil(v)是可表示的。否则, ceil(v)= floor(v)+ 1 ,所以也至多有 k-1 显着位,也可以代表。


For an arbitrary value 'v' of a floating point type (float/double/long double), does C89 guarantee that the mathematically exact integer result of floor(v) and ceil(v) is a representable value of the type of 'v'?

Does any of the later C or C++ standards guarantee this?

Does IEEE 754 guarantee this?

解决方案

This is guaranteed by the construction of IEEE-754 numbers. (To be clear: C does not guarantee IEEE-754, but the following analysis holds for all other floating-point formats with which I am familiar as well; the crucial property is that all sufficiently large numbers in the format are integers).


Recall that a normal IEEE-754 number has the form ±1.xxx...xxx * 2^n, where the width of the significand field (the xxx...xxx part) is defined by the type of the number (23 binary digits for single precision, 52 binary digits for double precision). All such numbers with an exponent (n) within the allowed range are representable.

Assume WLOG that v is positive (if v were negative, we could swap ceil and floor in the following analysis).

Let v have k significant bits, and write v out as a binary fixed point number; there are three possibilities:

Case 1: All significand bits are integral. When we write out v, it looks like this

xxxxxxxxxxxxxxxxxxxxxxxx000000...00000.0

then v is an integer, and so ceil(v) = floor(v) = v, and so both are trivially representable.

Case 2: All significand bits are fractional. When we write out v, it looks like

0.000000...00000xxxxxxxxxxxxxxxxxxxxxxxx

then v is in the range [0,1), and so floor(v) = 0, which is representable, and ceil(v) is either zero or one, both of which are representable.

Case 3: v contains both integral and fractional significand bits:

xxxxxxxxxxxxxx.xxxxxxxxxx

then floor(v) is just:

xxxxxxxxxxxxxx.

because we have thrown away at least one fractional bit, floor(v) has at most k-1 significant bits, and the same exponent as v, so it is representable.

If v is an integer, then ceil(v) = floor(v) = v, so ceil(v) is representable. Otherwise, ceil(v) = floor(v) + 1, and so also has at most k-1 significant bits and is also representable.

这篇关于可表示的结果()和ceil()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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