summaryrefslogtreecommitdiff
path: root/src/util/rational.h
blob: e3f7600314bbff3c653009617f67a422fbf6e60e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*********************                                                        */
/** rational.h
 ** Original author: taking
 ** Major contributors: none
 ** Minor contributors (to current version): none
 ** This file is part of the CVC4 prototype.
 ** Copyright (c) 2009, 2010  The Analysis of Computer Systems Group (ACSys)
 ** Courant Institute of Mathematical Sciences
 ** New York University
 ** See the file COPYING in the top-level source directory for licensing
 ** information.
 **
 ** A multiprecision rational constant.
 **/

#include <gmpxx.h>
#include <string>
#include "integer.h"

#ifndef __CVC4__RATIONAL_H
#define __CVC4__RATIONAL_H

namespace CVC4 {

class Rational {
private:
  /**
   * Stores the value of the rational is stored in a C++ GMP rational class.
   * Using this instead of mpq_t allows for easier destruction.
   */
  mpq_class d_value;

  /**
   * Constructs a Rational from a mpq_class object.
   * Does a deep copy.
   * Assumes that the value is in canonical form, and thus does not
   * have to call canonicalize() on the value.
   */
  Rational(const mpq_class& val) : d_value(val) {  }

public:

  /** Constructs a rational with the value 0/1. */
  Rational() : d_value(0){
    d_value.canonicalize();
  }

  /**
   * Constructs a Rational from a C string.
   * Throws std::invalid_argument if the stribng is not a valid rational.
   * For more information about what is a vaid rational string,
   * see GMP's documentation for mpq_set_str().
   */
  Rational(const char * s, int base = 10): d_value(s,base) {
    d_value.canonicalize();
  }
  Rational(const std::string& s, unsigned base = 10) : d_value(s, base) {
    d_value.canonicalize();
  }

  Rational(const Rational& q) : d_value(q.d_value) {
    d_value.canonicalize();
  }

  Rational(  signed int n,   signed int d) : d_value(n,d) {
    d_value.canonicalize();
  }
  Rational(unsigned int n, unsigned int d) : d_value(n,d) {
    d_value.canonicalize();
  }
  Rational(  signed long int n,   signed long int d) : d_value(n,d) {
    d_value.canonicalize();
  }
  Rational(unsigned long int n, unsigned long int d) : d_value(n,d) {
    d_value.canonicalize();
  }

  Rational(const Integer& n, const Integer& d):
    d_value(n.get_mpz(), d.get_mpz())
  {
    d_value.canonicalize();
  }

  ~Rational() {}


  Integer getNumerator() const {
    return Integer(d_value.get_num());
  }

  Integer getDenominator() const{
    return Integer(d_value.get_den());
  }


  Rational& operator=(const Rational& x){
    if(this == &x) return *this;
    d_value = x.d_value;
    return *this;
  }

  Rational operator-() const{
    return Rational(-(d_value));
  }

  bool operator==(const Rational& y) const {
    return d_value == y.d_value;
  }

  bool operator!=(const Rational& y) const {
    return d_value != y.d_value;
  }

  bool operator< (const Rational& y) const {
    return d_value < y.d_value;
  }

  bool operator<=(const Rational& y) const {
    return d_value <= y.d_value;
  }

  bool operator> (const Rational& y) const {
    return d_value > y.d_value;
  }

  bool operator>=(const Rational& y) const {
    return d_value >= y.d_value;
  }


  Rational operator+(const Rational& y) const{
    return Rational( d_value + y.d_value );
  }

  Rational operator-(const Rational& y) const {
    return Rational( d_value - y.d_value );
  }

  Rational operator*(const Rational& y) const {
    return Rational( d_value * y.d_value );
  }
  Rational operator/(const Rational& y) const {
    return Rational( d_value / y.d_value );
  }

  std::string toString(int base = 10) const {
    return d_value.get_str(base);
  }


  friend std::ostream& operator<<(std::ostream& os, const Rational& n);


  /**
   * Computes the hash of the node from the first word of the
   * numerator, the denominator.
   */
  size_t hash() const {
    size_t numeratorHash = gmpz_hash(d_value.get_num_mpz_t());
    size_t denominatorHash = gmpz_hash(d_value.get_den_mpz_t());

    return numeratorHash xor denominatorHash;
  }

};/* class Rational */

std::ostream& operator<<(std::ostream& os, const Rational& n);

}/* CVC4 namespace */

#endif /* __CVC4__RATIONAL_H */

generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback