예제 #1
0
static mp_result read_rat_value(mp_rat q, char *str)
{
  int radix = 10;

  if(*str == '#') {
    ++str;
    switch(*str) {
    case 'x': case 'X':
      radix = 16;
      break;
    case 'd': case 'D':
      radix = 10;
      break;
    case 'o': case 'O':
      radix = 8;
      break;
    case 'b': case 'B':
      radix = 2;
      break;
    default:
      return MP_RANGE;
    }
    ++str;
  }

  if(*str == '@') 
    return mp_rat_read_decimal(q, radix, str + 1);
  else
    return mp_rat_read_string(q, radix, str);
}
예제 #2
0
int main(int argc, char *argv[]) {
  mp_result mode, len, res = 0;
  mp_size prec, radix;
  mpq_t value;
  char *buf;

  if (argc < 5) {
    fprintf(stderr, "Usage: rounding <mode> <precision> <radix> <value>\n");
    return 1;
  }

  if ((res = mp_rat_init(&value)) != MP_OK) {
    fprintf(stderr, "Error initializing: %s\n", mp_error_string(res));
    return 2;
  }

  mode = atoi(argv[1]);
  prec = atoi(argv[2]);
  radix = atoi(argv[3]);

  printf(
      "Rounding mode:   %d\n"
      "Precision:       %u digits\n"
      "Radix:           %u\n"
      "Input string:    \"%s\"\n",
      mode, prec, radix, argv[4]);

  if ((res = mp_rat_read_decimal(&value, radix, argv[4])) != MP_OK) {
    fprintf(stderr, "Error reading input string: %s\n", mp_error_string(res));
    goto CLEANUP;
  }

  len = mp_rat_decimal_len(&value, radix, prec);
  buf = malloc(len);

  if ((res = mp_rat_to_decimal(&value, radix, prec, mode, buf, len)) != MP_OK) {
    fprintf(stderr, "Error converting output: %s\n", mp_error_string(res));
  }

  printf("Result string:   \"%s\"\n", buf);
  free(buf);

CLEANUP:
  mp_rat_clear(&value);
  return res;
}
예제 #3
0
int test_qrdec(testspec_t *t, FILE *ofp)
{
  mp_rat out[1], reg = g_qreg + 1;
  long   radix;
  mp_result res, expect;

  if(!parse_rat_values(t, NULL, out, &expect)) 
    return imath_errno = MP_BADARG, 0;
  
  trim_line(t->input[1]);
  if(!read_long(&radix, t->input[1]))
    return imath_errno = MP_BADARG, 0;

  if((res = mp_rat_read_decimal(reg, radix, t->input[0])) != expect)
    return imath_errno = res, 0;

  if(expect == MP_OK &&
     mp_rat_compare(reg, out[0]) != 0) {
    mp_rat_to_string(reg, 10, g_output, OUTPUT_LIMIT);
    return imath_errno = OTHER_ERROR, 0;
  }

  return 1;
}