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 | |
36 | int 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, format: "Usage: rounding <mode> <precision> <radix> <value>\n" ); |
44 | return 1; |
45 | } |
46 | |
47 | if ((res = mp_rat_init(r: &value)) != MP_OK) { |
48 | fprintf(stderr, format: "Error initializing: %s\n" , mp_error_string(res)); |
49 | return 2; |
50 | } |
51 | |
52 | mode = atoi(nptr: argv[1]); |
53 | prec = atoi(nptr: argv[2]); |
54 | radix = atoi(nptr: argv[3]); |
55 | |
56 | printf( |
57 | format: "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(r: &value, radix, str: argv[4])) != MP_OK) { |
64 | fprintf(stderr, format: "Error reading input string: %s\n" , mp_error_string(res)); |
65 | goto CLEANUP; |
66 | } |
67 | |
68 | len = mp_rat_decimal_len(r: &value, radix, prec); |
69 | buf = malloc(size: len); |
70 | |
71 | if ((res = mp_rat_to_decimal(r: &value, radix, prec, round: mode, str: buf, limit: len)) != MP_OK) { |
72 | fprintf(stderr, format: "Error converting output: %s\n" , mp_error_string(res)); |
73 | } |
74 | |
75 | printf(format: "Result string: \"%s\"\n" , buf); |
76 | free(ptr: buf); |
77 | |
78 | CLEANUP: |
79 | mp_rat_clear(r: &value); |
80 | return res; |
81 | } |
82 | |
83 | /* Here there be dragons */ |
84 | |