可以表示为浮点数的最大奇数整数 [英] Largest odd integer that can be represented as a float

查看:84
本文介绍了可以表示为浮点数的最大奇数整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道什么最大的奇数整数可以精确地表示为浮点数?以及为什么在这种情况下表示为浮点数的最大偶数整数之间存在差异.

我相信这与2的指数2 ^ n-1有关,但是我对C语言中的数据表示不够熟悉,看不到区别.

解决方案

对于IEEE-754基本的32位二进制浮点,最大可表示的奇数是2 24 −1.

对于IEEE-754基本的64位二进制浮点数,最大可表示的奇数整数是2 53 -1.

这是由于格式具有24位和53位有效数字的事实.(有效位是浮点数的小数部分.)

有效位中的位表示的值根据浮点数的指数进行缩放.为了表示奇数,浮点数的有效位数必须为2 0 .对于24位有效数,如果最低位表示2 0 ,则最高位表示2 23 .当所有位都打开时,将获得最大值,这将使值2 0 + 2 1 + 2 2 +…2 23 ,等于2 24 −1.

通常,最大可表示的奇数整数通常为 scalbnf(1,FLT_MANT_DIG)-1 .也可以将其计算为(2-FLT_EPSILON)/FLT_EPSILON .(这是正常情况,其中 FLT_RADIX 是偶数,而 FLT_MANT_DIG< = FLT_MAX_EXP .请注意,如果 FLT_MANT_DIG == FLT_MAX_EXP ,则后一个表达式,应使用 FLT_EPSILON ,因为前者会溢出.)

为了完整起见,异常情况:

  • 如果 FLT_RADIX 为奇数且 FLT_MANT_DIG< = FLT_MAX_EXP ,则如果 FLT_MANT_DIG ,则最大可表示的奇数整数为 FLT_MAX .>是奇数, FLT_MAX-scalbnf(FLT_EPSILON,FLT_MAX_EXP + 1)否则.
  • 如果 FLT_RADIX 是偶数,并且 FLT_MANT_DIG>FLT_MAX_EXP ,然后:如果 FLT_MAX_EXP>0 ,最大可表示的奇数整数是 floorf(FLT_MAX).否则,将无法表示奇数整数.
  • 如果 FLT_RADIX 为奇数且 FLT_MANT_DIG>FLT_MAX_EXP ,然后:如果 FLT_MAX_EXP>0 ,如果 FLT_MANT_DIG-FLT_MAX_EXP 为奇数或 floorf(FLT_MAX)-1 ,则最大可表示的奇数整数为 floorf(FLT_MAX)除此以外.否则,将无法表示奇数整数.

I was wondering what largest odd integer that can be represented exactly as a float? And why there is a difference between the largest even integer represented as a float in this case.

I believe it would have to do with the base 2 exponents 2^n-1, however I am not familiar enough with data representation in C to see the distinction.

解决方案

For IEEE-754 basic 32-bit binary floating-point, the largest representable odd integer is 224−1.

For IEEE-754 basic 64-bit binary floating-point, the largest representable odd integer is 253−1.

This is due to the fact that the formats have 24-bit and 53-bit significands. (The significand is the fraction part of a floating-point number.)

The values represented by the bits in the significand are scaled according to the exponent of the floating-point number. In order to represent an odd number, the floating-point number must have a bit in the significand that represents 20. With a 24-bit significand, if the lowest bit represents 20, then the highest bit represents 223. The largest value is obtained when all the bits are on, which makes the value 20 + 21 + 22 + … 223, which equals 224−1.

More generally, the largest representable odd integer is normally scalbnf(1, FLT_MANT_DIG) - 1. This can also be computed as (2 - FLT_EPSILON) / FLT_EPSILON. (This assumes a normal case in which FLT_RADIX is even and FLT_MANT_DIG <= FLT_MAX_EXP. Note that if FLT_MANT_DIG == FLT_MAX_EXP, the latter expression, with FLT_EPSILON, should be used, because the former overflows.)

The abnormal cases, just for completeness:

  • If FLT_RADIX is odd and FLT_MANT_DIG <= FLT_MAX_EXP, the largest representable odd integer is FLT_MAX if FLT_MANT_DIG is odd and FLT_MAX - scalbnf(FLT_EPSILON, FLT_MAX_EXP+1) otherwise.
  • If FLT_RADIX is even and FLT_MANT_DIG > FLT_MAX_EXP, then: If FLT_MAX_EXP > 0, the largest representable odd integer is floorf(FLT_MAX). Otherwise, no odd integers are representable.
  • If FLT_RADIX is odd and FLT_MANT_DIG > FLT_MAX_EXP, then: If FLT_MAX_EXP > 0, the largest representable odd integer is floorf(FLT_MAX) if FLT_MANT_DIG - FLT_MAX_EXP is odd or floorf(FLT_MAX)-1 otherwise. Otherwise, no odd integers are representable.

这篇关于可以表示为浮点数的最大奇数整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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