Question

Quelqu'un peut-il s'il vous plaît laissez-moi savoir Quelle sera la différence entre ces approcahes quand je convertir fixé à flotteur et flotteur fixe.

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);

Si ma machine utilise la représentation du point fixe Q23, qui dois-je utiliser?

Était-ce utile?

La solution

Je ne trouve pas d'informations détaillées sur la « Q23 représentation point fixe » que vos utilisations de la machine, donc je fait ma propre définition, écrit quelques routines de conversion et les ai testées pour certaines quelques valeurs:

#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;
}

Quelques remarques:

  • Si vous vous devez arrondir saturés ont à mettre en œuvre que vous en vérifiant l'argument cvt_float_to_q23.
  • Il y aura des erreurs d'arrondi partout, et ils sont inévitables. Assurez-vous de les gérer de façon appropriée.
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top