Skip to content

Commit a94d415

Browse files
committed
Avoid division and manipulate the string directly instead
1 parent 173defb commit a94d415

File tree

3 files changed

+24
-27
lines changed

3 files changed

+24
-27
lines changed

extra/detail/decode_decimal128.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,14 @@ def decode_decimal128(bits):
4444
if significand == 0:
4545
result = "0.0e+0"
4646
else:
47-
n_digits = len(str(abs(significand)))
48-
49-
# If there is no fractional component we want to remove the decimal point and zero
50-
if n_digits > 1:
51-
normalized = significand / (10 ** (n_digits - 1))
52-
total_exp = exp + n_digits - 1
53-
else:
54-
normalized = significand
47+
sig_str = str(significand)
48+
n_digits = len(sig_str)
49+
if n_digits == 1:
50+
normalized_str = sig_str
5551
total_exp = exp
56-
result = f"{'-' if sign else ''}{normalized:.{n_digits - 1}f}e{total_exp:+}"
52+
else:
53+
normalized_str = sig_str[0] + '.' + sig_str[1:]
54+
total_exp = exp + n_digits - 1
55+
result = f"{'-' if sign else ''}{normalized_str}e{total_exp:+}"
5756

5857
return result

extra/detail/decode_decimal32.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,14 @@ def decode_decimal32(bits):
4040
if significand == 0:
4141
result = "0.0e+0"
4242
else:
43-
n_digits = len(str(abs(significand)))
44-
45-
# If there is no fractional component we want to remove the decimal point and zero
46-
if n_digits > 1:
47-
normalized = significand / (10 ** (n_digits - 1))
48-
total_exp = exp + n_digits - 1
49-
else:
50-
normalized = significand
43+
sig_str = str(significand)
44+
n_digits = len(sig_str)
45+
if n_digits == 1:
46+
normalized_str = sig_str
5147
total_exp = exp
52-
result = f"{'-' if sign else ''}{normalized:.{n_digits - 1}f}e{total_exp:+}"
48+
else:
49+
normalized_str = sig_str[0] + '.' + sig_str[1:]
50+
total_exp = exp + n_digits - 1
51+
result = f"{'-' if sign else ''}{normalized_str}e{total_exp:+}"
5352

5453
return result

extra/detail/decode_decimal64.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,14 @@ def decode_decimal64(bits):
3939
if significand == 0:
4040
result = "0.0e+0"
4141
else:
42-
n_digits = len(str(abs(significand)))
43-
44-
# If there is no fractional component we want to remove the decimal point and zero
45-
if n_digits > 1:
46-
normalized = significand / (10 ** (n_digits - 1))
47-
total_exp = exp + n_digits - 1
48-
else:
49-
normalized = significand
42+
sig_str = str(significand)
43+
n_digits = len(sig_str)
44+
if n_digits == 1:
45+
normalized_str = sig_str
5046
total_exp = exp
51-
result = f"{'-' if sign else ''}{normalized:.{n_digits - 1}f}e{total_exp:+}"
47+
else:
48+
normalized_str = sig_str[0] + '.' + sig_str[1:]
49+
total_exp = exp + n_digits - 1
50+
result = f"{'-' if sign else ''}{normalized_str}e{total_exp:+}"
5251

5352
return result

0 commit comments

Comments
 (0)