【问题标题】:IEEE754 single precision - General algorithm for representing the half of a numberIEEE754 单精度 - 表示数字一半的通用算法
【发布时间】:2018-10-12 21:38:30
【问题描述】:

假设 N 是根据 IEEE754 单精度标准表示的任意数。我想在 IEEE754 中再次找到 N/2 的最精确表示。

我想找到一个通用算法(用文字描述,我只想考虑必要的步骤和案例)来获得表示。

我的做法是:

说数字代表:b0_b1_b2_b3...b_34

  1. 隔离确定数字符号 (-/+) 的第一位。
  2. 根据无符号表示 b_1...b_11 计算幂 (p) 的表示。
  3. 如果power = 128 我们有一个特殊情况。如果尾数的所有位都等于 0,则根据b_0,我们有负无穷大或正无穷大。我们不会改变任何东西。如果尾数至少有一位等于 1,那么我们就有 NaN 值。同样,我们什么也没做。
  4. 如果e is inside]-126, 127[then we have a normalized mantissam.新的幂p can be calculated asp' = p - 1and belongs in the interval]-127, 126]. We then calculatem/2` 我们从右边开始表示它并丢失任何不能包含在 23 位中的位尾数。
  5. 如果e = -126,那么在计算这个数字的一​​半时,我们传入一个非规范化尾数。我们代表p = 127,计算尾数的一半并从右边开始重新代表它,丢失任何不能包含的信息。
  6. 最后,如果e = -127 我们有一个非规范化尾数。只要m/2 可以用尾数中可用的位数表示而不会丢失信息,我们就表示并保留p = -127。在任何其他情况下,我们将数字表示为正数或负数 0,具体取决于 b_0

我遗漏的任何步骤、可以进行的任何改进(我相信有)或任何看起来完全错误的地方?

【问题讨论】:

  • 没有“简单精度”。有精度和精度。使用 64 位,您说的是 double 精度。
  • Power = 128 not 是一种特殊情况(对于单 双精度)。 This wikipedia article 应该会有所帮助,假设您使用的是双精度。
  • 好的,我正在学习另一种语言,而不是英语,其中单精度是所用术语的直译,简单精度。至于你说得对的 64 位,我犯了一个错误,整篇文章都是关于单精度的,因此是 34 位。为什么 p = 128 不是一个特殊情况,不对应于 (+/-) infty 或 NaN?@user3386109
  • Here's the page for single precision,解释了指数的特殊情况。
  • p = 127...你是对的。我认为其他一切似乎都很好

标签: algorithm binary numbers ieee-754


【解决方案1】:

我在 Java 中实现了除以二的算法,并对所有 32 位输入进行了验证。我试图遵循你的伪代码,但我在三个地方出现了分歧。首先,无穷大/NaN 指数是 128。其次,在情况 4(正常 -> 正常)中,无需对分数进行运算。第三,当您对分数进行运算时,您没有描述半数甚至是如何工作的。否则为 LGTM。

public final class FloatDivision {
  public static float divideFloatByTwo(float value) {
    int bits = Float.floatToIntBits(value);
    int sign = bits >>> 31;
    int biased_exponent = (bits >>> 23) & 0xff;
    int exponent = biased_exponent - 127;
    int fraction = bits & 0x7fffff;
    if (exponent == 128) {
      // value is NaN or infinity
    } else if (exponent == -126) {
      // value is normal, but result is subnormal
      biased_exponent = 0;
      fraction = divideNonNegativeIntByTwo(0x800000 | fraction);
    } else if (exponent == -127) {
      // value is subnormal or zero
      fraction = divideNonNegativeIntByTwo(fraction);
    } else {
      // value and result are normal
      biased_exponent--;
    }
    return Float.intBitsToFloat((sign << 31) | (biased_exponent << 23) | fraction);
  }

  private static int divideNonNegativeIntByTwo(int value) {
    // round half to even
    return (value >>> 1) + ((value >>> 1) & value & 1);
  }

  public static void main(String[] args) {
    int bits = Integer.MIN_VALUE;
    do {
      if (bits % 0x800000 == 0) {
        System.out.println(bits);
      }
      float value = Float.intBitsToFloat(bits);
      if (Float.floatToIntBits(divideFloatByTwo(value)) != Float.floatToIntBits(value / 2)) {
        System.err.println(bits);
        break;
      }
    } while (++bits != Integer.MIN_VALUE);
  }
}

【讨论】:

    猜你喜欢
    • 2020-04-06
    • 2017-01-10
    • 2011-11-17
    • 2013-01-03
    • 2022-09-24
    • 1970-01-01
    • 2019-05-15
    • 2013-04-16
    • 2011-07-22
    相关资源
    最近更新 更多