CBMC
Loading...
Searching...
No Matches
algebraic_number.cpp
Go to the documentation of this file.
1/*******************************************************************\
2
3Module: Algebraic Numbers
4
5Author: Michael Tautschnig
6
7\*******************************************************************/
8
11
12#include "algebraic_number.h"
13
14#include "mathematical_types.h"
15#include "rational_tools.h"
16
18{
19 if(coefficients.size() == 2 && coefficients.back().is_one())
20 {
21 // root of x - c
22 auto c = from_rational(-coefficients.front());
23 c.type() = real_typet{};
24 return c;
25 }
26 else
27 {
28 std::ostringstream oss;
29 oss << *this;
30 return constant_exprt{oss.str(), real_typet{}};
31 }
32}
33
34std::ostream &operator<<(std::ostream &out, const algebraic_numbert &a)
35{
36 out << "x ∈ ℝ.(";
37
38 const auto &coefficients = a.get_coefficients();
39
40 bool need_plus = false;
41 for(std::size_t d = coefficients.size(); d > 0; --d)
42 {
43 if(coefficients[d - 1].is_zero())
44 continue;
45 if(need_plus)
46 out << " + ";
47 if(d == 1)
48 out << coefficients[d - 1];
49 else
50 {
51 if(!coefficients[d - 1].is_one())
52 out << coefficients[d - 1] << "*";
53 out << "x^" << d - 1;
54 }
55 need_plus = true;
56 }
57
58 if(!need_plus)
59 out << "0";
60 out << " = 0)";
61
62 return out;
63}
std::ostream & operator<<(std::ostream &out, const algebraic_numbert &a)
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition ai.h:562
Represents real numbers as roots (zeros) of a polynomial with rational coefficients.
constant_exprt as_expr() const
coefficientst coefficients
A constant literal expression.
Definition std_expr.h:3118
Unbounded, signed real numbers.
Mathematical types.
constant_exprt from_rational(const rationalt &a)