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