简体   繁体   English

定点算术中的单精度

[英]Single precision in Fixed Point Arithmetic

I need up to 6 decimal places precision for a Taylor series calculation using fixed point arithmetic. 使用定点算法进行泰勒级数计算时,我需要达到6位小数位精度。 I have tried different fixed point format for achieving 6 decimal places precision. 我尝试了不同的定点格式以实现6个小数位精度。

For example, Using s16.15 (Left shift by 15) format I have got up to 2 decimal places precision.1 sign bit,16 integer bits and 15 fraction bits. 例如,使用s16.15(左移15)格式,我得到了2个小数位精度。1个符号位,16个整数位和15个小数位。

For s8.23 (Left shift by 23) format up to 4 decimal places and with s4.27 (Left shift by 27) format the precision is still the same. 对于s8.23(左移23)格式,最多可保留小数点后4位;对于s4.27(左移27)格式,精度仍然相同。 I was expecting the situation will improve. 我原以为情况会有所改善。

The following is a Taylor Series expansion to calculate natural logarithm around a certain point a. 以下是泰勒级数展开式,用于计算某个点a附近的自然对数。

So q=xa, x is the user input between 1 and 2. 所以q = xa,x是1到2之间的用户输入。

  // These are converted constants into s4.27 fixed point format
  const int32_t con=0x0B8AA3B3; //1.44269504088895
  const int32_t c0=0x033E647E; //0.40546510810816
  const int32_t c1=0x05555555; //0.66666666666666
  const int32_t c2=0x01C71C72; //0.222222222222
  const int32_t c3=0x00CA4588; //0.0987654321
  const int32_t c4=0x006522C4; //0.04938271605
  const int32_t c5=0x0035F069; //0.02633744856
  const int32_t c6=0x001DF757; //0.01463191587

//Expanded taylor series    
taylor=c0+mul(q,(c1-mul(q,(c2+mul(q,(c3-mul(q,(c4-mul(q,(c5+mul(q,c6)))))))))));
// Multiplication function
int32_t mul(int32_t x, int32_t y)
{
int32_t mul;
mul=((((x)>>13)*((y)>>13))>>1); // for s4.27 format, the best possible right shift
return mul;
}

Above mentioned code snippets were used in C. 上面提到的代码段用在C语言中。

Result I need: 0.584963 but the result I got is: 0.584949 我需要的结果:0.584963但我得到的结果是:0.584949

How can I achieve more precision? 如何获得更高的精度?

OP's mul() throws away too much precision. OP的mul()放弃了太多的精度。

(x)>>13)*((y)>>13) immediately discards the least significant 13 bits of x and y . (x)>>13)*((y)>>13)立即丢弃xy的最低有效13位。

Instead, perform a 64-bit multiply 而是执行64位乘法

int32_t mul_better(int32_t x, int32_t y) {
  int64_t mul = x;
  mul *= y;
  mul >>= 27;

  // Code may want to detect overflow here (not shown)

  return (int32_t) mul;
}

Even better, round the product to nearest (ties to even) before discarding the least significant bits. 更好的是,在舍弃最低有效位之前,将乘积四舍五入至最接近(等于偶数)。 Simplifications are possible. 简化是可能的。 Verbose code below as it is illustrative. 下面的详细代码仅供参考。

int32_t mul_better(int32_t x, int32_t y) {
  int64_t mul = x;
  mul *= y;
  int32_t least = mul % ((int32_t)1 << 27);
  mul /= (int32_t)1 << 27;
  int carry = 0;
  if (least >= 0) {
    if (least >  ((int32_t)1 << 26) carry = 1;
    else if ((least ==  ((int32_t)1 << 26)) && (mul % 2)) carry = 1;
  } else {
    if (-least > ((int32_t)1 << 26) carry = -1;
    else if ((-least ==  ((int32_t)1 << 26)) && (mul % 2)) carry = -1;
  }
  return (int32_t) (mul + carry);
}

int32_t mul(int32_t x, int32_t y) {
  int64_t mul = x;
  mul *= y;
  return mul >> 27;
}

void foo(double x) {
  int32_t q = (int32_t) (x * (1 << 27));  // **
  int32_t taylor =
      c0 + mul(q, (c1 - mul(q, (c2  + mul(q,
      (c3 - mul(q, (c4 - mul(q, (c5 + mul(q, c6)))))))))));
  printf("%f %f\n", x,  taylor * 1.0 / (1 << 27));
}

int main(void) {
  foo(0.303609);
}

Output 输出量

0.303609 0.584963

** Could round here too rather than simply truncate the FP to an integer. **也可以在这里舍入,而不是简单地将FP截断为整数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM