+import io, math
from . import data
T_END = 0
dst.write(text.encode("utf-8"))
dst.write(b'\0')
+ @staticmethod
+ def writefloat(dst, x):
+ if x == 0.0:
+ mnt, exp = (0, 0)
+ elif math.isinf(x):
+ if x > 0:
+ mnt, exp = (0, 2)
+ else:
+ mnt, exp = (0, 3)
+ elif math.isnan(x):
+ mnt, exp = (0, 4)
+ else:
+ mnt, exp = math.frexp(x)
+ mnt *= 2
+ exp -= 1
+ while mnt != int(mnt):
+ mnt *= 2
+ mnt = int(mnt)
+ buf = bytearray()
+ buf.extend(encoder.encint(mnt))
+ buf.extend(encoder.encint(exp))
+ dst.write(encoder.encint(len(buf)))
+ dst.write(buf)
+
def dumpseq(self, dst, seq):
for v in seq:
self.dump(dst, v)
self.writetag(dst, T_BIT, 0, datum)
dst.write(self.encint(len(datum)))
dst.write(datum)
+ elif isinstance(datum, float):
+ self.writetag(dst, T_BIT, BIT_BFLOAT, datum)
+ self.writefloat(dst, datum)
elif isinstance(datum, data.symbol):
if datum.ns == "":
self.writetag(dst, T_STR, STR_SYM, datum)
return self.addref(ret)
elif pri == T_BIT:
ln = self.loadint(fp)
- ret = self.addref(fp.read(ln))
+ ret = fp.read(ln)
if len(ret) < ln:
raise eoferror()
- return ret
+ if sec == BIT_BFLOAT:
+ buf = io.BytesIO(ret)
+ mnt = self.loadint(buf)
+ exp = self.loadint(buf)
+ if mnt == 0:
+ if exp == 0:
+ return 0.0
+ elif exp == 1:
+ return -0.0
+ elif exp == 2:
+ return float("inf")
+ elif exp == 3:
+ return -float("inf")
+ else:
+ return float("nan")
+ else:
+ ret = math.ldexp(mnt, exp - (mnt.bit_length() - 1))
+ return self.addref(ret)
+ return self.addref(ret)
elif pri == T_NIL:
if sec == NIL_TRUE:
return self.addref(True)