-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconstants.dart
465 lines (413 loc) · 15.1 KB
/
constants.dart
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
import 'term.dart';
enum _SpecialType {
neg_infinity, // unbounded negative limit
neg_overflow, // negative number too large to represent
neg_underflow, // negative number too small to represent
neg_vanishing, // approaching zero from below in the limit
pos_vanishing, // approaching zero from above in the limit
pos_underflow, // positive number too small to represent
pos_overflow, // positive number too large to represent
pos_infinity, // unbounded positive limit
indeterminate, // indeterminate or impossible number
}
/// A Term representing an ordinary number with no unknown values.
abstract class Constant extends Term {
const Constant._();
/// Returns the appropriate Constant Term object for the given double value, while
/// attempting to preserve the singleton nature of the hard-coded constants.
static Constant _forDouble(double v) {
if (v.isInfinite) return (v < 0) ? neg_infinity : pos_infinity;
if (v.isNaN) return indeterminate;
if (v == 0) return zero;
if (v == 1.0) return one;
if (v == -1.0) return neg_one;
return _Number(v);
}
@override bool negatesGracefully() => true;
bool overwhelmsProducts();
bool overwhelmsSums();
/// Helper method to add or subtract two values based on a signum boolean.
static double addOrSub(double v1, double v2, bool isSub) {
return isSub ? v1 - v2 : v1 + v2;
}
@override
Term addDirect(Term other, bool isNegated) {
if (other is Constant) {
return isNegated ? this - other : this + other;
}
return null;
}
@override bool equals(Term term) {
return (term is Constant && this._equals(term));
}
bool _equals(Constant other);
int compareTo(Constant other);
@override String toOutline() => 'K';
@override bool startsWithMinus() => isNegative();
}
class _Special extends Constant {
final _SpecialType _type;
final String _name;
const _Special(this._type, this._name) : super._();
@override
bool isNegative() {
switch (_type) {
case _SpecialType.neg_infinity:
case _SpecialType.neg_overflow:
case _SpecialType.neg_underflow:
case _SpecialType.neg_vanishing:
return true;
default:
return false;
}
}
@override
bool overwhelmsProducts() {
switch (_type) {
case _SpecialType.neg_infinity:
case _SpecialType.pos_infinity:
case _SpecialType.indeterminate:
return true;
default:
return false;
}
}
@override
bool overwhelmsSums() {
switch (_type) {
case _SpecialType.neg_infinity:
case _SpecialType.pos_infinity:
case _SpecialType.indeterminate:
return true;
default:
return false;
}
}
@override
Term operator -() {
switch (_type) {
case _SpecialType.neg_infinity: return pos_infinity;
case _SpecialType.neg_overflow: return pos_overflow;
case _SpecialType.neg_underflow: return pos_underflow;
case _SpecialType.neg_vanishing: return pos_vanishing;
case _SpecialType.pos_vanishing: return neg_vanishing;
case _SpecialType.pos_underflow: return neg_underflow;
case _SpecialType.pos_overflow: return neg_overflow;
case _SpecialType.pos_infinity: return neg_infinity;
default: break;
}
return indeterminate;
}
_Special reciprocal() {
switch (_type) {
case _SpecialType.neg_infinity: return neg_vanishing;
case _SpecialType.neg_overflow: return neg_underflow;
case _SpecialType.neg_underflow: return neg_overflow;
case _SpecialType.neg_vanishing: return neg_infinity;
case _SpecialType.pos_vanishing: return pos_infinity;
case _SpecialType.pos_underflow: return pos_overflow;
case _SpecialType.pos_overflow: return pos_underflow;
case _SpecialType.pos_infinity: return pos_vanishing;
default: break;
}
return indeterminate;
}
@override
Term operator +(Term other) {
if (other is! Constant) {
return super + other;
}
if (other == indeterminate) return other;
switch (_type) {
case _SpecialType.neg_infinity:
if (other == pos_infinity) return indeterminate;
return this;
case _SpecialType.neg_overflow:
if (other == neg_infinity) return other;
if (other == pos_infinity) return other;
if (other == pos_overflow) return indeterminate;
return this;
case _SpecialType.neg_underflow:
if (other == neg_underflow) return this;
if (other == neg_vanishing) return this;
if (other == zero) return this;
if (other == pos_vanishing) return this;
if (other == pos_underflow) return indeterminate;
return other;
case _SpecialType.neg_vanishing:
if (other == neg_vanishing) return this;
if (other == zero) return this;
if (other == pos_vanishing) return indeterminate;
return other;
case _SpecialType.pos_vanishing:
if (other == pos_vanishing) return this;
if (other == zero) return this;
if (other == neg_vanishing) return indeterminate;
return other;
case _SpecialType.pos_underflow:
if (other == pos_underflow) return this;
if (other == pos_vanishing) return this;
if (other == zero) return this;
if (other == neg_vanishing) return this;
if (other == neg_underflow) return indeterminate;
return other;
case _SpecialType.pos_overflow:
if (other == pos_infinity) return other;
if (other == neg_infinity) return other;
if (other == neg_overflow) return indeterminate;
return this;
case _SpecialType.pos_infinity:
if (other == neg_infinity) return indeterminate;
return this;
default:
return this;
}
}
@override
Term operator -(Term other) {
if (other is! Constant) {
return super - other;
}
if (other == indeterminate) return other;
return this + -other;
}
@override
Term operator *(Term other) {
if (other is! Constant) {
return super * other;
}
if (other == indeterminate) return other;
if (other == zero) return other;
switch (_type) {
case _SpecialType.neg_infinity:
if (other == pos_vanishing) return indeterminate;
if (other == neg_vanishing) return indeterminate;
if (other.isNegative()) return pos_infinity;
return this;
case _SpecialType.neg_overflow:
if (other == neg_infinity) return pos_infinity;
if (other == neg_underflow) return indeterminate;
if (other == neg_vanishing) return pos_vanishing;
if (other == pos_vanishing) return neg_vanishing;
if (other == pos_underflow) return indeterminate;
if (other == pos_infinity) return neg_infinity;
if (other.isNegative()) return pos_overflow;
return this;
case _SpecialType.neg_underflow:
if (other == neg_infinity) return pos_infinity;
if (other == neg_overflow) return indeterminate;
if (other == neg_vanishing) return pos_vanishing;
if (other == pos_vanishing) return neg_vanishing;
if (other == pos_overflow) return indeterminate;
if (other == pos_infinity) return neg_infinity;
if (other.isNegative()) return pos_underflow;
return this;
case _SpecialType.neg_vanishing:
if (other == neg_infinity) return indeterminate;
if (other == pos_infinity) return indeterminate;
if (other.isNegative()) return pos_vanishing;
return this;
case _SpecialType.pos_vanishing:
if (other == neg_infinity) return indeterminate;
if (other == pos_infinity) return indeterminate;
if (other.isNegative()) return neg_vanishing;
return this;
case _SpecialType.pos_underflow:
if (other == neg_infinity) return neg_infinity;
if (other == neg_overflow) return indeterminate;
if (other == neg_vanishing) return neg_vanishing;
if (other == pos_vanishing) return pos_vanishing;
if (other == pos_overflow) return indeterminate;
if (other == pos_infinity) return pos_infinity;
if (other.isNegative()) return neg_underflow;
return this;
case _SpecialType.pos_overflow:
if (other == neg_infinity) return neg_infinity;
if (other == neg_underflow) return indeterminate;
if (other == neg_vanishing) return neg_vanishing;
if (other == pos_vanishing) return pos_vanishing;
if (other == pos_underflow) return indeterminate;
if (other == pos_infinity) return pos_infinity;
if (other.isNegative()) return neg_overflow;
return this;
case _SpecialType.pos_infinity:
if (other == pos_vanishing) return indeterminate;
if (other == neg_vanishing) return indeterminate;
if (other.isNegative()) return neg_infinity;
return this;
default:
return this;
}
}
@override
Term operator /(Term other) {
if (other is! Constant) {
return super * other;
}
if (other == indeterminate) return other;
if (other == zero) {
if (this == indeterminate) return this;
if (this.isNegative()) return neg_infinity;
return pos_infinity;
}
switch (_type) {
case _SpecialType.neg_infinity:
if (other == neg_infinity) return indeterminate;
if (other == pos_infinity) return indeterminate;
if (other.isNegative()) return pos_infinity;
return this;
case _SpecialType.neg_overflow:
if (other == neg_infinity) return pos_vanishing;
if (other == neg_overflow) return indeterminate;
if (other == neg_vanishing) return pos_infinity;
if (other == pos_vanishing) return neg_infinity;
if (other == pos_overflow) return indeterminate;
if (other == pos_infinity) return neg_vanishing;
if (other.isNegative()) return pos_overflow;
return this;
case _SpecialType.neg_underflow:
if (other == neg_infinity) return pos_vanishing;
if (other == neg_underflow) return indeterminate;
if (other == neg_vanishing) return pos_infinity;
if (other == pos_vanishing) return neg_infinity;
if (other == pos_underflow) return indeterminate;
if (other == pos_infinity) return neg_vanishing;
if (other.isNegative()) return pos_overflow;
return this;
case _SpecialType.neg_vanishing:
if (other == neg_vanishing) return indeterminate;
if (other == pos_vanishing) return indeterminate;
if (other.isNegative()) return pos_vanishing;
return this;
case _SpecialType.pos_vanishing:
if (other == neg_vanishing) return indeterminate;
if (other == pos_vanishing) return indeterminate;
if (other.isNegative()) return neg_vanishing;
return this;
case _SpecialType.pos_underflow:
if (other == neg_infinity) return neg_vanishing;
if (other == neg_underflow) return indeterminate;
if (other == neg_vanishing) return neg_infinity;
if (other == pos_vanishing) return pos_infinity;
if (other == pos_underflow) return indeterminate;
if (other == pos_infinity) return pos_vanishing;
if (other.isNegative()) return neg_overflow;
return this;
case _SpecialType.pos_overflow:
if (other == neg_infinity) return neg_vanishing;
if (other == neg_overflow) return indeterminate;
if (other == neg_vanishing) return neg_infinity;
if (other == pos_vanishing) return pos_infinity;
if (other == pos_overflow) return indeterminate;
if (other == pos_infinity) return pos_vanishing;
if (other.isNegative()) return neg_overflow;
return this;
case _SpecialType.pos_infinity:
if (other == neg_infinity) return indeterminate;
if (other == pos_infinity) return indeterminate;
if (other.isNegative()) return neg_infinity;
return this;
default:
return this;
}
}
@override bool _equals(Constant other) =>
this != indeterminate && this == other;
@override
int compareTo(Constant other) {
if (other is _Special) {
return _type.index - other._type.index;
}
int index;
if (other.isNegative()) {
index = _SpecialType.neg_overflow.index;
} else if (other == zero) {
index = _SpecialType.neg_vanishing.index;
} else {
index = _SpecialType.pos_underflow.index;
}
return index <= _type.index ? -1 : 1;
}
@override String toString() => _name;
}
class _Number extends Constant {
final double _value;
const _Number(this._value) : super._();
@override bool isNegative() => (_value < 0.0);
@override bool overwhelmsProducts() => _value == 0.0;
@override bool overwhelmsSums() => false;
@override
Term operator -() {
if (_value == -1.0) return one;
if (_value == 1.0) return neg_one;
if (_value == 0.0) return zero;
return _Number(-this._value);
}
Term operator +(Term other) {
if (other is _Special) {
return other + this;
}
if (other is _Number) {
return Constant._forDouble(_value + other._value);
}
return super + other;
}
Term operator -(Term other) {
if (other is _Special) {
return -other + this;
}
if (other is _Number) {
return Constant._forDouble(_value - other._value);
}
return super - other;
}
Term operator *(Term other) {
if (this == zero) return this;
if (other is _Special) {
return other * this;
}
if (other is _Number) {
return Constant._forDouble(_value * other._value);
}
return super * other;
}
Term operator /(Term other) {
if (this == zero) return other == zero ? indeterminate : this;
if (other is _Special) {
return other.reciprocal() * this;
}
if (other is _Number) {
return Constant._forDouble(_value / other._value);
}
return super * other;
}
@override
int compareTo(Constant other) {
if (other is _Number) {
if (_value < other._value) return -1;
if (_value > other._value) return 1;
return 0;
}
return -other.compareTo(this);
}
@override bool _equals(Constant other) =>
other is _Number && _value == other._value;
String toString() {
if (_value.isFinite && _value == _value.toInt()) {
return _value.toInt().toString();
}
return _value.toString();
}
}
const indeterminate = _Special(_SpecialType.indeterminate, 'indeterminate');
const neg_infinity = _Special(_SpecialType.neg_infinity, '-Infinity');
const neg_overflow = _Special(_SpecialType.neg_overflow, '-overflow');
const neg_underflow = _Special(_SpecialType.neg_underflow, '-underflow');
const neg_vanishing = _Special(_SpecialType.neg_vanishing, '-vanishing');
const pos_vanishing = _Special(_SpecialType.pos_vanishing, '+vanishing');
const pos_underflow = _Special(_SpecialType.pos_underflow, '+underflow');
const pos_overflow = _Special(_SpecialType.pos_overflow, '+overflow');
const pos_infinity = _Special(_SpecialType.pos_infinity, '+Infinity');
const neg_one = _Number(-1.0);
const zero = _Number(0.0);
const one = _Number(1.0);