-
Notifications
You must be signed in to change notification settings - Fork 11
/
unum.py
1581 lines (1443 loc) · 56 KB
/
unum.py
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Copyright (c) Taylor & Francis Group, 2014.
Copyright (c) Jeff Muizelaar, 2015.
Permission is hereby granted, free of charge, to any person obtaining \
a copy of this software and associated documentation files (the \
"Software"), to deal in the Software without restriction, including \
without limitation the rights to use, copy, modify, merge, publish, \
distribute, sublicense, and/or sell copies of the Software, and to \
permit persons to whom the Software is furnished to do so, subject to \
the following conditions:
The above copyright notice and this permission notice shall be \
included in all copies or substantial portions of the software.
THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, \
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF \
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND \
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS \
BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER LIABILITY, WHETHER IN AN \
ACTION OR CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN \
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE \
SOFTWARE.
"""
from __future__ import division
import math
open = True
closed = False
from fractions import *
import unum_config
e = unum_config.e
f = unum_config.f
esizesize = e
fsizesize = f
esizemax = 2**esizesize
fsizemax = 2**fsizesize
utagsize = 1 + f + e
maxubits = 1 + esizemax + fsizemax + utagsize
ubitmask = 1 << (utagsize - 1)
fsizemask = (1 << f) - 1
esizemask = (ubitmask - 1) - fsizemask
efsizemask = esizemask | fsizemask
utagmask = ubitmask | efsizemask
ulpu = 1 << utagsize
smallsubnormalu = efsizemask + ulpu
smallnormalu = efsizemask + (1 << (maxubits - 1 - esizemax))
signbigu = 1 << maxubits - 1
posinfu = signbigu - 1 - ubitmask
maxrealu = posinfu - ulpu
minrealu = maxrealu + signbigu
neginfu = posinfu + signbigu
negbigu = neginfu - ulpu
qNaNu = posinfu + ubitmask
sNaNu = neginfu + ubitmask
NaN = float("nan")
if utagsize == 1:
negopeninfu = 0xd # 1101
else:
negopeninfu = 0xf << (utagsize - 1)
if utagsize == 1:
posopeninfu = 0x5 # 0101
else:
posopeninfu = 0x7 << (utagsize - 1)
negopenzerou = 0x9 << (utagsize - 1)
maxreal = Fraction(2**(2**(esizemax-1)) * (2**fsizemax - 1), (2**(fsizemax-1)))
smallsubnormal = Fraction(2**2, 2**(2**(esizemax-1) + fsizemax))
def fractionalPart(x):
return x - math.trunc(x)
def integerPart(x):
return math.trunc(x)
def flatten(container):
for i in container:
if isinstance(i, list) or isinstance(i, tuple):
for j in flatten(i):
yield j
else:
yield i
def transpose(l):
return tuple([tuple(i) for i in zip(*l)])
# XXX: this is a bad implementation, but at least it avoids division
def binomial(n, k):
if k == 0:
return 1
if n == k:
return 1
return binomial(n-1, k-1) + binomial(n-1, k)
def log(x, base):
return math.log(x, base)
def floor(x):
if math.isinf(x):
return x
return int(math.floor(x))
def ceil(x):
if math.isinf(x):
return x
return int(math.ceil(x))
def unumQ(x):
if isinstance(x, (int, long)):
if x >= 0 and x <= sNaNu:
return True
return False
def uQ(x):
return unumQ(x) or uboundQ(x)
def f2g(x):
if floatQ(x):
if math.isnan(x):
return ((NaN, NaN), (open, open))
else:
return ((x, x), (closed, closed))
def unum2g(u):
if unumQ(u):
if u == qNaNu or u == sNaNu:
return ((NaN, NaN), (open, open))
x = u2f(exact(u))
y = u2f(exact(u) + ulpu)
if exQ(u):
return ((x, x), (closed, closed))
elif u == (bigu(u) + ubitmask):
return ((big(u), float('inf')), (open, open))
elif u == signmask(u) + bigu(u) + ubitmask:
return ((float('-inf'), -big(u)), (open, open))
elif sign(u) == 1:
return ((y, x), (open, open))
else:
return ((x, y), (open, open))
def floatQ(x):
if math.isnan(x) or math.isinf(x) or isinstance(x, (Fraction, int, long, float)):
return True
return False
def esizeminus1(u):
if unumQ(u):
return (u & esizemask) >> fsizesize
def esize(u):
if unumQ(u):
return 1 + esizeminus1(u)
def fsizeminus1(u):
if unumQ(u):
return u & fsizemask
def fsize(u):
if unumQ(u):
return 1 + fsizeminus1(u)
def numbits(u):
if unumQ(u):
return 1 + esize(u) + fsize(u) + utagsize
def signmask(u):
if unumQ(u):
return 1 << (numbits(u) - 1)
def hiddenmask(u):
if unumQ(u):
return 1 << (fsize(u) + utagsize)
def fracmask(u):
if unumQ(u):
return ((1 << fsize(u)) - 1) << utagsize
def expomask(u):
if unumQ(u):
return ((1 << esize(u)) - 1) << (fsize(u) + utagsize)
def floatmask(u):
if unumQ(u):
return signmask(u) + expomask(u) + fracmask(u)
def bias(u):
if unumQ(u):
return 2**(esizeminus1(u)) - 1
def sign(u):
if unumQ(u):
return (u & signmask(u) > 0)
def expo(u):
if unumQ(u):
return (u & expomask(u)) >> (utagsize + fsize(u))
def hidden(u):
if unumQ(u):
return expo(u) > 0
def frac(u):
if unumQ(u):
return (u & fracmask(u)) >> utagsize
def inexQ(u):
if unumQ(u):
return (u & ubitmask) > 0
def exQ(u):
if unumQ(u):
return (u & ubitmask) == 0
def exact(u):
if unumQ(u):
if inexQ(u):
return u ^ ubitmask
else:
return u
def expovalue(u):
if unumQ(u):
return expo(u) - bias(u) + 1 - hidden(u)
def u2f(u):
if unumQ(u) and exQ(u):
if u == posinfu:
return float("inf")
if u == neginfu:
return -float("inf")
k = ((-1)**sign(u))*(Fraction(2,1)**expovalue(u))*(hidden(u) + Fraction(frac(u), 2**fsize(u)))
assert not isinstance(k, float)
return k
# Biggest unum possible with identical utag contents.
def bigu(u):
if unumQ(u):
return expomask(u) + fracmask(u) + (efsizemask & u) - ulpu * ((u & efsizemask) == efsizemask)
# Biggest numerical value representable with identical utag contents.
def big(u):
if unumQ(u):
return u2f(bigu(u))
def gQ(x):
if isinstance(x, tuple):
if len(x) == 2:
if isinstance(x[0], tuple):
if len(x[0]) == 2:
if isinstance(x[1], tuple):
if len(x[1]) == 2:
if isinstance(x[1][0], bool) and isinstance(x[1][1], bool) and floatQ(x[0][0]) and floatQ(x[0][1]):
if math.isnan(x[0][0]) or math.isnan(x[0][1]):
return True
if (x[0][0] == x[0][1] and not x[1][0] and not x[1][1]) or x[0][0] < x[0][1]:
return True
return False
def uboundQ(x):
if isinstance(x, tuple):
if len(x) == 1 or len(x) == 2:
xL = x[0]
xR = x[-1]
if unumQ(xL) and unumQ(xR):
gL, gR = (unum2g(xL), unum2g(xR))
if len(x) == 1 or (xL == qNaNu or xL == sNaNu or xR == qNaNu or xR == sNaNu) or \
((gL[0][0] < gR[0][1]) or (gL[0][0] == gR[0][1] and (exQ(xL) and exQ(xR)))):
return True
return False
# should this look at negative inf?
def scale(x):
if floatQ(x) and x != float("inf") and not math.isnan(x):
if x == 0:
return 0
else:
return floor(log(abs(x),2))
def ne(x):
if floatQ(x) and x != float("inf") and not math.isnan(x):
if x == 0 and scale(x) == 1:
return 1
else:
return int(math.ceil(log(1+abs(scale(x)-1),2))) + 1
def nbits(u):
if uQ(u):
if unumQ(u):
ub = (u,)
else:
ub = u
if len(ub) == 2:
return 1 + numbits(ub[0]) + numbits(ub[1])
else:
return 1 + numbits(ub[0])
def ubound2g(ub):
if uboundQ(ub):
uL = ub[0]
uR = ub[-1]
if uL == qNaNu or uL == sNaNu or uR == qNaNu or uR == sNaNu:
return ((float("nan"), float("nan")), (open, open))
gL, gR = (unum2g(uL), unum2g(uR))
return ((gL[0][0], gR[0][1]), (gL[1][0], gR[1][1]))
def u2g(u):
if uQ(u):
if unumQ(u):
return unum2g(u)
else:
return ubound2g(u)
#raise TypeError(u)
def x2u(x):
if floatQ(x):
# Exceptional nonnumeric values:
if math.isnan(x):
return qNaNu
elif x == float("inf"):
return posinfu
elif x == float("-inf"):
return neginfu
# Magnitudes too large to represent:
elif abs(x) > maxreal:
return maxrealu + ubitmask + (signbigu if x < 0 else 0)
# Zero is a special case. The smallest unum for it is just 0:
elif x == 0:
return 0
# Magnitudes too small to represent become "inexact zero" with
# the maximum exponent and fraction field sizes:
elif abs(x) < smallsubnormal:
return utagmask + (signbigu if x < 0 else 0)
# For subnormal numbers, divide by the ULP value to get the
# fractional part. The while loop strips off the trailing bits.
elif abs(x) < u2f(smallnormalu):
y = abs(x) / smallsubnormal
y = (signbigu if x < 0 else 0) + efsizemask + (ubitmask if y != math.floor(y) else 0) + (floor(y) << utagsize)
# XXX is this right?
#assert(False)
while ((3 << (utagsize - 1)) & y) == 0:
y = (y - (efsizemask & y))//2 + (efsizemask & y) - 1
return y
# All remaining cases are in the normalized range.
else:
n = 0
y = abs(x) / 2**(scale(x))
n = 0
while math.floor(y) != y and n < fsizemax:
n += 1
y *= 2
if y == math.floor(y): # then the value is representable
# exactly. Fill in the fields from right to left:
# Size of fraction field,
# fits in the rightmost fsizesize bits...
y1 = n - (n > 0)
# Size of exponent field minus 1 fits in the esizesize bits...
y1 += ((ne(x) - 1) << fsizesize)
# Significant bits after hidden bit fits left of the unum tag bits...
y1 += 0 if n == 0 else ((floor(y) - 2**scale(y)) << utagsize)
# Value of exponent bits, adjusted for bias...
y1 += (scale(x) + 2**(ne(x) - 1) - 1) << (utagsize + n + (n == 0))
# If negative, add the sign bit
y1 += (1 << (utagsize + n + (n == 0) + ne(x))) if x < 0 else 0
# if a number is more concise as a subnormal, make it one
z = None
try:
z = log(1 - log(abs(x), 2), 2)
except:
pass
if z and z.is_integer() and z >= 0:
z = long(z)
return (z << fsizesize) + ulpu + (x < 0) * signmask(z << fsizesize)
else:
return y1
else:
# inexact. Use all available fraction bits
z = math.ceil(abs(x) / 2**(scale(x) - fsizemax)) * 2 ** (scale(x)-fsizemax)
n = max(ne(x), ne(z))
# All bits on for the fraction size, since we're using the maximum
y1 = fsizemask
# Store the exponent size minus 1 in the exponent size field
y1 += (n - 1) << fsizesize
# Back off by one ULP and make it inexact
y1 += ubitmask - ulpu
# Fraction bits are the ones to the left of the
# binary point after removing hidden bit and scaling
y1 += floor((z / 2**scale(z) - 1) * 2**fsizemax) << utagsize
# Exponent value goes in the exponent field
y1 += (scale(z) + 2**(n-1) - 1) << (utagsize + fsizemax)
if x < 0:
y1 += signmask(y1)
return y1
# View a float as a decimal, using as many digits as needed to be exact.
def autoN(x):
if math.isnan(x) or x == 0 or x == float('inf'):
return str(x)
if x < 0:
return "-" + autoN(-x)
y = log(x.denominator, 2)
if y == 0:
return str(x).zfill(1 + floor(log(x, 10)))
if isinstance(x, Fraction) and y == floor(y):
y = x - floor(x)
z = floor(log(y.denominator, 2))
return str(floor(x)) + "." + str(y*10**z).zfill(z)
return "?"
def view(g):
if gQ(g) or unumQ(g) or uboundQ(g):
((L, R), (LQ, RQ)) = g if gQ(g) else u2g(g)
if math.isnan(L) or math.isnan(R):
return "NaN"
if L == R and not LQ and not RQ:
return autoN(L)
if L < R:
return ("(" if LQ else "[") + autoN(L) + ", " + autoN(R) + (")" if RQ else "]")
return "NaN"
def plusg(x, y):
((xlo, xhi), (xlob, xhib)) = x
((ylo, yhi), (ylob, yhib)) = y
if math.isnan(xlo) or math.isnan(xhi) or math.isnan(ylo) or math.isnan(yhi):
return ((NaN, NaN), (open, open))
if xlo == float('-inf') and not xlob:
(sumleft, openleft) = (NaN, open) if ylo == float("inf") and not ylob else (float("-inf"), closed)
elif ylo == float('-inf') and not ylob:
(sumleft, openleft) = (NaN, open) if xlo == float("inf") and not xlob else (float("-inf"), closed)
elif (xlo == float('inf') and not xlob) or (ylo == float('inf') and not ylob):
(sumleft, openleft) = (float('inf'), closed)
elif xlo == float('-inf'):
(sumleft, openleft) = (float('inf'), closed) if ylo == float('inf') and not ylob else (float('-inf'), open)
elif ylo == float('-inf'):
(sumleft, openleft) = (float('inf'), closed) if xlo == float('inf') and not xlob else (float('-inf'), open)
else:
(sumleft, openleft) = ((xlo + ylo), (xlob or ylob))
if xhi == float('-inf') and not xhib:
(sumright, openright) = (NaN, open) if yhi == float("inf") and not yhib else (float("-inf"), closed)
elif yhi == float('-inf') and not yhib:
(sumright, openright) = (NaN, open) if xhi == float("inf") and not xhib else (float("-inf"), closed)
elif (xhi == float('inf') and not xhib) or (yhi == float('inf') and not yhib):
(sumright, openright) = (float('inf'), closed)
elif xhi == float('-inf'):
(sumright, openright) = (float('inf'), closed) if yhi == float('inf') and not yhib else (float('-inf'), open)
elif yhi == float('-inf'):
(sumright, openright) = (float('inf'), closed) if xhi == float('inf') and not xhib else (float('-inf'), open)
else:
(sumright, openright) = ((xhi + yhi), (xhib or yhib))
return ((sumleft, sumright), (openleft, openright))
def plusu(u, v):
if uQ(u) and uQ(v):
w = g2u(plusg(u2g(u), u2g(v)))
global ubitsmoved, numbersmoved
ubitsmoved += nbits(u) + nbits(v) + nbits(w)
numbersmoved += 3
return w
def negateg(x):
return ((-x[0][1], -x[0][0]), (x[1][1], x[1][0]))
def minusg(x, y):
return plusg(x, negateg(y))
def minusu(u, v):
w = g2u(minusg(u2g(u), u2g(v)))
global ubitsmoved, numbersmoved
ubitsmoved += nbits(u) + nbits(v) + nbits(w)
numbersmoved += 3
return w
def ubright(xright):
open = xright[1]
u = x2u(xright[0])
x = xright[0]
if x == float('inf'):
if open:
return posopeninfu
else:
return posinfu
if x == 0 and open:
return negopenzerou
if u2f(u) == x:
if open:
return (u - (ulpu * (x >= 0))) | ubitmask
else:
return u
else:
return u | (open * ubitmask)
# Find the left half of a ubound (numerical value and open-closed)
def ubleft(xleft):
open = xleft[1]
u = x2u(xleft[0])
x = xleft[0]
if x == float('-inf'):
if open:
return negopeninfu
else:
return neginfu
if u2f(u) == x:
if open:
return (u - (ulpu * (x < 0))) | ubitmask
else:
return u
else:
return u | (open * ubitmask)
def negateu(u):
if uQ(u):
if uboundQ(u):
if len(u) == 1:
return (x2u(0) if u2g(u[0]) == u2g(0) else signmask(u[0]) ^ u[0],)
else:
return (x2u(0) if u2g(u[1]) == u2g(0) else signmask(u[1]) ^ u[1],
x2u(0) if u2g(u[0]) == u2g(0) else signmask(u[0]) ^ u[0])
else:
return x2u(0) if u2g(u) == u2g(0) else signmask(u) ^ u
def unifypos(ub):
if uboundQ(ub):
u = ub[0]
v = ub[-1]
# First do trivial case where endpoints express the same value
if u2g(u) == u2g(v):
return g2u(u2g(u))
# Cannot unify if the interval includes exact 0, 1, 2, or 3
if nnequQ(ub, x2u(0)) or nnequQ(ub, x2u(1)) or nnequQ(ub, x2u(2)) or nnequQ(ub, x2u(3)):
return ub
# Refine the endpoints for the tightest possible unification.
u = promote(x2u(u2g(u)[0][0]), efsizemask)[0] + (ubitmask if inexQ(u) else -ubitmask)
v = promote(x2u(u2g(v)[0][1]), efsizemask)[0] - (ubitmask if inexQ(v) else -ubitmask)
if u == v:
return (u,)
# If upper bound is open inf and lower bound > maxreal, special handling is needed
if u2g(v)[0][1] == float("inf") and u2g(v)[1][1]:
if ltuQ((maxrealu,), (u,)):
return (maxrealu + ubitmask,)
# Defmote the left bound until the upper bound is open inf
while u2g(u)[0][1] < float('inf'):
if esize(u) > 1:
u = demotee(u)
else:
u = demotef(u)
return (u,)
# While demoting exponents is possible and still leaves unums within ubound, demote both exponents
while u != v and (((u2g(demotee(u))[0][0] < u2g(demotee(v))[0][0] and \
u2g(demotee(u))[0][1] < u2g(demotee(v))[0][1] < float('inf')))) and esize(u) > 1:
u = demotee(u)
v = demotee(v)
while u != v and frac(v) != frac(u) and fsize(u) > 1:
u = demotef(u)
v = demotef(v)
if u != v and ((floatmask(u) + ubitmask) | u) == ubitmask and ltuQ((v,), (x2u(1),)):
n = min(esizemax, floor(log(1 - log(u2g(v + ubitmask if exQ[v] else 0)[0,1], 2), 2)))
return (x2u(2**(-2**n+1)) - ubitmask,)
else:
if u == v:
return (u,)
else:
return ub
def unify(ub):
if uboundQ(ub):
u = ub[0]
v = ub[-1]
if u == qNaNu or u == sNaNu or v == qNaNu or v == sNaNu:
return (qNaNu,)
if u == posinfu and v == posinfu:
return (posinfu,)
if u == neginfu and v == neginfu:
return (neginfu,)
if u == neginfu or u == posinfu or v == neginfu or v == posinfu or \
ltuQ((u,), (x2u(0),)) and not ltuQ((v,), (x2u(0),)):
return ub
if ltuQ((u,), (x2u(0),)) and ltuQ((v,), (x2u(0),)):
return negateu(unifypos(negateu(ub)))
if u2g(u) == u2g(v):
return (min(u, v),)
return unifypos(ub)
def g2u(g):
if gQ(g):
ulo = x2u(g[0][0])
uhi = x2u(g[0][1])
blo = g[1][0]
bhi = g[1][1]
# XXX why not use the named version from above?
if ulo == qNaNu or uhi == qNaNu or g[0][0] > g[0][1] or (g[0][0] == g[0][1] and (blo or bhi)):
return qNaNu
elif ulo == uhi and not (blo != bhi):
return x2u(g[0][0])
else:
u1 = ubleft((g[0][0], blo))
u2 = ubright((g[0][1], bhi))
if u2g(unify((u1, u2))) == u2g((u1, u2)):
return unify((u1, u2))
else:
return (u1, u2)
#raise TypeError(g)
# Test if interval g is strictly less than interval h.
def ltgQ(g, h):
if gQ(g) and gQ(h):
if math.isnan(g[0][0]) or math.isnan(g[0][1]) or math.isnan(h[0][0]) or math.isnan(h[0][1]):
return False
return g[0][1] < h[0][0] or (g[0][1] == h[0][0] and (g[1][1] or h[1][0]))
# Test if ubound or unum u is strictly less than ubound or unum v.
def ltuQ(u, v):
if uQ(u) and uQ(v):
return ltgQ(u2g(u), u2g(v))
# Test if interval g is strictly greater than interval h.
def gtgQ(g, h):
if gQ(g) and gQ(h):
if math.isnan(g[0][0]) or math.isnan(g[0][1]) or math.isnan(h[0][0]) or math.isnan(h[0][1]):
return False
return g[0][0] > h[0][1] or (g[0][0] == h[0][1] and (g[1][0] or h[1][1]))
def gtuQ(u, v):
if uQ(u) and uQ(v):
return gtgQ(u2g(u), u2g(v))
# Test if interval g is not nowhere equal to inveral h
def nneqgQ(g, h):
if gQ(g) and gQ(h):
if math.isnan(g[0][0]) or math.isnan(g[0][1]) or math.isnan(h[0][0]) or math.isnan(h[0][1]):
return False
return not (ltgQ(g, h) or gtgQ(g, h))
# Test if ubound or unum u is not nowhere equal to ubound or unum v
def nnequQ(u, v):
if uQ(u) and uQ(v):
return nneqgQ(u2g(u), u2g(v))
# Test if interval g is identical to interval h
def samegQ(g, h):
if gQ(g) and gQ(h):
return g == h
# Test if ubound or unum u value is identical to ubound or unum v value.
def sameuQ(u, v):
if uQ(u) and uQ(v):
return samegQ(u2g(u), u2g(v))
def intersectg(g, h):
k = intersectgi(g,h)
#print k
return k
def intersectgi(g, h):
#print 'intersect', g,h
if gQ(g) and gQ(h):
glo, ghi = g[0]
glob, ghib = g[1]
hlo, hhi = h[0]
hlob, hhib = h[1]
if math.isnan(glo) or math.isnan(ghi) or math.isnan(hlo) or math.isnan(hhi):
return ((float("nan"), float("nan")), (open, open))
if glo < hlo or (glo == hlo and hlob):
# left end of g is left of end of h. Three sub-cases to test.
if ghi < hlo or (ghi == hlo and (ghib or hlob)):
return ((float("nan"), float("nan")), (open, open))
# g is completely left of h.
if ghi < hhi or (ghi == hhi and (ghib or not hhib)):
return ((hlo, ghi), (hlob, ghib))
# right part of g overlaps left part of h.
return ((hlo, hhi), (hlob, hhib)) # h is entire inside g.
if glo < hhi or (glo == hhi and (not glob and not hhib)):
# left end of g is inside h. Two sub-cases to test.
if ghi < hhi or (ghi == hhi and (ghib or not hhib)):
# g is entirely inside h.
return ((glo, ghi), (glob, ghib))
# left end of g overlaps right end of h.
return ((glo, hhi), (glob, hhib))
# g is entirely to the right of h.
return ((NaN, NaN), (open, open))
# Add a zero bit to the fraction length of an exact unum, if possible.
def promotef(u):
if unumQ(u) and exQ(u):
if fsize(u) < fsizemax:
return 2 * (floatmask(u) & u) + (utagmask & u) + 1
return u
# Increase the length of the exponent field of an exact unum, if possible.
def promotee(u):
if unumQ(u) and exQ(u):
e = expo(u)
es = esize(u)
f = frac(u)
fs = fsize(u)
s = signmask(u) & u
ut = (utagmask & u) + fsizemax
# If already maximum exponent size, do nothing. This also handles NaN and inf values
if es == esizemax:
return u
# Take care of u = 0 case, ignoring the sign bit. It's simply the new utag.
if e == 0 and f == 0:
return ut
# If normal (nonzero exponent), slide sign bit left, add 2**(es-1), increment esize.
if e > 0:
return 2 * s + (e + 2**(es-1)) * hiddenmask(u) + ((hiddenmask(u) - 1) & u) + fsizemax
# Subnormal. Room to shift and stay subnormal?
if fs - (floor(log(f, 2)) + 1) >= 2**(es-1):
return 2 * s + frac(u) * (2**(2**(es-1))) * ulpu + ut
# Subnormal becomes normal. Trickiest case.
# The fraction slides left such that the lefmost 1 becomes the hidden bit
nsigbits = floor(log(f,2)) + 1
return 2*s + (2**(es-1) + 1 - fs + nsigbits)*(hiddenmask(u)) + \
(f - 2**nsigbits)*2**(fs-nsigbits+1)*ulpu + (utagmask & u) + fsizemask
# Promote a pair of exact unums to the same esize and fsize
def promote(u, v):
if unumQ(u) and unumQ(v) and exQ(u) and exQ(v):
eu = esize(u)
ev = esize(v)
fu = fsize(u)
fv = fsize(v)
ut = u
vt = v
while eu < ev:
ut = promotee(ut)
eu += 1
while ev < eu:
vt = promotee(vt)
ev += 1
while fu < fv:
ut = promotef(ut)
fu += 1
while fv < fu:
vt = promotef(vt)
fv += 1
return (ut, vt)
# Demote the fraction of a unum if possible,
# even if it makes it inexact.
def demotef(u):
if unumQ(u):
# Cannot make the fraction any smaller
if fsize(u) == 1 or u == posinfu or u == neginfu or u == qNaNu or u == sNaNu:
return u
# Else shift fraction right one bit.
return ((u & floatmask(u)) // 2) | ((utagmask & u) - 1)
def demotee(u):
if unumQ(u):
es = esize(u)
mask = int(signmask(u) / 4)
fm = floatmask(u)
ut = u & utagmask
s = signmask(u) & u
f = frac(u)
left2 = Fraction((u & (3*mask)), mask)
# Cannot make the exponent any smaller:
if es == 1 or u == posinfu or u == neginfu or u == qNaNu or u == sNaNu:
return u
if expo(u) == 0:
f = Fraction(f, (2**(2**(es-2))))
ibit = ubitmask if fractionalPart(f) > 0 else 0
# XXX: is the division ok here?
return ibit | (s // 2 + integerPart(f) * ulpu + ut - fsizemax)
# If the left two exponent bits are 00
# (but it's normal, since we fell through the previous test),
# result switches to subnormal. The exponent after the first
# two bits joins the fraction like a fixed-point number,
# before shifting the fraction to the right. The
# new exponent is zero, of course.
if left2 == 0:
f = Fraction(2**fsize(u) + f, Fraction(2**(2**(es-2)+1), 2**expo(u)))
ibit = ubitmask if fractionalPart(f) > 0 else 0
assert unumQ( ibit | (s // 2 + integerPart(f) * ulpu + ut - fsizemax))
return ibit | (s // 2 + integerPart(f) * ulpu + ut - fsizemax)
# If the left two exponent bits are 01 or 10,
# squeeze out the second bit; if that leaves a subnormal exponent,
# shift the hidden bit and fraction bits right
if left2 <= 2:
e = int(((expomask(u) - 3 * mask) & u) + (u & (2*mask)) / 2)
if e == 0:
f = Fraction(2**fsize(u) + f, 2)
ibit = ubitmask if fractionalPart(f) > 0 else 0
assert unumQ(ibit | (int(s / 2) + e + integerPart(f) * ulpu + ut - fsizemax))
return ibit | (int(s / 2) + e + integerPart(f) * ulpu + ut - fsizemax)
# If the first two exponent bits are 11,
# always get an unbounded unum, all 1s for fraction:
assert unumQ( int(((u & signmask(u)) + (fm - signmask(u))) / 2) | ut - fsizemax)
return int(((u & signmask(u)) + (fm - signmask(u))) / 2) | ut - fsizemax
raise TypeError(u)
def timesposleft(x, y):
xb = x[1]
yb = y[1]
x = x[0]
y = y[0]
if (x, xb) == (0, closed):
if (y, yb) == (float('inf'), closed):
return (float('nan'), open)
else:
return (0, closed)
if (y, yb) == (0, closed):
if (x, xb) == (float('inf'), closed):
return (float('nan'), open)
else:
return (0, closed)
if (x, xb) == (0, open):
if (y, yb) == (float('inf'), closed):
return (float('inf'), closed)
else:
return (0, open)
if (y, yb) == (0, open):
if (x, xb) == (float('inf'), closed):
return (float('inf'), closed)
else:
return (0, open)
if (x, xb) == (float('inf'), closed) or (y, yb) == (float('inf'), closed):
return (float('inf'), closed)
return (x*y, xb or yb)
def timesposright(x, y):
xb = x[1]
yb = y[1]
x = x[0]
y = y[0]
if (x, xb) == (float('inf'), closed):
if (y, yb) == (0, closed):
return (float('nan'), open)
else:
return (float('inf'), closed)
if (y, yb) == (float('inf'), closed):
if (x, xb) == (0, closed):
return (float('nan'), open)
else:
return (float('inf'), closed)
if (x, xb) == (float('inf'), open):
if (y, yb) == (0, closed):
return (0, closed)
else:
return (float('inf'), open)
if (y, yb) == (float('inf'), open):
if (x, xb) == (0, closed):
return (0, closed)
else:
return (float('inf'), open)
if (x, xb) == (0, closed) or (y, yb) == (0, closed):
return (0, closed)
return (x*y, xb or yb)
def neg(x):
return (-x[0], x[1])
def unionfix(end1, end2):
return sorted(set(list(end1) + list(end2)))
def timesg(x, y):
xlo, xhi = x[0]
xlob, xhib = x[1]
ylo, yhi = y[0]
ylob, yhib = y[1]
lcan = []
rcan = []
# If any value is NaN, the result is also NaN.
if math.isnan(xlo) or math.isnan(xhi) or math.isnan(ylo) or math.isnan(yhi):
return ((float('nan'), float('nan')), (open, open))
# Lower left corner is in upper right quadrant, facing uphill:
if xlo >= 0 and ylo >= 0:
lcan = unionfix(lcan, (timesposleft((xlo, xlob), (ylo, ylob)),))
# Upper right corner is in lower left quadrant, facing uphill:
if (xhi < 0 or (xhi == 0 and xhib)) and (yhi < 0 or (yhi == 0 and yhib)):
lcan = unionfix(lcan, (timesposleft((-xhi, xhib), (-yhi, yhib)),))
# Upper left corner is in upper left quadrant, facing uphill:
if (xlo < 0 or (xlo == 0 and not xlob)) and (yhi > 0 or (yhi == 0 and not yhib)):
lcan = unionfix(lcan, (neg(timesposright((-xlo, xlob), (yhi, yhib))), ))
# Lower right corner is in lower right quadrant, facing uphill:
if (xhi > 0 or (xhi == 0 and not xhib)) and (ylo < 0 or (ylo == 0 and not ylob)):
lcan = unionfix(lcan, (neg(timesposright((xhi, xhib), (-ylo, ylob))),))
# Upper right corner is in upper right quadrant, facing downhill:
if (xhi > 0 or (xhi == 0 and not xhib)) and (yhi > 0 or (yhi == 0 and not yhib)):
rcan = unionfix(rcan, (timesposright((xhi, xhib), (yhi, yhib)),))
# Lower left corner is in lower left quadrant, facing downhill:
if (xlo < 0 or (xlo == 0 and not xlob)) and (ylo < 0 or (ylo == 0 and not ylob)):
rcan = unionfix(rcan, (timesposright((-xlo, xlob), (-ylo, ylob)),))
# Lower right corner is in upper left quadrant, facing downhill:
if (xhi < 0 or (xhi == 0 and xhib)) and ylo >= 0:
rcan = unionfix(rcan, (neg(timesposright((-xhi, xhib), (ylo, ylob))),))
# Upper left corner is in lower right quadrant, facing downhill:
if xlo >= 0 and (yhi < 0 or (yhi == 0 and yhib)):
rcan = unionfix(rcan, (neg(timesposright((xlo, xlob), (-yhi, yhib))),))
if any(isinstance(can, float) and math.isnan(can) for can in flatten(lcan)) or \
any(isinstance(can, float) and math.isnan(can) for can in flatten(rcan)):
(timesleft, timesright) = (float("nan"), float("nan"))
(openleft, openright) = (open, open)
(timesleft, timesright) = (lcan[0][0], rcan[-1][0])
(openleft, openright) = (lcan[0][1], rcan[-1][1])
if len(lcan) > 1:
if lcan[0][0] == lcan[1][0] and (not lcan[0][1] or not lcan[1][1]):
openleft = closed
if len(rcan) > 1:
if rcan[-1][0] == rcan[-2][0] and (not rcan[-1][1] or not rcan[-2][1]):
openright = closed
return ((timesleft, timesright), (openleft, openright))
def timesu(u, v):
if uQ(u) and uQ(v):
w = g2u(timesg(u2g(u), u2g(v)))
global ubitsmoved, numbersmoved
ubitsmoved += nbits(u) + nbits(v) + nbits(w)
numbersmoved += 3
return w
def divideposleft(x, y):
xb = x[1]
yb = y[1]
x = x[0]
y = y[0]
if (y, yb) == (0, closed):
return (float('nan'), open)
if (x, xb) == (float('inf'), closed):
if (y, yb) == (float('inf'), closed):
return (float('nan'), open)
else:
return (float('inf'), closed)
if (x, xb) == (0, closed) or (y, yb) == (float('inf'), closed):
return (0, closed)
if (x, xb) == (float('inf'), open) or (y, yb) == (0, open):
return (float('inf'), open)
return (x/y, xb or yb)
def divideposright(x, y):
xb = x[1]
yb = y[1]
x = x[0]
y = y[0]
if (y, yb) == (0, closed):
return (float('nan'), open)
if (x, xb) == (float('inf'), closed):
if (y, yb) == (float('inf'), closed):
return (float('nan'), open)
else:
return (float('inf'), closed)
if (x, xb) == (0, closed) or (y, yb) == (float('inf'), closed):
return (0, closed)
if (x, xb) == (float('inf'), open) or (y, yb) == (0, open):
return (float('inf'), open)
return (x/y, xb or yb)
def divideg(x, y):
xlo, xhi = x[0]
xlob, xhib = x[1]
ylo, yhi = y[0]
ylob, yhib = y[1]
lcan = []
rcan = []
# If any value is NaN, the result is also NaN.
if math.isnan(xlo) or math.isnan(xhi) or math.isnan(ylo) or math.isnan(yhi) \
or ((ylo < 0 or (ylo == 0 and not ylob)) and (yhi > 0 or (yhi == 0 and not yhib))):
return ((float('nan'), float('nan')), (open, open))
# Upper left corner is in upper right quadrant, facing uphill:
if xlo >= 0 and (yhi > 0 or (yhi == 0 and not yhib)):
lcan = unionfix(lcan, (divideposleft((xlo, xlob), (yhi, yhib)),))
# Lower right corner is in lower left quadrant, facing uphill:
if (xhi < 0 or (xhi == 0 and xhib)) and (ylo < 0 or (ylo == 0 and not ylob)):
lcan = unionfix(lcan, (divideposleft((-xhi, xhib), (-ylo, ylob)),))
# Lower left corner is in upper left quadrant, facing uphill:
if (xlo < 0 or (xlo == 0 and not xlob)) and ylo >= 0:
lcan = unionfix(lcan, (neg(divideposright((-xlo, xlob), (ylo, ylob))),))
# Upper right corner is in lower right quadrant, facing uphill:
if (xhi > 0 or (xhi == 0 and not xhib)) and (yhi < 0 or (yhi == 0 and yhib)):
lcan = unionfix(lcan, (neg(divideposright((xhi, xhib), (-yhi, yhib))),))
# Lower right corner is in upper right quadrant, facing downhill:
if (xhi > 0 or (xhi == 0 and not xhib)) and ylo >= 0:
rcan = unionfix(rcan, (divideposright((xhi, xhib), (ylo, ylob)),))
# Upper left corner is in lower left quadrant, facing downhill:
if (xlo < 0 or (xlo == 0 and not xlob)) and (yhi < 0 or (yhi == 0 and yhib)):
rcan = unionfix(rcan, (divideposright((-xlo, xlob), (-yhi, yhib)),))
# Upper right corner is in upper left quadrant, facing downhill:
if (xhi < 0 or (xhi == 0 and xhib)) and (yhi > 0 or (yhi == 0 and not yhib)):
rcan = unionfix(rcan, (neg(divideposleft((-xhi, xhib), (yhi, yhib))),))
# Lower left corner is in lower right quadrant, facing downhill:
if xlo >= 0 and (ylo < 0 or (ylo == 0 and not ylob)):
rcan = unionfix(rcan, (neg(divideposleft((xlo, xlob), (-ylo, ylob))),))
if any(isinstance(can, float) and math.isnan(can) for can in flatten(lcan)) or \
any(isinstance(can, float) and math.isnan(can) for can in flatten(rcan)):
(divleft, divright) = (float("nan"), float("nan"))
(openleft, openright) = (open, open)
(divleft, divright) = (lcan[0][0], rcan[-1][0])
(openleft, openright) = (lcan[0][1], rcan[-1][1])