cprover
Toggle main menu visibility
Loading...
Searching...
No Matches
rational_tools.cpp
Go to the documentation of this file.
1
/*******************************************************************\
2
3
Module: Rational Numbers
4
5
Author: 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
18
bool
to_rational
(
const
exprt
&expr,
rationalt
&rational_value)
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)
57
rational_value =
rationalt
{-
string2integer
(no1)};
58
else
59
rational_value =
rationalt
{
string2integer
(no1)};
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
{
71
DATA_INVARIANT
(
72
no2.back() !=
'0'
,
"decimal suffix should not have trailing zeros"
);
73
rational_value +=
74
rationalt
(
string2integer
(no2)) /
rationalt
(
power
(10, no2.size()));
75
}
76
break
;
77
78
case
'/'
:
79
rational_value/=
rationalt
(
string2integer
(no2));
80
break
;
81
82
default
:
83
return
true
;
84
}
85
86
return
false
;
87
}
88
89
constant_exprt
from_rational
(
const
rationalt
&a)
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
}
power
mp_integer power(const mp_integer &base, const mp_integer &exponent)
A multi-precision implementation of the power operator.
Definition
arith_tools.cpp:207
arith_tools.h
constant_exprt
A constant literal expression.
Definition
std_expr.h:2997
exprt
Base class for all expressions.
Definition
expr.h:57
exprt::is_constant
bool is_constant() const
Return whether the expression is a constant.
Definition
expr.h:213
irept::get_string
const std::string & get_string(const irep_idt &name) const
Definition
irep.h:401
rational_typet
Unbounded, signed rational numbers.
Definition
mathematical_types.h:48
rationalt
Definition
rational.h:16
rationalt::get_numerator
const mp_integer & get_numerator() const
Definition
rational.h:85
rationalt::get_denominator
const mp_integer & get_denominator() const
Definition
rational.h:90
mathematical_types.h
Mathematical types.
string2integer
const mp_integer string2integer(const std::string &n, unsigned base)
Definition
mp_arith.cpp:54
integer2string
const std::string integer2string(const mp_integer &n, unsigned base)
Definition
mp_arith.cpp:103
rational.h
to_rational
bool to_rational(const exprt &expr, rationalt &rational_value)
Definition
rational_tools.cpp:18
from_rational
constant_exprt from_rational(const rationalt &a)
Definition
rational_tools.cpp:89
rational_tools.h
DATA_INVARIANT
#define DATA_INVARIANT(CONDITION, REASON)
This condition should be used to document that assumptions that are made on goto_functions,...
Definition
invariant.h:534
PRECONDITION
#define PRECONDITION(CONDITION)
Definition
invariant.h:463
util
rational_tools.cpp
Generated by
1.17.0