Esempio n. 1
0
void test_SingleDecimalRounding(void)
{
	s_fp test = (2 << 16) + (1 << 14)*3; // 2 + 0.25*3 = 2.75

	TEST_ASSERT_EQUAL_STRING("2.8", fptoa(test, 1));
	TEST_ASSERT_EQUAL_STRING("2750.0", fptoms(test, 1));
}
Esempio n. 2
0
void test_NegativeIntegerPositiveFraction(void)
{
	s_fp test = (-200 << 16) + (1 << 14)*3; // -200 + 0.75

	TEST_ASSERT_EQUAL_STRING("-199.250000", fptoa(test, SFP_MAX_PRECISION));
	TEST_ASSERT_EQUAL_STRING("-199250.000", fptoms(test, SFP_MAX_PRECISION));
}
Esempio n. 3
0
void test_SingleDecimalInteger(void)
{
	s_fp test = 300 << 16; // 300

	TEST_ASSERT_EQUAL_STRING("300.0", fptoa(test, 1));
	TEST_ASSERT_EQUAL_STRING("300000.0", fptoms(test, 1));
}
Esempio n. 4
0
void test_PositiveIntegerNegativeFraction(void)
{
	s_fp test = (300 << 16) - (1 << 14); // 300 - 0.25

	TEST_ASSERT_EQUAL_STRING("299.750000", fptoa(test, SFP_MAX_PRECISION));
	TEST_ASSERT_EQUAL_STRING("299750.000", fptoms(test, SFP_MAX_PRECISION));
}
Esempio n. 5
0
void test_NegativeIntegerNegativeFraction(void)
{
	s_fp test = (-200 << 16) - (1 << 15); // -200 - 0.5

	TEST_ASSERT_EQUAL_STRING("-200.500000", fptoa(test, SFP_MAX_PRECISION));
	TEST_ASSERT_EQUAL_STRING("-200500.000", fptoms(test, SFP_MAX_PRECISION));
}
Esempio n. 6
0
void test_PositiveIntegerPositiveFraction(void)
{
	s_fp test = (300 << 16) + (1 << 15); // 300 + 0.5

	TEST_ASSERT_EQUAL_STRING("300.500000", fptoa(test, SFP_MAX_PRECISION));
	TEST_ASSERT_EQUAL_STRING("300500.000", fptoms(test, SFP_MAX_PRECISION));
}
Esempio n. 7
0
void test_NegativeInteger(void)
{
	s_fp test = -200 << 16; // exact -200.000000

	TEST_ASSERT_EQUAL_STRING("-200.000000", fptoa(test, SFP_MAX_PRECISION));
	TEST_ASSERT_EQUAL_STRING("-200000.000", fptoms(test, SFP_MAX_PRECISION));
}
Esempio n. 8
0
void test_PositiveInteger(void)
{
	s_fp test = 300 << 16; // exact 300.000000

	TEST_ASSERT_EQUAL_STRING("300.000000", fptoa(test, SFP_MAX_PRECISION));
	TEST_ASSERT_EQUAL_STRING("300000.000", fptoms(test, SFP_MAX_PRECISION));
}
Esempio n. 9
0
/*
 * best float to string (lib)
 *
 * This is the real float-to-string routine.
 * It used by the routines:
 *   bestfta(double x, char *dest)
 *   expfta(double x, char *dest)
 */
void bestfta_p(var_num_t x, char *dest, var_num_t minx, var_num_t maxx) {
  var_num_t ipart, fpart, fdif;
  var_int_t power = 0;
  int sign, i;
  char *d = dest;
  char buf[64];

  memset(buf, 0, sizeof(buf));

  if (fabs(x) == 0.0) {
    strcpy(dest, "0");
    return;
  }

  // find sign
  sign = sgn(x);
  if (sign < 0) {
    *d++ = '-';
  }
  x = fabs(x);

  if (x >= 1E308) {
    *d = '\0';
    strcat(d, WORD_INF);
    return;
  } else if (x <= 1E-307) {
    *d = '\0';
    strcat(d, "0");
    return;
  }

  // find power
  if (x < minx) {
    for (i = 37; i >= 0; i--) {
      if (x < nfta_eminus[i]) {
        x *= nfta_eplus[i];
        power = -((i + 1) * 8);
      } else {
        break;
      }
    }

    while (x < 1.0 && power > -307) {
      x *= 10.0;
      power--;
    }
  } else if (x > maxx) {
    for (i = 37; i >= 0; i--) {
      if (x > nfta_eplus[i]) {
        x /= nfta_eplus[i];
        power = ((i + 1) * 8);
      } else {
        break;
      }
    }

    while (x >= 10.0 && power < 308) {
      x /= 10.0;
      power++;
    }
  }

  // format left part
  ipart = fabs(fint(x));
  fpart = fround(frac(x), FMT_RND) * FMT_xRND;
  if (fpart >= FMT_xRND) {      // rounding bug
    ipart = ipart + 1.0;
    if (ipart >= maxx) {
      ipart = ipart / 10.0;
      power++;
    }
    fpart = 0.0;
  }

  fptoa(ipart, buf);
  strcpy(d, buf);
  d += strlen(buf);

  if (fpart > 0.0) {
    // format right part
    *d++ = '.';

    fdif = frac(x) * FMT_xRND;
    if (fdif < fpart) {
      // rounded value has greater precision
      fdif = fpart;
    }

    while (fdif < FMT_xRND2) {
      fdif *= 10;
      *d++ = '0';
    }

    fptoa(rmzeros(fpart), buf);
    strcpy(d, buf);
    d += strlen(buf);
  }

  if (power) {
    // add the power
    *d++ = 'E';
    if (power > 0) {
      *d++ = '+';
    }
    fptoa(power, buf);
    strcpy(d, buf);
    d += strlen(buf);
  }

  // finish
  *d = '\0';
}