Blame view

build3/poincare/src/rational.cpp 5.32 KB
6663b6c9   adorian   projet complet av...
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
173
174
175
176
177
178
  #include <poincare/rational.h>
  extern "C" {
  #include <stdlib.h>
  #include <string.h>
  #include <assert.h>
  #include <math.h>
  }
  #include <poincare/arithmetic.h>
  #include <poincare/opposite.h>
  #include "layout/string_layout.h"
  #include "layout/fraction_layout.h"
  
  namespace Poincare {
  
  // Constructors
  
  Rational::Rational(const Integer numerator, const Integer denominator) {
    assert(!denominator.isZero());
    if (numerator.isOne() || denominator.isOne()) {
      // Avoid computing GCD if possible
      m_numerator = numerator;
      m_denominator = denominator;
    } else {
      Integer gcd = Arithmetic::GCD(&numerator, &denominator);
      m_numerator = Integer::Division(numerator, gcd).quotient;
      m_denominator = Integer::Division(denominator, gcd).quotient;
    }
    if (m_numerator.isNegative() && m_denominator.isNegative()) {
      m_numerator.setNegative(false);
      m_denominator.setNegative(false);
    } else if (m_denominator.isNegative()) {
      m_numerator.setNegative(true);
      m_denominator.setNegative(false);
    }
  }
  
  Rational::Rational(const Integer numerator) {
    m_numerator = numerator;
    m_denominator = Integer(1);
  }
  
  Rational::Rational(const Rational & other) {
    m_numerator = other.m_numerator;
    m_denominator = other.m_denominator;
  }
  
  Rational & Rational::operator=(const Rational & other) {
    m_numerator = other.m_numerator;
    m_numerator = other.m_numerator;
    m_denominator = other.m_denominator;
    return *this;
  }
  
  // Getter
  const Integer Rational::numerator() const {
    return m_numerator;
  }
  
  const Integer Rational::denominator() const {
    return m_denominator;
  }
  // Expression subclassing
  
  Expression::Type Rational::type() const {
    return Type::Rational;
  }
  
  Expression * Rational::clone() const {
    return new Rational(m_numerator, m_denominator);
  }
  
  Expression::Sign Rational::sign() const {
    if (m_numerator.isNegative()) {
      return Sign::Negative;
    }
    return Sign::Positive;
  }
  
  Expression * Rational::setSign(Sign s) {
    assert(s != Sign::Unknown);
    bool negative = s == Sign::Negative ? true : false;
    m_numerator.setNegative(negative);
    return this;
  }
  
  Expression * Rational::shallowBeautify(Context & context, AngleUnit angleUnit) {
    if (m_numerator.isNegative()) {
      m_numerator.setNegative(false);
      Opposite * o = new Opposite(this, true);
      return replaceWith(o, true);
    }
    return this;
  }
  
  Expression * Rational::cloneDenominator(Context & context, AngleUnit angleUnit) const {
    if (m_denominator.isOne()) {
      return nullptr;
    }
    return new Rational(m_denominator);
  }
  
  // Basic operations
  
  Rational Rational::Addition(const Rational & i, const Rational & j) {
    Integer newNumerator = Integer::Addition(Integer::Multiplication(i.numerator(), j.denominator()), Integer::Multiplication(j.numerator(), i.denominator()));
    Integer newDenominator = Integer::Multiplication(i.denominator(), j.denominator());
    return Rational(newNumerator, newDenominator);
  }
  
  Rational Rational::Multiplication(const Rational & i, const Rational & j) {
    Integer newNumerator = Integer::Multiplication(i.numerator(), j.numerator());
    Integer newDenominator = Integer::Multiplication(i.denominator(), j.denominator());
    return Rational(newNumerator, newDenominator);
  }
  
  Rational Rational::Power(const Rational & i, const Integer & j) {
    Integer absJ = j;
    absJ.setNegative(false);
    Integer newNumerator = Integer::Power(i.numerator(), absJ);
    Integer newDenominator = Integer::Power(i.denominator(), absJ);
    if (j.isNegative()) {
      return Rational(newDenominator, newNumerator);
    }
    return Rational(newNumerator, newDenominator);
  }
  
  int Rational::NaturalOrder(const Rational & i, const Rational & j) {
    Integer i1 = Integer::Multiplication(i.numerator(), j.denominator());
    Integer i2 = Integer::Multiplication(i.denominator(), j.numerator());
    return Integer::NaturalOrder(i1, i2);
  }
  
  // Comparison
  
  int Rational::simplificationOrderSameType(const Expression * e, bool canBeInterrupted) const {
    assert(e->type() == Expression::Type::Rational);
    const Rational * other = static_cast<const Rational *>(e);
    return NaturalOrder(*this, *other);
  }
  
  template<typename T> Complex<T> * Rational::templatedApproximate(Context& context, Expression::AngleUnit angleUnit) const {
    T n = m_numerator.approximate<T>();
    T d = m_denominator.approximate<T>();
    return new Complex<T>(Complex<T>::Float(n/d));
  }
  
  bool Rational::needParenthesisWithParent(const Expression * e) const {
    if (m_denominator.isOne()) {
      return false;
    }
    Type types[] = {Type::Division, Type::Power, Type::Factorial};
    return e->isOfType(types, 3);
  }
  
  ExpressionLayout * Rational::privateCreateLayout(FloatDisplayMode floatDisplayMode, ComplexFormat complexFormat) const {
    ExpressionLayout * numeratorLayout = m_numerator.createLayout();
    if (m_denominator.isOne()) {
      return numeratorLayout;
    }
    ExpressionLayout * denominatorLayout = m_denominator.createLayout();
    return new FractionLayout(numeratorLayout, denominatorLayout);
  }
  
  int Rational::writeTextInBuffer(char * buffer, int bufferSize, int numberOfSignificantDigits) const {
    buffer[bufferSize-1] = 0;
    int numberOfChar = m_numerator.writeTextInBuffer(buffer, bufferSize);
    if (m_denominator.isOne()) {
      return numberOfChar;
    }
    if (numberOfChar >= bufferSize-1) {
      return numberOfChar;
    }
    buffer[numberOfChar++] = '/';
    numberOfChar += m_denominator.writeTextInBuffer(buffer+numberOfChar, bufferSize-numberOfChar);
    return numberOfChar;
  }
  
  }