File: __init__.py

package info (click to toggle)
spigot 0.2017-01-15.gdad1bbc6-1
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, buster, forky, sid, stretch, trixie
  • size: 904 kB
  • ctags: 1,521
  • sloc: cpp: 9,516; sh: 1,225; python: 643; makefile: 24
file content (144 lines) | stat: -rwxr-xr-x 4,400 bytes parent folder | download
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
# User-visible side of the spigot Python package. Uses Python operator
# overloading to build a wrapper around the C++ spigot class that
# makes it look more or less like a Python numeric type.

import spigot.internal
import struct
import types

def mkintspig(x):
    ret = spigot.internal.Spigot()
    if isinstance(x, Spigot):
        ret.clone(x)
    elif type(x) == types.FloatType:
        bitpat = struct.unpack(">Q", struct.pack(">d",1))[0]
        if 0x7ff & ~(bitpat >> 52) == 0:
            raise ValueError("cannot construct spigot from infinity or NaN")
        ret.parse("ieee:%x" % bitpat)
    elif type(x) == types.IntType or type(x) == types.LongType:
        ret.parse("%d" % x)
    else:
        raise TypeError("cannot convert %s to spigot" % repr(type(x)))
    return ret

class Spigot(object):
    def __init__(self, x, scope=None):
        if scope != None or type(x) == types.StringType:
            self.sp = spigot.internal.Spigot()
            self.sp.parse(x, scope)
        else:
            self.sp = mkintspig(x)

    def __add__(self, s2):
        if not isinstance(s2, Spigot):
            s2 =  Spigot(s2)
        return Spigot("x+y", {"x":self.sp, "y":s2.sp})

    def __radd__(self, s2):
        if not isinstance(s2, Spigot):
            s2 =  Spigot(s2)
        return Spigot("y+x", {"x":self.sp, "y":s2.sp})

    def __sub__(self, s2):
        if not isinstance(s2, Spigot):
            s2 =  Spigot(s2)
        return Spigot("x-y", {"x":self.sp, "y":s2.sp})

    def __rsub__(self, s2):
        if not isinstance(s2, Spigot):
            s2 =  Spigot(s2)
        return Spigot("y-x", {"x":self.sp, "y":s2.sp})

    def __mul__(self, s2):
        if not isinstance(s2, Spigot):
            s2 =  Spigot(s2)
        return Spigot("x*y", {"x":self.sp, "y":s2.sp})

    def __rmul__(self, s2):
        if not isinstance(s2, Spigot):
            s2 =  Spigot(s2)
        return Spigot("y*x", {"x":self.sp, "y":s2.sp})

    def __div__(self, s2):
        if not isinstance(s2, Spigot):
            s2 =  Spigot(s2)
        return Spigot("x/y", {"x":self.sp, "y":s2.sp})

    def __rdiv__(self, s2):
        if not isinstance(s2, Spigot):
            s2 =  Spigot(s2)
        return Spigot("y/x", {"x":self.sp, "y":s2.sp})

    def __pow__(self, s2):
        if not isinstance(s2, Spigot):
            s2 =  Spigot(s2)
        return Spigot("x^y", {"x":self.sp, "y":s2.sp})

    def __rpow__(self, s2):
        if not isinstance(s2, Spigot):
            s2 =  Spigot(s2)
        return Spigot("y^x", {"x":self.sp, "y":s2.sp})

    def __neg__(self):
        return Spigot("-x", {"x":self.sp})

    def __pos__(self):
        return self

    def __abs__(self):
        return Spigot("abs(x)", {"x":self.sp})

    def __long__(self):
        sp = spigot.internal.Spigot()
        sp.clone(self.sp)
        intpart = sp.cfracterm()
        if intpart < 0:
            next = sp.cfracterm()
            if next != None:
                intpart = intpart + 1
        return intpart

    def __int__(self):
        return int(self.__long__())

    def __float__(self):
        sp = spigot.internal.Spigot()
        sp.clone(self.sp)
        sp.ieee(64, 0, 2) # FIXME: symbolic rounding mode constant
        out = ""
        while 1:
            s = sp.readfmt()
            if s == "": break
            out = out + s
        return struct.unpack(">d", struct.pack(">Q", long(out, base=16)))[0]

    def __cmp__(self, s2): 
        if not isinstance(s2, Spigot):
            s2 =  Spigot(s2)
        sp = spigot.internal.Spigot()
        sp.parse("x-y", {"x":self.sp, "y":s2.sp})
        return sp.sign()

    def __nonzero__(self):
        sp = spigot.internal.Spigot()
        sp.clone(self.sp)
        return sp.sign() != 0

    def continued_fraction(self):
        "Generator which returns a number's continued fraction term by term."
        sp = spigot.internal.Spigot()
        sp.clone(self.sp)
        while True:
            term = sp.cfracterm()
            if term is None:
                return
            yield term

    def convergents(self):
        """Generator which returns a number's continued fraction convergents
as successive (numerator, denominator) 2-tuples."""
        n0, n1, d0, d1 = 0, 1, 1, 0
        for term in self.continued_fraction():
            n0, n1 = n1, term*n1+n0
            d0, d1 = d1, term*d1+d0
            yield (n1, d1)