-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbig_float.py
More file actions
executable file
·119 lines (91 loc) · 3.14 KB
/
big_float.py
File metadata and controls
executable file
·119 lines (91 loc) · 3.14 KB
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
#!/usr/bin/env python3
from __future__ import annotations
from collections import deque
from dataclasses import dataclass
from typing import Deque, Generic, List, Sequence, TypeVar, Union
T = TypeVar('T')
# class Arr(Generic[T]):
# def __init__(self, default: T) -> None:
# self.default = default
# self.items = Deque[T]()
# self.start = 0
# @property
# def stop(self) -> int:
# return self.start + len(self.items)
# def __getitem__(self, index: int) -> T:
# i = index - self.start
# if i not in range(len(self.items)):
# return self.default
# return self.items[i]
# def __setitem__(self, index: int, item: T) -> None:
# if index < self.start:
# self.items.extendleft(self.default for _ in range(self.start - index))
# self.start = index
# elif index >= self.stop:
# # + 1 since `self[index]` must be in range
# self.items.extend(self.default for _ in range(index - self.stop + 1))
# self.items[index - self.start] = item
# class BigFloat:
# RADIX = 256
# def __init__(self, value: Union[int] = None) -> None:
# self.digits = Arr(default=0)
# if value is None:
# return
# if isinstance(value, int):
# self.digits[0] = value
# self.normalize()
# def __str__(self) -> str:
# return '{} : {}'.format(
# ' '.join(f'{d:02x}' for d in reversed(self.digits.items)),
# self.digits.start,
# )
# def __add__(self, other: BigFloat) -> BigFloat:
# r = BigFloat()
# for f in (self, other):
# d = f.digits
# for i in range(d.start, d.stop):
# r.digits[i] += d[i]
# r.normalize()
# return r
# def normalize(self) -> None:
# d = self.digits
# for i in range(d.start, d.stop):
# c, d[i] = divmod(d[i], self.RADIX)
# d[i + 1] += c
class BigFloat:
def __init__(self, mant: int, prec: int = 0) -> None:
if mant != 0:
while True:
d, r = divmod(mant, 2)
if r != 0:
break
mant = d
prec -= 1
self.mant = mant
self.prec = prec
@staticmethod
def from_expo(mant: int, expo: int) -> BigFloat:
return BigFloat(mant=mant, prec=mant.bit_length() - expo)
def with_prec(self, prec: int) -> int:
return self.mant << (prec - self.prec)
@property
def expo(self) -> int:
"""how many bits before the decimal point"""
return self.mant.bit_length() - self.prec
def __repr__(self) -> str:
return f'{self.mant}:{self.prec}'
def __str__(self) -> str:
return f'0.{self.mant:x}^{self.expo:x}'
def __add__(self, other: BigFloat) -> BigFloat:
prec = max(self.prec, other.prec)
mant = self.with_prec(prec) + other.with_prec(prec)
return BigFloat(mant, prec)
def main() -> None:
a = BigFloat(0x123456)
b = BigFloat(0x789abc)
c = a + b
print(a)
print(b)
print(c)
if __name__ == '__main__':
main()