decimal.cpp
9.98 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#include <poincare/decimal.h>
#include <poincare/complex.h>
#include <poincare/rational.h>
#include <poincare/opposite.h>
#include <assert.h>
#include <ion.h>
#include <cmath>
extern "C" {
#include <assert.h>
}
#include "layout/string_layout.h"
namespace Poincare {
int Decimal::exponent(const char * integralPart, int integralPartLength, const char * fractionalPart, int fractionalPartLength, const char * exponent, int exponentLength, bool exponentNegative) {
int base = 10;
int exp = 0;
for (int i = 0; i < exponentLength; i++) {
exp *= base;
exp += *exponent-'0';
exponent++;
}
if (exponentNegative) {
exp = -exp;
}
const char * integralPartEnd = integralPart + integralPartLength;
if (integralPart != nullptr) {
while (*integralPart == '0' && integralPart < integralPartEnd) {
integralPart++;
}
}
exp += integralPartEnd-integralPart-1;
if (integralPart == integralPartEnd) {
const char * fractionalPartEnd = fractionalPart + fractionalPartLength;
if (fractionalPart != nullptr) {
while (*fractionalPart == '0' && fractionalPart < fractionalPartEnd) {
fractionalPart++;
exp--;
}
}
if (fractionalPart == fractionalPartEnd) {
exp += fractionalPartLength+1;
}
}
return exp;
}
void removeZeroAtTheEnd(Integer & i) {
if (i.isZero()) {
return;
}
Integer base = Integer(10);
IntegerDivision d = Integer::Division(i, base);
while (d.remainder.isZero()) {
i = d.quotient;
d = Integer::Division(i, base);
}
}
Integer Decimal::mantissa(const char * integralPart, int integralPartLength, const char * fractionalPart, int fractionalPartLength, bool negative) {
Integer zero = Integer(0);
Integer base = Integer(10);
Integer numerator = Integer(integralPart, negative);
for (int i = 0; i < fractionalPartLength; i++) {
numerator = Integer::Multiplication(numerator, base);
numerator = Integer::Addition(numerator, Integer(*fractionalPart-'0'));
fractionalPart++;
}
removeZeroAtTheEnd(numerator);
return numerator;
}
Decimal::Decimal(Integer mantissa, int exponent) :
m_mantissa(mantissa),
m_exponent(exponent)
{
}
Decimal::Decimal(double f) {
double logBase10 = f != 0 ? std::log10(std::fabs(f)) : 0;
int exponentInBase10 = std::floor(logBase10);
/* Correct the exponent in base 10: sometines the exact log10 of f is 6.999999
* but is stored as 7 in hardware. We catch these cases here. */
if (f != 0 && logBase10 == (int)logBase10 && std::fabs(f) < std::pow(10, logBase10)) {
exponentInBase10--;
}
double m = f*std::pow(10, (double)-exponentInBase10); // TODO: hangle exponentInBase10 is too big! mantissa is nan
m = m * std::pow(10, (double)(k_doublePrecision-1));
int64_t integerMantissa = std::round(m);
/* If m > 999999999999999.5, the mantissa stored will be 1 (as we keep only
* 15 significative numbers from double. In that case, the exponent must be
* increment as well. */
if (m >= k_biggestMantissaFromDouble+0.5) {
exponentInBase10++;
}
m_mantissa = Integer(integerMantissa);
removeZeroAtTheEnd(m_mantissa);
m_exponent = exponentInBase10;
}
Expression::Type Decimal::type() const {
return Type::Decimal;
}
Expression * Decimal::clone() const {
return new Decimal(m_mantissa, m_exponent);
}
template<typename T> Expression * Decimal::templatedApproximate(Context& context, Expression::AngleUnit angleUnit) const {
T m = m_mantissa.approximate<T>();
int numberOfDigits = numberOfDigitsInMantissaWithoutSign();
return new Complex<T>(Complex<T>::Float(m*std::pow((T)10.0, (T)(m_exponent-numberOfDigits+1))));
}
int Decimal::writeTextInBuffer(char * buffer, int bufferSize, int numberOfSignificantDigits) const {
if (bufferSize == 0) {
return -1;
}
buffer[bufferSize-1] = 0;
int currentChar = 0;
if (currentChar >= bufferSize-1) { return bufferSize-1; }
if (m_mantissa.isZero()) {
buffer[currentChar++] = '0';
buffer[currentChar] = 0;
return currentChar;
}
char tempBuffer[200];
int mantissaLength = m_mantissa.writeTextInBuffer(tempBuffer, 200);
if (strcmp(tempBuffer, "undef") == 0) {
strlcpy(buffer, tempBuffer, bufferSize);
return mantissaLength;
}
int nbOfDigitsInMantissaWithoutSign = numberOfDigitsInMantissaWithoutSign();
int numberOfRequiredDigits = nbOfDigitsInMantissaWithoutSign > m_exponent ? nbOfDigitsInMantissaWithoutSign : m_exponent;
numberOfRequiredDigits = m_exponent < 0 ? 1+nbOfDigitsInMantissaWithoutSign-m_exponent : numberOfRequiredDigits;
/* Case 0: the number would be too long if we print it as a natural decimal */
if (numberOfRequiredDigits > k_maxLength) {
if (nbOfDigitsInMantissaWithoutSign == 1) {
currentChar += strlcpy(buffer, tempBuffer, bufferSize);
} else {
currentChar++;
if (currentChar >= bufferSize-1) { return bufferSize-1; }
currentChar += strlcpy(buffer+currentChar, tempBuffer, bufferSize-currentChar);
int decimalMarkerPosition = 1;
if (buffer[1] == '-') {
decimalMarkerPosition++;
buffer[0] = buffer[1];
}
buffer[decimalMarkerPosition-1] = buffer[decimalMarkerPosition];
buffer[decimalMarkerPosition] = '.';
}
if (m_exponent == 0) {
return currentChar;
}
if (currentChar >= bufferSize-1) { return bufferSize-1; }
buffer[currentChar++] = Ion::Charset::Exponent;
currentChar += Integer(m_exponent).writeTextInBuffer(buffer+currentChar, bufferSize-currentChar);
return currentChar;
}
/* Case 2: Print a natural decimal number */
int deltaCharMantissa = m_exponent < 0 ? -m_exponent+1 : 0;
strlcpy(buffer+deltaCharMantissa, tempBuffer, bufferSize-deltaCharMantissa);
if (m_mantissa.isNegative()) {
buffer[currentChar++] = '-';
}
if (m_exponent < 0) {
for (int i = 0; i <= -m_exponent; i++) {
if (currentChar >= bufferSize-1) { return bufferSize-1; }
if (i == 1) {
buffer[currentChar++] = '.';
continue;
}
buffer[currentChar++] = '0';
}
}
/* If mantissa is negative, m_mantissa.writeTextInBuffer is going to add an
* unwanted '-' in place of the temp char. We store it to replace it back
* after calling m_mantissa.writeTextInBuffer. */
char tempChar = 0;
int tempCharPosition = 0;
if (m_mantissa.isNegative()) {
currentChar--;
tempChar = buffer[currentChar];
tempCharPosition = currentChar;
}
currentChar += mantissaLength;
if (m_mantissa.isNegative()) { // replace the temp char back
buffer[tempCharPosition] = tempChar;
}
int currentExponent = m_mantissa.isNegative() ? currentChar-2 : currentChar-1;
if (m_exponent >= 0 && m_exponent < currentExponent) {
if (currentChar+1 >= bufferSize-1) { return bufferSize-1; }
int decimalMarkerPosition = m_mantissa.isNegative() ? m_exponent +1 : m_exponent;
for (int i = currentChar-1; i > decimalMarkerPosition; i--) {
buffer[i+1] = buffer[i];
}
buffer[decimalMarkerPosition+1] = '.';
currentChar++;
}
if (m_exponent >= 0 && m_exponent > currentExponent) {
int decimalMarkerPosition = m_mantissa.isNegative() ? m_exponent+1 : m_exponent;
for (int i = currentChar-1; i < decimalMarkerPosition; i++) {
if (currentChar+1 >= bufferSize-1) { return bufferSize-1; }
buffer[currentChar++] = '0';
}
}
buffer[currentChar] = 0;
return currentChar;
}
bool Decimal::needParenthesisWithParent(const Expression * e) const {
if (sign() == Sign::Positive) {
return false;
}
Type types[] = {Type::Addition, Type::Subtraction, Type::Opposite, Type::Multiplication, Type::Division, Type::Power, Type::Factorial};
return e->isOfType(types, 7);
}
ExpressionLayout * Decimal::privateCreateLayout(FloatDisplayMode floatDisplayMode, ComplexFormat complexFormat) const {
char buffer[255];
int numberOfChars = writeTextInBuffer(buffer, 255);
return new StringLayout(buffer, numberOfChars);
}
Expression * Decimal::shallowReduce(Context& context, AngleUnit angleUnit) {
Expression * e = Expression::shallowReduce(context, angleUnit);
if (e != this) {
return e;
}
// Do not reduce decimal to rational if the exponent is too big or too small.
if (m_exponent > k_maxDoubleExponent || m_exponent < -k_maxDoubleExponent) {
return this; // TODO: return new Infinite() ? new Rational(0) ?
}
int numberOfDigits = numberOfDigitsInMantissaWithoutSign();
Integer numerator = m_mantissa;
Integer denominator = Integer(1);
if (m_exponent >= numberOfDigits-1) {
numerator = Integer::Multiplication(m_mantissa, Integer::Power(Integer(10), Integer(m_exponent-numberOfDigits+1)));
} else {
denominator = Integer::Power(Integer(10), Integer(numberOfDigits-1-m_exponent));
}
return replaceWith(new Rational(numerator, denominator), true);
}
Expression * Decimal::shallowBeautify(Context & context, AngleUnit angleUnit) {
if (m_mantissa.isNegative()) {
m_mantissa.setNegative(false);
Opposite * o = new Opposite(this, true);
return replaceWith(o, true);
}
return this;
}
int Decimal::simplificationOrderSameType(const Expression * e, bool canBeInterrupted) const {
assert(e->type() == Type::Decimal);
const Decimal * other = static_cast<const Decimal *>(e);
if (sign() == Sign::Negative && other->sign() == Sign::Positive) {
return -1;
}
if (sign() == Sign::Positive && other->sign() == Sign::Negative) {
return 1;
}
assert(sign() == other->sign());
int unsignedComparison = 0;
if (exponent() < other->exponent()) {
unsignedComparison = -1;
} else if (exponent() > other->exponent()) {
unsignedComparison = 1;
} else {
assert(exponent() == other->exponent());
unsignedComparison = Integer::NaturalOrder(mantissa(), other->mantissa());
}
return ((int)sign())*unsignedComparison;
}
int Decimal::numberOfDigitsInMantissaWithoutSign() const {
int numberOfDigits = 1;
Integer mantissaCopy = m_mantissa;
mantissaCopy.setNegative(false);
IntegerDivision d = Integer::Division(mantissaCopy, Integer(10));
while (!d.quotient.isZero()) {
mantissaCopy = d.quotient;
d = Integer::Division(mantissaCopy, Integer(10));
numberOfDigits++;
}
return numberOfDigits;
}
}