CBMC
Loading...
Searching...
No Matches
rational_tools.cpp
Go to the documentation of this file.
1/*******************************************************************\
2
3Module: Rational Numbers
4
5Author: Daniel Kroening, kroening@kroening.com
6
7\*******************************************************************/
8
11
12#include "rational_tools.h"
13
14#include "arith_tools.h"
15#include "mathematical_types.h"
16#include "rational.h"
17
19{
20 if(!expr.is_constant())
21 return true;
22
23 std::string value = expr.get_string(ID_value);
24 PRECONDITION(!value.empty());
25
26 std::string no1, no2;
27 char mode=0;
28
29 bool is_negative = false;
30 if(value[0] == '-')
31 {
32 is_negative = true;
33 value = value.substr(1);
34 }
35
36 for(const char ch : value)
37 {
38 if(isdigit(ch))
39 {
40 if(mode==0)
41 no1+=ch;
42 else
43 no2+=ch;
44 }
45 else if(ch=='/' || ch=='.')
46 {
47 if(mode==0)
48 mode=ch;
49 else
50 return true;
51 }
52 else
53 return true;
54 }
55
56 if(is_negative)
58 else
60
61 switch(mode)
62 {
63 case 0:
64 // do nothing
65 break;
66
67 case '.':
68 DATA_INVARIANT(!no2.empty(), "decimal suffix should not be empty");
69 if(no2 != "0")
70 {
72 no2.back() != '0', "decimal suffix should not have trailing zeros");
75 }
76 break;
77
78 case '/':
80 break;
81
82 default:
83 return true;
84 }
85
86 return false;
87}
88
90{
91 std::string d=integer2string(a.get_numerator());
92 if(a.get_denominator()!=1)
93 d+="/"+integer2string(a.get_denominator());
94 return constant_exprt(d, rational_typet());
95}
mp_integer power(const mp_integer &base, const mp_integer &exponent)
A multi-precision implementation of the power operator.
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition ai.h:562
A constant literal expression.
Definition std_expr.h:3118
Base class for all expressions.
Definition expr.h:56
bool is_constant() const
Return whether the expression is a constant.
Definition expr.h:212
const std::string & get_string(const irep_idt &name) const
Definition irep.h:401
Unbounded, signed rational numbers.
int isdigit(int c)
Definition ctype.c:24
Mathematical types.
const mp_integer string2integer(const std::string &n, unsigned base)
Definition mp_arith.cpp:54
const std::string integer2string(const mp_integer &n, unsigned base)
Definition mp_arith.cpp:103
bool to_rational(const exprt &expr, rationalt &rational_value)
constant_exprt from_rational(const rationalt &a)
#define DATA_INVARIANT(CONDITION, REASON)
This condition should be used to document that assumptions that are made on goto_functions,...
Definition invariant.h:534
#define PRECONDITION(CONDITION)
Definition invariant.h:463