具有不同缩放因子的浮动到固定转换

发布于 2024-09-15 13:36:58 字数 317 浏览 7 评论 0原文

谁能告诉我当我将固定转换为浮动和浮动转换为固定时,这些方法之间有什么区别。

一个) int a=32767; float b = 32765*(1/32767) // 16 位缩放因子 int c = b*32767;

b) int a=32767; float b = 32765*(1/1.0) // 缩放因子=1 int c = b*1;

a) int a=32767; float b = 32765*(1/0x80000) // 24 位缩放因子 int c = b*(0x80000);

如果我的机器使用 Q23 定点表示,我应该使用哪个?

Can anyone please let me know What will be the difference between these approcahes when I convert fixed to float and float to fixed.

a)
int a=32767;
float b = 32765*(1/32767) // 16 bit scaling factor
int c = b*32767;

b)
int a=32767;
float b = 32765*(1/1.0) // scaling factor=1
int c = b*1;

a)
int a=32767;
float b = 32765*(1/0x80000) // 24 bit scaling factor
int c = b*(0x80000);

If my machine uses Q23 fixed point representation, which should I use ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟雨凡馨 2024-09-22 13:37:01

我没有找到有关您的机器使用的“Q23定点表示”的任何详细信息,因此我编写了自己的定义,编写了一些转换例程并测试了它们的一些值:

#include <limits.h>
#include <stdio.h>
#include <stdlib.h>

/**
 * q23 is a fixed-point two's-complement type:
 * - 1 bit sign,
 * - 8 bit integer part,
 * - 23 bit fraction part.
 *
 * long is assumed to be 32 bit two's complement without padding bits.
 */
typedef long q23;

static q23
cvt_float_to_q23(float f) {
  return f * (1 << 23);
}

static float
cvt_q23_to_float(q23 x) {
  return ((float) x) / (1 << 23);
}

/*
 * Here starts the testing code.
 */

static unsigned errors = 0;

static void
assert_q23_is(q23 fixed, float f) {
  if (cvt_q23_to_float(fixed) != f) {
    fprintf(stderr, "E: cvt_q23_to_float(%08lx) expected %.10f, got %.10f\n",
        fixed, f, cvt_q23_to_float(fixed));
    errors++;
  }
}

static void
assert_float_is(float f, q23 fixed) {
  if (cvt_float_to_q23(f) != fixed) {
    fprintf(stderr, "E: cvt_float_to_q23(%f) expected %08lx, got %08lx\n",
        f, fixed, cvt_float_to_q23(f));
    errors++;
  }
}

static void
assert_equals(q23 fixed, float f) {
  assert_q23_is(fixed, f);
  assert_float_is(f, fixed);
}

int
main() {
  /* Some values have equivalent representation. */
  assert_equals(LONG_MIN, -256.0f);
  assert_equals(LONG_MIN / 2, -128.0f);
  assert_equals(0, 0.0f);
  assert_equals(LONG_MAX / 2 + 1, 128.0f);

  /* There will be a fixpoint ... */
  assert_float_is(1.0 / 3, 0x002aaaaa);
  assert_q23_is(0x002aaaaa, 0.3333332539f);

  /* float only has 24 bits of precision */
  assert_equals(0x2aaaaac0, 256.0 / 3);

  if (errors == 0) {
    printf("ok\n");
    return EXIT_SUCCESS;
  }
  return EXIT_FAILURE;
}

一些备注:

  • 如果您需要饱和舍入,必须通过检查 cvt_float_to_q23 的参数来自己实现。
  • 到处都会有舍入误差,而且这是不可避免的。请务必妥善处理它们。

I didn't find any detailed information about the "Q23 fixed point representation" that your machine uses, so I made up my own definition, wrote some conversion routines and tested them for some few values:

#include <limits.h>
#include <stdio.h>
#include <stdlib.h>

/**
 * q23 is a fixed-point two's-complement type:
 * - 1 bit sign,
 * - 8 bit integer part,
 * - 23 bit fraction part.
 *
 * long is assumed to be 32 bit two's complement without padding bits.
 */
typedef long q23;

static q23
cvt_float_to_q23(float f) {
  return f * (1 << 23);
}

static float
cvt_q23_to_float(q23 x) {
  return ((float) x) / (1 << 23);
}

/*
 * Here starts the testing code.
 */

static unsigned errors = 0;

static void
assert_q23_is(q23 fixed, float f) {
  if (cvt_q23_to_float(fixed) != f) {
    fprintf(stderr, "E: cvt_q23_to_float(%08lx) expected %.10f, got %.10f\n",
        fixed, f, cvt_q23_to_float(fixed));
    errors++;
  }
}

static void
assert_float_is(float f, q23 fixed) {
  if (cvt_float_to_q23(f) != fixed) {
    fprintf(stderr, "E: cvt_float_to_q23(%f) expected %08lx, got %08lx\n",
        f, fixed, cvt_float_to_q23(f));
    errors++;
  }
}

static void
assert_equals(q23 fixed, float f) {
  assert_q23_is(fixed, f);
  assert_float_is(f, fixed);
}

int
main() {
  /* Some values have equivalent representation. */
  assert_equals(LONG_MIN, -256.0f);
  assert_equals(LONG_MIN / 2, -128.0f);
  assert_equals(0, 0.0f);
  assert_equals(LONG_MAX / 2 + 1, 128.0f);

  /* There will be a fixpoint ... */
  assert_float_is(1.0 / 3, 0x002aaaaa);
  assert_q23_is(0x002aaaaa, 0.3333332539f);

  /* float only has 24 bits of precision */
  assert_equals(0x2aaaaac0, 256.0 / 3);

  if (errors == 0) {
    printf("ok\n");
    return EXIT_SUCCESS;
  }
  return EXIT_FAILURE;
}

Some remarks:

  • If you need saturated rounding you have to implement that yourself by checking the argument to cvt_float_to_q23.
  • There will be rounding errors everywhere, and they are inevitable. Be sure to handle them appropriately.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文