@@ -23,39 +23,43 @@ def to_csd(num, places=0, debug=False):
23
23
""" Convert the argument to CSD Format. """
24
24
25
25
if debug :
26
- print ("Converting %f " % (num ), )
26
+ print (
27
+ "Converting %f " % (num ),
28
+ )
27
29
28
30
# figure out binary range, special case for 0
29
- if num == 0. :
30
- return '0'
31
+ if num == 0 :
32
+ return "0"
31
33
32
34
absnum = fabs (num )
33
- n = 0 if absnum < 1. else ceil (log (absnum * 1.5 , 2 ))
34
- csd_str = '0' if absnum < 1. else ''
35
+ n = 0 if absnum < 1.0 else ceil (log (absnum * 1.5 , 2 ))
36
+ csd_str = "0" if absnum < 1.0 else ""
35
37
36
38
if debug :
37
39
print ("to %d.%d format" % (n , places ))
38
40
39
- limit = pow (2. , n ) / 3.
40
- while (n > - places ):
41
+ # limit = pow(2., n) / 3.
42
+ pow2n = pow (2.0 , n - 1 )
43
+ while n > - places :
41
44
if debug :
42
- print (" " , num , limit )
45
+ print (" " , num , 2 * pow2n / 3 )
43
46
44
47
# decimal point?
45
48
if n == 0 : # unlikely
46
- csd_str += '.'
49
+ csd_str += "."
47
50
48
51
n -= 1
49
52
# convert the number
50
- if num > limit :
51
- csd_str += '+'
52
- num -= 1.5 * limit
53
- elif num < - limit :
54
- csd_str += '-'
55
- num += 1.5 * limit
53
+ d = 1.5 * num
54
+ if d > pow2n :
55
+ csd_str += "+"
56
+ num -= pow2n
57
+ elif d < - pow2n :
58
+ csd_str += "-"
59
+ num += pow2n
56
60
else :
57
- csd_str += '0'
58
- limit /= 2.
61
+ csd_str += "0"
62
+ pow2n /= 2
59
63
60
64
if debug :
61
65
print (csd_str )
@@ -69,23 +73,23 @@ def to_decimal(csd_str, debug=False):
69
73
if debug :
70
74
print ("Converting: " , csd_str )
71
75
72
- num = 0.
76
+ num = 0.0
73
77
loc = 0
74
78
for i , c in enumerate (csd_str ):
75
- num *= 2.
76
- if c == '+' :
77
- num += 1.
78
- elif c == '-' :
79
- num -= 1.
80
- elif c == '0' :
79
+ num *= 2.0
80
+ if c == "+" :
81
+ num += 1.0
82
+ elif c == "-" :
83
+ num -= 1.0
84
+ elif c == "0" :
81
85
pass
82
- elif c == '.' : # unlikely
83
- num /= 2.
86
+ elif c == "." : # unlikely
87
+ num /= 2.0
84
88
loc = i + 1
85
89
else :
86
90
raise ValueError
87
91
if loc != 0 :
88
- num /= pow (2. , len (csd_str ) - loc )
92
+ num /= pow (2.0 , len (csd_str ) - loc )
89
93
return num
90
94
91
95
@@ -127,36 +131,40 @@ def to_csdfixed(num, nnz=4, debug=False):
127
131
""" Convert the argument to CSD Format. """
128
132
129
133
if debug :
130
- print ("Converting %f " % (num ), )
134
+ print (
135
+ "Converting %f " % (num ),
136
+ )
131
137
132
138
# figure out binary range, special case for 0
133
- if num == 0. :
134
- return '0'
139
+ if num == 0.0 :
140
+ return "0"
135
141
absnum = fabs (num )
136
- n = 0 if absnum < 1. else ceil (log (absnum * 1.5 , 2 ))
137
- csd_str = '0' if absnum < 1. else ''
138
- limit = pow (2. , n ) / 3.
139
- while (n > 0 or nnz > 0 ):
142
+ n = 0 if absnum < 1.0 else ceil (log (absnum * 1.5 , 2 ))
143
+ csd_str = "0" if absnum < 1.0 else ""
144
+ # limit = pow(2., n) / 3.
145
+ pow2n = pow (2.0 , n - 1 )
146
+ while n > 0 or nnz > 0 :
140
147
if debug :
141
- print (" " , num , limit )
148
+ print (" " , num , 2 * pow2n / 3 )
142
149
143
150
# decimal point?
144
151
if n == 0 :
145
- csd_str += '.'
152
+ csd_str += "."
146
153
147
154
n -= 1
148
155
# convert the number
149
- if num > limit :
150
- csd_str += '+'
151
- num -= 1.5 * limit
156
+ d = 1.5 * num
157
+ if d > pow2n :
158
+ csd_str += "+"
159
+ num -= pow2n
152
160
nnz -= 1
153
- elif num < - limit :
154
- csd_str += '-'
155
- num += 1.5 * limit
161
+ elif d < - pow2n :
162
+ csd_str += "-"
163
+ num += pow2n
156
164
nnz -= 1
157
165
else :
158
- csd_str += '0'
159
- limit /= 2.
166
+ csd_str += "0"
167
+ pow2n /= 2.0
160
168
161
169
if nnz == 0 :
162
170
num = 0
0 commit comments