English 中文(简体)
只从未签名的借方对浮标进行检查
原标题:Bounds checking a float from only an unsigned bit string

我正试图把单一精度浮标变成仅仅从环绕中推出来的 in。 我正确地从环形阵列中 gr住了标线、广度和红issa,但我不敢确保浮体在距离内(0x80000<float<0x7fff)仅从其轨道上.。

此外,当我把结果四舍五入到零时,我感到困惑不解。 看来, cast上一只一只一只,我不知道何时或为何发生这种情况?

我的法典是:

int convertFloatToAnInt(unsigned f) {
//trying to return (inf)f
  int signBit = (f >> 31) & 1;
  int exponent = (f >> 23) & 0xFF;
  int mantissa = f & 0x7FFFFF;
  exponent = exponent - 127;
 
  int truncatedFloat = 0;
  //trucate based on the exponent with decimal at 23rd bit
  truncatedFloat = (mantissa>>(23-exponent));
    
  //add the implicit 1 back to the mantissa
  truncatedFloat |= (1<<exponent);

  //change the sign if needed
  if (signBit) {
    truncatedFloat = ~(truncatedFloat)+1;
  }

  return truncatedFloat;//(int)f
}
问题回答
uint32_t u = ...;

float f = ( ( union { uint32_t u; float f; } ){ .u = u } ).f;

I will assume the IEEE 754 single-precision representation of float and 32-bit width for int and unsigned as indicated in the question.

<代码>int 范围[-2^31;2^31-1]。

<代码>float上的标识基本上为24倍比值,乘以2^-23(即23个位置改为正确)。 如果在最多30个地点向左边转移,则这在31个轨道之内。 因此,如果票价最多为30,则该数值将并入<条码>int。 然而,有一个情况是,票价为31,也符合:如果价值为-2^31,即标值为31,票价为31,而红利为0。

在进行转换时,必须在进行借方转换之前进行这些检查,以避免。 此外,必须检查的是,如果要转移红土,或者由于转移负值的行为不明确,就应当转移红土。

因此,可以实施:

int toInt(unsigned f)
{
  int signBit = (f >> 31) & 1;
  int exponent = (f >> 23) & 0xFF;
  int mantissa = f & 0x7FFFFF;
  exponent = exponent - 127;

  int result = 0;
  if(exponent >= 0)
  {
    if(exponent <= 30)
    { // The result is within range
      int mantissa1 = mantissa | (1u << 23);
      if(exponent <= 23)   // Need to shift right
        result = mantissa1 >> (23-exponent);
      else                 // Need to shift left
        result = mantissa1 << (exponent-23);  
      if(signBit) result = -result;
    }
    else
    { // Probably out of range, but check for -2^31
      if(exponent == 31 && signBit && mantissa == 0)
        result = -(1u<<31);
      else
        printf("Error: out of range");
    }
  }
  // else the absolute value is less than 1, so the result is 0
  return result;
}

Note: calling this function requires passing the bit pattern of a float as an unsigned. This can be done as follows:

#include <math.h>
#include <string.h>

int main()
{
    float x = -pow(2.0, 31);
    unsigned xBits;
    memcpy(&xBits, &x, 4);
    printf("%d, %d", (int)x, toInt(xBits));

    return 0;
}

必须指出,<代码>(int)x>将未界定的行为,条件是x不适用于<<>>。 <toInt(>>,对于x的任何价值,不得有任何未界定的行为。 然而,当然,toInt(>在很大程度上取决于实施界定的行为,例如float的表述。

我不敢肯定,如何确保浮标范围(0x80000<float<0x7fffff)完全靠其断层。

您可使用<条码>0 <= f && f < 0x4F000000u >0x80000000<=f & f <=0xCF000u。

The encodings of IEEE-754 binary32 floating-point numbers are in ascending order for non-negative numbers and descending order for negative numbers. I assume you are using binary32 for float and you have the bit strings in the proper order.

+0和+231的轨道型号为000<00016和4F000。 afloat 载于[+0, +231]。 发放轨道体为[0000016,4F00016

The patterns for −0 and −231 are 8000000016 and CF00000016. So a float is in [−231, −0] iff its bit string is in [8000000016, CF00000016].

看来, cast上一只一只一只,我不知道何时或为何发生这种情况?

C2018 6.3.1.41具体规定了将浮动点类型转换为星体类型,并称其数值为零。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签