Skip to content

Commit 40c30f7

Browse files
committed
fix up-scale for negative numbers
1 parent 057bd8a commit 40c30f7

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

ieee754/IEEE754.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ def __init__(
7575
self.original_number: Decimal = self.number
7676
if self.__edge_case is None:
7777
self.sign: str = self.find_sign()
78+
self.number = self.number.copy_abs()
7879
self.__scale, self.number = self.scale_up_to_integer(self.number, 2)
7980
self.binary: str = f"{self.number:b}"
8081
self.binary_output: str = (
@@ -171,12 +172,12 @@ def find_sign(self) -> str:
171172
return "1"
172173
return "0"
173174

174-
@staticmethod
175-
def scale_up_to_integer(number: Decimal, base: int) -> (int, int):
175+
def scale_up_to_integer(self, number: Decimal, base: int) -> (int, int):
176176
scale = 0
177177
while number != int(number):
178178
number *= base
179179
scale += 1
180+
self.output["unable_to_scale"] = scale > 100
180181
return scale, int(number)
181182

182183
def find_exponent(self) -> str:
@@ -449,5 +450,13 @@ def octuple(x: str) -> IEEE754:
449450
x = 8.7
450451
a = IEEE754(x, 1)
451452
print(f"{x} is converted as {a.converted_number} ± {a.error}")
452-
# you can get the full output as a dictionary with produce_output()
453-
print(a.produce_output())
453+
x = -0.75
454+
a = IEEE754(x, 1)
455+
print(f"{x} is converted as {a.converted_number} ± {a.error}")
456+
# test edge cases
457+
print(IEEE754(0))
458+
print(IEEE754("-0"))
459+
print(IEEE754("Infinity"))
460+
print(IEEE754("-Infinity"))
461+
print(IEEE754("NaN"))
462+
print(IEEE754("-NaN"))

0 commit comments

Comments
 (0)