Polly 20.0.0git
rounding.c
Go to the documentation of this file.
1/*
2 Name: rounding.c
3 Purpose: Demonstrates rounding modes.
4 Author: M. J. Fromberger
5
6 Bugs: The rounding mode can only be specified by value, not name.
7
8 Copyright (C) 2002-2008 Michael J. Fromberger, All Rights Reserved.
9
10 Permission is hereby granted, free of charge, to any person obtaining a copy
11 of this software and associated documentation files (the "Software"), to deal
12 in the Software without restriction, including without limitation the rights
13 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 copies of the Software, and to permit persons to whom the Software is
15 furnished to do so, subject to the following conditions:
16
17 The above copyright notice and this permission notice shall be included in
18 all copies or substantial portions of the Software.
19
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 SOFTWARE.
27 */
28#include <limits.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32
33#include "imath.h"
34#include "imrat.h"
35
36int main(int argc, char *argv[]) {
37 mp_result mode, len, res = 0;
38 mp_size prec, radix;
39 mpq_t value;
40 char *buf;
41
42 if (argc < 5) {
43 fprintf(stderr, "Usage: rounding <mode> <precision> <radix> <value>\n");
44 return 1;
45 }
46
47 if ((res = mp_rat_init(&value)) != MP_OK) {
48 fprintf(stderr, "Error initializing: %s\n", mp_error_string(res));
49 return 2;
50 }
51
52 mode = atoi(argv[1]);
53 prec = atoi(argv[2]);
54 radix = atoi(argv[3]);
55
56 printf(
57 "Rounding mode: %d\n"
58 "Precision: %u digits\n"
59 "Radix: %u\n"
60 "Input string: \"%s\"\n",
61 mode, prec, radix, argv[4]);
62
63 if ((res = mp_rat_read_decimal(&value, radix, argv[4])) != MP_OK) {
64 fprintf(stderr, "Error reading input string: %s\n", mp_error_string(res));
65 goto CLEANUP;
66 }
67
68 len = mp_rat_decimal_len(&value, radix, prec);
69 buf = malloc(len);
70
71 if ((res = mp_rat_to_decimal(&value, radix, prec, mode, buf, len)) != MP_OK) {
72 fprintf(stderr, "Error converting output: %s\n", mp_error_string(res));
73 }
74
75 printf("Result string: \"%s\"\n", buf);
76 free(buf);
77
78CLEANUP:
79 mp_rat_clear(&value);
80 return res;
81}
82
83/* Here there be dragons */
unsigned int mp_size
Definition: imath/imath.h:39
int mp_result
Definition: imath/imath.h:40
const char * res
Definition: isl_test.c:775
#define mp_error_string
Definition: wrap.h:66
#define mp_rat_to_decimal
Definition: wrap.h:166
#define mp_rat_clear
Definition: wrap.h:131
#define mp_rat_init
Definition: wrap.h:144
#define mp_rat_read_decimal
Definition: wrap.h:155
#define mp_rat_decimal_len
Definition: wrap.h:137
#define MP_OK
Definition: wrap.h:9