Skip to content

Commit 941a297

Browse files
committed
improve float_to_rational method
1 parent f34eb12 commit 941a297

File tree

3 files changed

+154
-16
lines changed

3 files changed

+154
-16
lines changed

shared/utils.py

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,33 +25,42 @@ def rational_to_float(value) -> float:
2525

2626

2727
def float_to_rational(value: float):
28-
"""Casts a float to a DcRationalNumberType.
28+
"""Casts a float to a RationalNumberType.
2929
3030
:param value: The value to cast.
31-
:return: DcRationalNumberType -- the cast result.
31+
:return: tuple with value and exponent-- the cast result.
3232
"""
33-
if value == 0:
34-
return 0, 0
35-
string = format(value, ".2e")
36-
if abs(value) < 1:
37-
string = string.split("e") # Keep the sign
33+
sign = ""
34+
if value < 0:
35+
sign = "-"
36+
exp = 0
37+
string = format(value, ".3f") # Keep three decimal places
38+
elements = string.split(".")
39+
val = elements[0]
40+
decimal = elements[1]
41+
42+
if abs(value) < 1: # decimal number
43+
val = sign + decimal
44+
exp = -3
3845
else:
39-
string = string.split("e+")
40-
try:
41-
value = int(string[0])
42-
exponent = int(string[1])
43-
except ValueError:
44-
value = int(string[0].replace('.', ''))
45-
exponent = int(string[1]) - 2
46-
return value, exponent
46+
while abs(int(val)) > 32767:
47+
val = str(round(int(val) / 10))
48+
exp += 1
49+
if int(decimal) != 0:
50+
while abs(int(val)) <= 3000:
51+
val = val + decimal[0]
52+
decimal = decimal[1:]
53+
exp -= 1
54+
if decimal == '':
55+
break
56+
return int(val), int(exp)
4757

4858

4959
def float_to_dc_rational(input: float) -> DcRationalNumberType:
5060
value, exponent = float_to_rational(input)
5161
return DcRationalNumberType(exponent, value)
5262

5363

54-
5564
def greater_rational(rational_1, rational_2):
5665
"""Compares two RationalNumberType.
5766

tests/__init__.py

Whitespace-only changes.

tests/test_utils.py

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
from shared.utils import float_to_rational
2+
import pytest
3+
4+
5+
# Positive values :
6+
def test_float_to_rational_003() -> None:
7+
output = float_to_rational(0.003)
8+
assert output[0] == 3 and output[1] == -3
9+
10+
11+
def test_float_to_rational_021() -> None:
12+
output = float_to_rational(0.021)
13+
assert output[0] == 21 and output[1] == -3
14+
15+
16+
def test_float_to_rational_0321() -> None:
17+
output = float_to_rational(0.321)
18+
assert output[0] == 321 and output[1] == -3
19+
20+
21+
def test_float_to_rational_1() -> None:
22+
output = float_to_rational(1)
23+
assert output[0] == 1 and output[1] == 0
24+
25+
26+
def test_float_to_rational_1_3888888888888888() -> None:
27+
output = float_to_rational(1.3888888888888888)
28+
assert output[0] == 1389 and output[1] == -3
29+
30+
31+
def test_float_to_rational_12() -> None:
32+
output = float_to_rational(12)
33+
assert output[0] == 12 and output[1] == 0
34+
35+
def test_float_to_rational_99_999() -> None:
36+
output = float_to_rational(99.999)
37+
assert output[0] == 9999 and output[1] == -2
38+
39+
40+
def test_float_to_rational_123() -> None:
41+
output = float_to_rational(123)
42+
assert output[0] == 123 and output[1] == 0
43+
44+
def test_float_to_rational_12_201() -> None:
45+
output = float_to_rational(12.201)
46+
assert output[0] == 12201 and output[1] == -3
47+
48+
def test_float_to_rational_123_201() -> None:
49+
output = float_to_rational(123.201)
50+
assert output[0] == 12320 and output[1] == -2
51+
52+
53+
def test_float_to_rational_3000() -> None:
54+
output = float_to_rational(3000)
55+
assert output[0] == 3000 and output[1] == 0
56+
57+
58+
def test_float_to_rational_32769() -> None:
59+
output = float_to_rational(32769)
60+
assert output[0] == 3277 and output[1] == 1
61+
62+
63+
def test_float_to_rational_100000() -> None:
64+
output = float_to_rational(100000)
65+
assert output[0] == 10000 and output[1] == 1
66+
67+
68+
# Negative values :
69+
70+
def test_float_to_rational_neg_003() -> None:
71+
output = float_to_rational(-0.003)
72+
assert output[0] == -3 and output[1] == -3
73+
74+
75+
def test_float_to_rational_neg_021() -> None:
76+
output = float_to_rational(-0.021)
77+
assert output[0] == -21 and output[1] == -3
78+
79+
80+
def test_float_to_rational_neg_0321() -> None:
81+
output = float_to_rational(-0.321)
82+
assert output[0] == -321 and output[1] == -3
83+
84+
85+
def test_float_to_rational_neg_1() -> None:
86+
output = float_to_rational(-1)
87+
assert output[0] == -1 and output[1] == 0
88+
89+
90+
def test_float_to_rational_neg_1_3888888888888888() -> None:
91+
output = float_to_rational(-1.3888888888888888)
92+
assert output[0] == -1389 and output[1] == -3
93+
94+
95+
def test_float_to_rational_neg_12() -> None:
96+
output = float_to_rational(-12)
97+
assert output[0] == -12 and output[1] == 0
98+
99+
def test_float_to_rational_neg_99_999() -> None:
100+
output = float_to_rational(-99.999)
101+
assert output[0] == -9999 and output[1] == -2
102+
103+
104+
def test_float_to_rational_neg_123() -> None:
105+
output = float_to_rational(-123)
106+
assert output[0] == -123 and output[1] == 0
107+
108+
def test_float_to_rational_neg_12_201() -> None:
109+
output = float_to_rational(-12.201)
110+
assert output[0] == -12201 and output[1] == -3
111+
112+
def test_float_to_rational_neg_123_201() -> None:
113+
output = float_to_rational(-123.201)
114+
assert output[0] == -12320 and output[1] == -2
115+
116+
117+
def test_float_to_rational_neg_3000() -> None:
118+
output = float_to_rational(-3000)
119+
assert output[0] == -3000 and output[1] == 0
120+
121+
122+
def test_float_to_rational_32769() -> None:
123+
output = float_to_rational(32769)
124+
assert output[0] == 3277 and output[1] == 1
125+
126+
127+
def test_float_to_rational_100000() -> None:
128+
output = float_to_rational(100000)
129+
assert output[0] == 10000 and output[1] == 1

0 commit comments

Comments
 (0)