csdigit 0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,23 @@
1
+ csdigit/__init__.py,sha256=ethe4DV1CwbXvO2DSjEzt5SL_7jt2fxQnF4RPgRgTDI,577
2
+ csdigit/cli.py,sha256=61wnJbytzqDceWq3P8U3xe1foqCKXjcRzzZYaNXBSSI,4861
3
+ csdigit/csd.py,sha256=8pHywE4MouE4JvYH18jHoFn2syOWPXJipFUh2GxlAOI,10453
4
+ csdigit/lcsre.py,sha256=khnTPl5OmUen-YCBUbrRiLZ3yP0wbl1PUuEDDyAArBo,4097
5
+ csdigit-0.1.dist-info/licenses/LICENSE.txt,sha256=MyvlyWiWdJ3ZpAOA4wziXlNa9Hbx8XnEL1Aztt9F7W8,1080
6
+ pycsd/README,sha256=R3WCgkJvH_SLy3Qdpq2RVYmVnvTUtQqebS_pccWL3BU,107
7
+ pycsd/__init__.py,sha256=GkvOaIq-1mVFK-8I1VUVau51KezscAoIhz4XG8OaYRM,576
8
+ pycsd/csd_orig.py,sha256=ulYJ0lY3zU_rHahFNK-Nc3bpxHt2gt0MzimRWVohxU8,2043
9
+ pycsd/from_csd,sha256=7tOc5VqFDTmXA-g7A0lGbZ8Wuh1VDPeFq2AIStt1Wi4,130
10
+ pycsd/run_tests.py,sha256=zq-l8XxOz2qbcR6TeN4sGOVc35y6W98IC5rU_h89F6c,140
11
+ pycsd/to_csd,sha256=-H0j1sahFyXl21bGDM_oozVCukpEMw4fFHDuSozuvdE,132
12
+ pycsd/csd/__init__.py,sha256=8RyxYl6Pbn85ROIa1NIpUAb2hxvgTf0ZDYDtgtm84FE,18
13
+ pycsd/csd/csd.py,sha256=zuBE8AF-I6_uMZgLdu_wYdNp04J2wuUqmGKNZHuRs8A,2510
14
+ pycsd/dev/csd_numbers.py,sha256=X5OKjDdZqEHTESwa-i1_ymoIT_yromrdQ6wQCqEQNqA,1471
15
+ pycsd/test/__init__.py,sha256=9ZPkF1BIV_0m3jzrScec7lfueRM9l1HI4QOyVikiqPU,101
16
+ pycsd/test/decimal_tests.py,sha256=1jCGT2wRqG9suG_NSGJVFUz6YyaX3fAgy0tVp38mwaw,645
17
+ pycsd/test/test_csd_decimals.py,sha256=6_cSt9avT3fLuy9NvMNdnRhLaGgbJRwfr-TJGz3RCb0,1853
18
+ pycsd/test/test_csd_integers.py,sha256=tQEEWcqG4R-_MR-WQ9ntBOPAUJr4p-wnK8W-TpDs_Kw,888
19
+ pycsd/test/test_to_and_fro_10bits.py,sha256=wqDnKmUCHUuOOj8Vgyn14-DDXlLZCFCgFUvykIBlb2g,35570
20
+ csdigit-0.1.dist-info/METADATA,sha256=mrORoKITCqJ9-T9Yd9J447lgDJ0KxC1eya8T8nKckmA,6024
21
+ csdigit-0.1.dist-info/WHEEL,sha256=CmyFI0kx5cdEMTLiONQRbGQwjIoR1aIYB7eCAQ4KPJ0,91
22
+ csdigit-0.1.dist-info/top_level.txt,sha256=IZLj8Opb9xA8Dz-pBH1l-vhTlogz2RFld-07NH0cw_8,14
23
+ csdigit-0.1.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (78.1.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Wai-Shing Luk
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,2 @@
1
+ csdigit
2
+ pycsd
pycsd/README ADDED
@@ -0,0 +1 @@
1
+ Python module to convert decimals/floating points to and from Canonical Signed Digit (CSD) representation.
pycsd/__init__.py ADDED
@@ -0,0 +1,16 @@
1
+ import sys
2
+
3
+ if sys.version_info[:2] >= (3, 8):
4
+ # TODO: Import directly (no need for conditional) when `python_requires = >= 3.9`
5
+ from importlib.metadata import PackageNotFoundError, version # pragma: no cover
6
+ else:
7
+ from importlib_metadata import PackageNotFoundError, version # pragma: no cover
8
+
9
+ try:
10
+ # Change here if project is renamed and does not equal the package name
11
+ dist_name = "pycsd"
12
+ __version__ = version(dist_name)
13
+ except PackageNotFoundError: # pragma: no cover
14
+ __version__ = "unknown"
15
+ finally:
16
+ del version, PackageNotFoundError
pycsd/csd/__init__.py ADDED
@@ -0,0 +1 @@
1
+ from csd import *
pycsd/csd/csd.py ADDED
@@ -0,0 +1,118 @@
1
+ #! /usr/bin/env python
2
+ """
3
+ Canonical Signed Digit Functions
4
+
5
+ Handles:
6
+ * Decimals
7
+ *
8
+ *
9
+
10
+ eg, +00-00+000.0 or 0.+0000-00+
11
+ Where: '+' is +1
12
+ '-' is -1
13
+
14
+ Harnesser
15
+ License: GPL2
16
+ """
17
+
18
+ from math import *
19
+
20
+
21
+ def to_csd( num, places=0, debug=False ):
22
+ """ Convert the argument to CSD Format. """
23
+
24
+ if debug: print "Converting %f " % ( num ),
25
+
26
+ # figure out binary range, special case for 0
27
+ if num == 0 :
28
+ return '0'
29
+ if fabs(num) < 1.0 :
30
+ n = 0
31
+ else :
32
+ n = ceil( log( fabs(num) * 3.0 / 2.0 , 2 ) )
33
+
34
+ csd_digits = []
35
+
36
+ if debug: print "to %d.%d format" % ( n, places )
37
+
38
+ # Hone in on the CSD code for the input number
39
+ remainder = num
40
+ previous_non_zero = False
41
+ n -= 1
42
+
43
+ while( n >= -places):
44
+
45
+ limit = pow(2.0,n+1) / 3.0
46
+
47
+ if debug: print " ", remainder, limit,
48
+
49
+ # decimal point?
50
+ if n == -1 :
51
+ csd_digits.extend( ['.'] )
52
+
53
+ # convert the number
54
+ if previous_non_zero:
55
+ csd_digits.extend( ['0'] )
56
+ prev_non_zero = False
57
+
58
+ elif remainder > limit :
59
+ csd_digits.extend( ['+'] )
60
+ remainder -= pow(2.0, n )
61
+ prev_non_zero = True
62
+
63
+ elif remainder < -limit :
64
+ csd_digits.extend( ['-'] )
65
+ remainder += pow(2.0, n )
66
+ prev_non_zero = True
67
+
68
+ else :
69
+ csd_digits.extend( ['0'] )
70
+ prev_non_zero = False
71
+
72
+ n -= 1
73
+
74
+ if debug: print csd_digits
75
+
76
+ # Always have something before the point
77
+ if fabs(num) < 1.0:
78
+ csd_digits.insert(0, '0')
79
+
80
+ csd_str = "".join( csd_digits )
81
+
82
+ return csd_str
83
+
84
+
85
+
86
+ def to_decimal( csd_str, debug=False ):
87
+ """ Convert the CSD string to a decimal """
88
+
89
+ if debug:
90
+ print "Converting: ", csd_str
91
+
92
+
93
+ # Find out what the MSB power of two should be, keeping in
94
+ # mind we may have a fractional CSD number
95
+ try:
96
+ (m,n) = csd_str.split('.')
97
+ csd_str = csd_str.replace('.','') # get rid of point now...
98
+ except ValueError:
99
+ m = csd_str
100
+ n = ""
101
+
102
+ msb_power = len(m)-1
103
+
104
+ num = 0.0
105
+ for ii in xrange( len(csd_str) ):
106
+
107
+ power_of_two = 2.0**(msb_power-ii)
108
+
109
+ if csd_str[ii] == '+' :
110
+ num += power_of_two
111
+ elif csd_str[ii] == '-' :
112
+ num -= power_of_two
113
+
114
+ if debug:
115
+ print ' "%s" (%d.%d); 2**%d = %d; Num=%f' % (
116
+ csd_str[ii], len(m), len(n), msb_power-ii, power_of_two, num)
117
+
118
+ return num
pycsd/csd_orig.py ADDED
@@ -0,0 +1,99 @@
1
+ #! /usr/bin/env python
2
+ """
3
+ Canonical Signed Digit Functions
4
+
5
+ Handles:
6
+ * Decimals
7
+ *
8
+ *
9
+
10
+ eg, +00-00+000.0 or 0.+0000-00+
11
+ Where: '+' is +1
12
+ '-' is -1
13
+
14
+ Harnesser
15
+ License: GPL2
16
+ """
17
+
18
+ from math import *
19
+
20
+
21
+ def to_csd(num, places=0):
22
+ """Convert the argument to CSD Format."""
23
+
24
+ # figure out binary range, special case for 0
25
+ if num == 0:
26
+ return "0"
27
+ if fabs(num) < 1.0:
28
+ n = 0
29
+ else:
30
+ n = ceil(log(fabs(num) * 3.0 / 2.0, 2))
31
+
32
+ csd_digits = []
33
+
34
+ # Hone in on the CSD code for the input number
35
+ remainder = num
36
+ previous_non_zero = False
37
+ n -= 1
38
+
39
+ while n >= -places:
40
+ limit = pow(2.0, n + 1) / 3.0
41
+
42
+ # decimal point?
43
+ if n == -1:
44
+ csd_digits.extend(["."])
45
+
46
+ # convert the number
47
+ if previous_non_zero:
48
+ csd_digits.extend(["0"])
49
+ prev_non_zero = False
50
+
51
+ elif remainder > limit:
52
+ csd_digits.extend(["+"])
53
+ remainder -= pow(2.0, n)
54
+ prev_non_zero = True
55
+
56
+ elif remainder < -limit:
57
+ csd_digits.extend(["-"])
58
+ remainder += pow(2.0, n)
59
+ prev_non_zero = True
60
+
61
+ else:
62
+ csd_digits.extend(["0"])
63
+ prev_non_zero = False
64
+
65
+ n -= 1
66
+
67
+ # Always have something before the point
68
+ if fabs(num) < 1.0:
69
+ csd_digits.insert(0, "0")
70
+
71
+ csd_str = "".join(csd_digits)
72
+
73
+ return csd_str
74
+
75
+
76
+ def to_decimal(csd_str):
77
+ """Convert the CSD string to a decimal"""
78
+
79
+ # Find out what the MSB power of two should be, keeping in
80
+ # mind we may have a fractional CSD number
81
+ try:
82
+ (m, n) = csd_str.split(".")
83
+ csd_str = csd_str.replace(".", "") # get rid of point now...
84
+ except ValueError:
85
+ m = csd_str
86
+ n = ""
87
+
88
+ msb_power = len(m) - 1
89
+
90
+ num = 0.0
91
+ for ii in range(len(csd_str)):
92
+ power_of_two = 2.0 ** (msb_power - ii)
93
+
94
+ if csd_str[ii] == "+":
95
+ num += power_of_two
96
+ elif csd_str[ii] == "-":
97
+ num -= power_of_two
98
+
99
+ return num
@@ -0,0 +1,86 @@
1
+ #! /usr/bin/env python
2
+ """ Convert some CSD numbers to get a feel for the range. """
3
+
4
+ import sys
5
+
6
+ sys.path.append('/home/mmcgrana/lib/python/csd')
7
+ import csd
8
+
9
+ num_digits = 4
10
+
11
+ # recursion
12
+ csd_num = [ '0' ] * num_digits
13
+
14
+ indent_ = 2
15
+
16
+ csd_dict = {}
17
+
18
+ def is_proper_csd(csd_num):
19
+ """ Check we've a valid CSD number
20
+ Returns false if the array contains consequitive non-zero terms.
21
+ """
22
+
23
+ for i in range( 1, len(csd_num) ):
24
+ if csd_num[i] != '0' and csd_num[i-1] != '0':
25
+ return False
26
+
27
+ return True
28
+
29
+
30
+ def cycle_bits( i ) :
31
+
32
+ global indent_
33
+
34
+ indent_ += 2
35
+
36
+ for ch in ( '0', '-', '+' ):
37
+ csd_num[i] = ch
38
+
39
+ if( i > 0 ):
40
+ cycle_bits( i-1 )
41
+ else :
42
+ if is_proper_csd(csd_num):
43
+ csd_str = ''.join(csd_num)
44
+ num = csd.to_decimal( csd_str )
45
+ csd_dict[num] = csd_str
46
+
47
+ indent_ -= 2
48
+
49
+
50
+ def wiki_table():
51
+ """ A table of CSD numbers for the wiki. """
52
+
53
+ ordered_keys = csd_dict.keys()
54
+ ordered_keys.sort()
55
+
56
+ wiki_str = """
57
+ {| BORDER=1
58
+ ! CSD
59
+ ! Decimal
60
+ |-
61
+ """
62
+
63
+ for key in ordered_keys:
64
+ wiki_str += """
65
+ | <tt>%s</tt>
66
+ | %d
67
+ |- """ % ( csd_dict[key], key )
68
+
69
+ wiki_str += "|}"
70
+ return wiki_str
71
+
72
+ def show_csd():
73
+ """ """
74
+
75
+ ordered_keys = csd_dict.keys()
76
+ ordered_keys.sort()
77
+
78
+ for key in ordered_keys:
79
+ print csd_dict[key], key
80
+
81
+
82
+ if __name__ == '__main__' :
83
+
84
+ cycle_bits( num_digits-1 )
85
+
86
+ print wiki_table()
pycsd/from_csd ADDED
@@ -0,0 +1,12 @@
1
+ #! /usr/bin/env python
2
+ """
3
+ from csd
4
+ """
5
+
6
+ import sys
7
+
8
+ sys.path.append('./csd')
9
+
10
+ import csd
11
+
12
+ print csd.to_decimal( sys.argv[1] )
pycsd/run_tests.py ADDED
@@ -0,0 +1,13 @@
1
+ #! /usr/bin/env python
2
+ """
3
+ Run all the tests
4
+ """
5
+
6
+ import sys
7
+
8
+ sys.path.append("./csd")
9
+
10
+ import unittest
11
+ from test import *
12
+
13
+ unittest.main()
pycsd/test/__init__.py ADDED
@@ -0,0 +1,3 @@
1
+ from test_csd_decimals import *
2
+ from test_csd_integers import *
3
+ from test_to_and_fro_10bits import *
@@ -0,0 +1,33 @@
1
+ #! /usr/bin/env python
2
+ """
3
+ Unittests for the CSD module
4
+
5
+ """
6
+
7
+ import sys
8
+
9
+ sys.path.append('../csd')
10
+ import csd
11
+
12
+ print dir(csd)
13
+ import unittest
14
+
15
+ good_values_dict = { 32.5 : '+0000.+' }
16
+ class test__integer_conversion( unittest.TestCase ):
17
+
18
+ def testToCSD(self):
19
+ """ Check that integers are converted to CSD properly. """
20
+
21
+ for key in good_values_dict.keys():
22
+ csd_str = csd.to_csd(key)
23
+ self.assert_(csd_str == good_values_dict[key] )
24
+
25
+
26
+ def suite():
27
+ suite = unittest.TestSuite()
28
+ suite.addTest(unittest.makeSuite(IntegerConversion))
29
+ return suite
30
+
31
+
32
+ if __name__ == '__main__':
33
+ unittest.main()
@@ -0,0 +1,66 @@
1
+ #! /usr/bin/env python
2
+ """
3
+ Unittests for the CSD module
4
+
5
+ """
6
+
7
+ import unittest
8
+
9
+ import csd
10
+
11
+ good_values_dict = {
12
+ 16.5: "+0000.+",
13
+ -16.5: "-0000.-",
14
+ -2.5: "-0.-",
15
+ 0.5: "0.+",
16
+ -0.5: "0.-",
17
+ }
18
+
19
+
20
+ class tests__decimals(unittest.TestCase):
21
+ def tmp(self):
22
+ pass
23
+
24
+ def test__01_to_decimal(self):
25
+ """Check the conversion from CSD with a binary point to decimal."""
26
+
27
+ for key in good_values_dict.keys():
28
+ csd_str = good_values_dict[key]
29
+ value = csd.to_decimal(csd_str)
30
+ self.assertEquals(value, key)
31
+
32
+ def test__01a_to_decimal(self):
33
+ self.assertEquals(csd.to_decimal("0.+00-"), 0.5 - (2.0**-4))
34
+ self.assertEquals(csd.to_decimal("0.0+0-"), 0.25 - (2.0**-4))
35
+ self.assertEquals(csd.to_decimal("0.+0-0"), 0.5 - (2.0**-3))
36
+
37
+ def test__02_to_csd_1_place(self):
38
+ """Check that decimals are converted to CSD properly."""
39
+
40
+ for key in good_values_dict.keys():
41
+ csd_str = csd.to_csd(key, places=1)
42
+ self.assertEquals(csd_str, good_values_dict[key])
43
+
44
+ def test__03_to_csd_4_places(self):
45
+ """To four places"""
46
+ self.assertEquals(csd.to_csd(0.0625, 4), "0.000+")
47
+ self.assertEquals(csd.to_csd(-0.0625, 4), "0.000-")
48
+ self.assertEquals(csd.to_csd(0.25, 4), "0.0+00")
49
+ self.assertEquals(csd.to_csd(-0.25, 4), "0.0-00")
50
+
51
+ def test__04_to_csd_x_places(self):
52
+ """To four places"""
53
+ self.assertEquals(csd.to_csd(0.0625, 4), "0.000+")
54
+ self.assertEquals(csd.to_csd(-0.0625, 4), "0.000-")
55
+ self.assertEquals(csd.to_csd(0.25, 4), "0.0+00")
56
+ self.assertEquals(csd.to_csd(-0.25, 4), "0.0-00")
57
+
58
+
59
+ def suite():
60
+ suite = unittest.TestSuite()
61
+ suite.addTest(unittest.makeSuite(tests__decimals))
62
+ return suite
63
+
64
+
65
+ if __name__ == "__main__":
66
+ unittest.main()
@@ -0,0 +1,38 @@
1
+ #! /usr/bin/env python
2
+ """
3
+ Unittests for the CSD module
4
+
5
+ """
6
+
7
+ import unittest
8
+
9
+ import csd
10
+
11
+ good_values_dict = {32: "+00000", -32: "-00000", 0: "0", 7: "+00-", 15: "+000-"}
12
+
13
+
14
+ class tests__integers(unittest.TestCase):
15
+ def test__01_to_integer(self):
16
+ """Check conversion from CSD to integer"""
17
+
18
+ for key in good_values_dict.keys():
19
+ csd_str = good_values_dict[key]
20
+ value = csd.to_decimal(csd_str)
21
+ self.assertEquals(value, key)
22
+
23
+ def test__02_to_csd(self):
24
+ """Check that integers are converted to CSD properly."""
25
+
26
+ for key in good_values_dict.keys():
27
+ csd_str = csd.to_csd(key)
28
+ self.assertEquals(csd_str, good_values_dict[key])
29
+
30
+
31
+ def suite():
32
+ suite = unittest.TestSuite()
33
+ suite.addTest(unittest.makeSuite(tests__integers))
34
+ return suite
35
+
36
+
37
+ if __name__ == "__main__":
38
+ unittest.main()