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
| ///////////////////////////////////////////////////////////////////////////
// //
// Copyright (c) 2015 by Charta Software B.V. //
// All rights reserved //
// //
// Version: 1.7.0.83525 //
// Web site: https://pascal.chartasoftware.com/ //
// //
// This code and information are provided "as is" without warranty of //
// any kind. Dissemination of this information or reproduction of //
// this material is strictly forbidden unless prior written permission //
// is obtained from Charta Software B.V.. //
// //
///////////////////////////////////////////////////////////////////////////
unit Decimal;
interface
uses
Boolean,
Exception,
FloatingPoint._64,
Integer,
Integer._32,
Mathematics.Sign,
Text;
type
EDecimal = class(EException);
TDecimal = record
public
Significand: TInteger;
Exponent: TInteger32;
function ToText(): TText;
function ToFloatingPoint64(): TFloatingPoint64;
function ToInteger(): TInteger;
function EqualRepresentation(const Value: TDecimal): TBoolean;
function SetExponent(Value: TInteger32): TDecimal;
function Scale(Value: TInteger32): TDecimal;
private
class procedure MatchExponents(var Left: TDecimal; var Right: TDecimal); static;
public
class function New(const Significand: TInteger; Exponent: TInteger32): TDecimal; static;
class function FromText(const Value: TText): TDecimal; static;
class function FromFloatingPoint64(Value: TFloatingPoint64): TDecimal; static;
class function Compare(Left: TDecimal; Right: TDecimal): TSign; static;
class operator Implicit(const Operand: TInteger32): TDecimal;
class operator Implicit(const Operand: TInteger): TDecimal;
class operator Equal(const Left: TDecimal; const Right: TDecimal): TBoolean;
class operator NotEqual(const Left: TDecimal; const Right: TDecimal): TBoolean;
class operator LessThan(const Left: TDecimal; const Right: TDecimal): TBoolean;
class operator LessThanOrEqual(const Left: TDecimal; const Right: TDecimal): TBoolean;
class operator GreaterThan(const Left: TDecimal; const Right: TDecimal): TBoolean;
class operator GreaterThanOrEqual(const Left: TDecimal; const Right: TDecimal): TBoolean;
class operator Positive(const Operand: TDecimal): TDecimal;
class operator Negative(const Operand: TDecimal): TDecimal;
class operator Add(Left: TDecimal; Right: TDecimal): TDecimal;
class operator Subtract(Left: TDecimal; Right: TDecimal): TDecimal;
class operator Multiply(const Left: TDecimal; const Right: TDecimal): TDecimal;
end;
implementation
uses
FloatingPoint._64.TextFormat,
Integer._32.TextFormat,
Integer.TextFormat.Standard,
Mathematics;
{ TDecimal }
class operator TDecimal.Add(Left: TDecimal; Right: TDecimal): TDecimal;
begin
MatchExponents(Left, Right);
Result.Significand := Left.Significand + Right.Significand;
Result.Exponent := Left.Exponent;
end;
class function TDecimal.Compare(Left: TDecimal; Right: TDecimal): TSign;
begin
MatchExponents(Left, Right);
Result := TInteger.Compare(Left.Significand, Right.Significand);
end;
class operator TDecimal.Equal(const Left: TDecimal; const Right: TDecimal): TBoolean;
begin
Result := Compare(Left, Right) = sZero;
end;
function TDecimal.EqualRepresentation(const Value: TDecimal): TBoolean;
begin
Result := (Self.Exponent = Value.Exponent) and (Self.Significand = Value.Significand);
end;
class function TDecimal.FromFloatingPoint64(Value: TFloatingPoint64): TDecimal;
begin
// TODO: We should not first convert to text and then to decimal. Instead we should natively implement this function and use
// it in the implementation of FloatingPoint64ToText()
Result := TDecimal.FromText(FloatingPoint64ToText(Value));
end;
class function TDecimal.FromText(const Value: TText): TDecimal;
var
IndexOfDot: TInteger32;
IndexOfExponent: TInteger32;
Mantissa: TText;
Significand: TInteger;
Exponent: TInteger32;
begin
// TODO: This can be implemented much better
IndexOfExponent := Value.IndexOf('e');
if IndexOfExponent = -1 then
IndexOfExponent := Value.IndexOf('E');
if IndexOfExponent <> -1 then
begin
Exponent := TextToInteger32(Value.SubtextFrom(IndexOfExponent + 1));
Mantissa := Value.SubtextTo(IndexOfExponent - 1);
end
else
begin
Mantissa := Value;
Exponent := 0;
end;
IndexOfDot := Mantissa.IndexOf('.');
if IndexOfDot <> -1 then
begin
Significand := TStandardIntegerTextFormat.Decimal.FromText(Mantissa.SubtextTo(IndexOfDot - 1) + Mantissa.SubtextFrom(IndexOfDot + 1));
Exponent := Exponent + IndexOfDot - Mantissa.Count + 1;
end
else
Significand := TStandardIntegerTextFormat.Decimal.FromText(Value);
Result := TDecimal.New(Significand, Exponent);
end;
class operator TDecimal.GreaterThan(const Left: TDecimal; const Right: TDecimal): TBoolean;
begin
Result := Compare(Left, Right) > sZero;
end;
class operator TDecimal.GreaterThanOrEqual(const Left: TDecimal; const Right: TDecimal): TBoolean;
begin
Result := Compare(Left, Right) >= sZero;
end;
class operator TDecimal.Implicit(const Operand: TInteger32): TDecimal;
begin
Result.Significand := Operand;
Result.Exponent := 0;
end;
class operator TDecimal.Implicit(const Operand: TInteger): TDecimal;
begin
Result.Significand := Operand;
Result.Exponent := 0;
end;
class operator TDecimal.LessThan(const Left: TDecimal; const Right: TDecimal): TBoolean;
begin
Result := Compare(Left, Right) < sZero;
end;
class operator TDecimal.LessThanOrEqual(const Left: TDecimal; const Right: TDecimal): TBoolean;
begin
Result := Compare(Left, Right) <= sZero;
end;
class procedure TDecimal.MatchExponents(var Left: TDecimal; var Right: TDecimal);
begin
if Left.Exponent > Right.Exponent then
Left := Left.SetExponent(Right.Exponent)
else if Right.Exponent > Left.Exponent then
Right := Right.SetExponent(Left.Exponent);
end;
class operator TDecimal.Multiply(const Left: TDecimal; const Right: TDecimal): TDecimal;
begin
Result.Significand := Left.Significand * Right.Significand;
Result.Exponent := Left.Exponent + Right.Exponent;
end;
class operator TDecimal.Negative(const Operand: TDecimal): TDecimal;
begin
Result.Significand := -Operand.Significand;
Result.Exponent := Operand.Exponent;
end;
class function TDecimal.New(const Significand: TInteger; Exponent: TInteger32): TDecimal;
begin
Result.Significand := Significand;
Result.Exponent := Exponent;
end;
class operator TDecimal.NotEqual(const Left: TDecimal; const Right: TDecimal): TBoolean;
begin
Result := Compare(Left, Right) <> sZero;
end;
class operator TDecimal.Positive(const Operand: TDecimal): TDecimal;
begin
Result := Operand;
end;
function TDecimal.Scale(Value: TInteger32): TDecimal;
begin
Result.Significand := Significand;
Result.Exponent := Exponent + Value;
end;
function TDecimal.SetExponent(Value: TInteger32): TDecimal;
begin
if Value < Exponent then
begin
Result.Significand := Significand.MultiplyWithPowerOfTen(Exponent - Value);
Result.Exponent := Value;
end
else if Value = Exponent then
Result := Self
else
raise EDecimal.Create('Increasing exponent not supported');
end;
class operator TDecimal.Subtract(Left: TDecimal; Right: TDecimal): TDecimal;
begin
MatchExponents(Left, Right);
Result.Significand := Left.Significand - Right.Significand;
Result.Exponent := Left.Exponent;
end;
function TDecimal.ToFloatingPoint64(): TFloatingPoint64;
begin
// TODO: We should raise an exception when the Decimal can not be fit into a TFloatingPoint64
Result := Significand.ToFloatingPoint64() * IntegerPower(10, Exponent);
end;
function TDecimal.ToInteger(): TInteger;
begin
if Exponent > 0 then
Result := Significand.MultiplyWithPowerOfTen(Exponent)
else if Exponent = 0 then
Result := Significand
else
raise EDecimal.Create('Cannot convert decimal with fraction to an integer');
end;
function TDecimal.ToText(): TText;
var
IndexOfDot: TInteger32;
begin
Result := Significand.ToText();
if Exponent > 0 then
Result := Result + TText.NewRepeated('0', Exponent)
else if Exponent < 0 then
begin
IndexOfDot := Result.Count + Exponent - 1;
if IndexOfDot >= 0 then
Result := Result.SubtextTo(IndexOfDot) + '.' + Result.SubtextFrom(IndexOfDot + 1)
else
Result := '0.' + TText.NewRepeated('0', -IndexOfDot - 1) + Result;
end;
end;
end.
|