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; }
int test_qtodec(testspec_t *t, FILE *ofp) { mp_rat in[4]; long radix, prec; mp_round_mode rmode; mp_result res, expect = MP_OK, len; if(!parse_rat_values(t, in, NULL, NULL)) return imath_errno = MP_BADARG, 0; if(t->output[0][0] == '$' && !parse_result_code(t->output[0], &expect)) { fprintf(stderr, "Line %d: Invalid result code [%s]\n", t->line, t->output[0]); return 0; } trim_line(t->input[1]); trim_line(t->input[2]); trim_line(t->input[3]); if(!read_long(&radix, t->input[1])) return imath_errno = MP_BADARG, 0; if(!read_long(&prec, t->input[2])) return imath_errno = MP_BADARG, 0; if(!read_long((long *)&rmode, t->input[3])) return imath_errno = MP_BADARG, 0; if(prec < 0) { fprintf(stderr, "Line %d: Precision %ld out of range\n", t->line, prec); return imath_errno = MP_RANGE, 0; } trim_line(t->output[0]); len = mp_rat_decimal_len(in[0], radix, prec); if((res = mp_rat_to_decimal(in[0], radix, prec, rmode, g_output, len)) != expect) return imath_errno = res, 0; if(res == MP_OK && strcmp(t->output[0], g_output) != 0) return imath_errno = OTHER_ERROR, 0; return 1; }