Skip to content

Commit c3526e0

Browse files
committed
Version 0.5
1 parent eed23f0 commit c3526e0

File tree

3 files changed

+200
-15
lines changed

3 files changed

+200
-15
lines changed

README.md

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
ieee754 is a Python module which finds the IEEE-754 representation of a floating point number. You can specify a precision given in the list below or you can even use your own custom precision.
44
<ul>
5-
<li>Half Precision (16 bit: 1 bit for sign + 5 bits for exponent + 10 bits for mantissa)</li>
6-
<li>Single Precision (32 bit: 1 bit for sign + 8 bits for exponent + 23 bits for mantissa)</li>
7-
<li>Double Precision (64 bit: 1 bit for sign + 11 bits for exponent + 52 bits for mantissa)</li>
8-
<li>Quadruple Precision (128 bit: 1 bit for sign + 15 bits for exponent + 112 bits for mantissa)</li>
9-
<li>Octuple Precision (256 bit: 1 bit for sign + 19 bits for exponent + 236 bits for mantissa)</li>
5+
<li>Half Precision (16 bit: 1 bit for sign + 5 bits for exponent + 10 bits for mantissa)</li>
6+
<li>Single Precision (32 bit: 1 bit for sign + 8 bits for exponent + 23 bits for mantissa)</li>
7+
<li>Double Precision (64 bit: 1 bit for sign + 11 bits for exponent + 52 bits for mantissa)</li>
8+
<li>Quadruple Precision (128 bit: 1 bit for sign + 15 bits for exponent + 112 bits for mantissa)</li>
9+
<li>Octuple Precision (256 bit: 1 bit for sign + 19 bits for exponent + 236 bits for mantissa)</li>
10+
</ul>
1011

1112
## Prerequisites
1213

@@ -20,9 +21,20 @@ $ pip install ieee754
2021
```
2122

2223
## Using
24+
2325
After installation, you can import ieee754 and use it in your projects.
2426

27+
### Simplest Example
28+
29+
The simplest example is to use the desired precision IEEE-754 representation of a floating point number. You can import the desired precision from ieee754 and use it like this. The available precisions are half, single, double, quadruple and octuple.
30+
```Python
31+
from ieee754 import double
32+
33+
print(double(13.375))
34+
```
35+
2536
### Default Options
37+
2638
Default precision is Double Precision and you can get the output by just calling the instance as a string.
2739
```Python
2840
from ieee754 import IEEE754
@@ -40,6 +52,7 @@ print(a.json())
4052
```
4153

4254
### Select a Precision
55+
4356
You can use Half (p=0), Single (p=1), Double (p=2), Quadrupole (p=3) or Octuple precision (p=4).
4457
```Python
4558
from ieee754 import IEEE754
@@ -50,6 +63,7 @@ for p in range(5):
5063
```
5164

5265
### Use the Precision Name as an Interface
66+
5367
You can use the precision name as an interface to get the IEEE-754 representation of a floating point number. With this method you can get the IEEE-754 representation of a floating point number without creating an instance.
5468
```Python
5569
from ieee754 import half, single, double, quadruple, octuple
@@ -63,6 +77,7 @@ print(octuple(x))
6377
```
6478

6579
### Using a Custom Precision
80+
6681
You can force exponent, and mantissa size by using force_exponent and force_mantissa parameters to create your own custom precision.
6782
```Python
6883
a = IEEE754(x, force_exponent=6, force_mantissa=12)

ieee754/IEEE754.py

Lines changed: 173 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,31 @@
22

33

44
class IEEE754:
5+
"""
6+
IEEE 754 Floating Point Representation
7+
https://en.wikipedia.org/wiki/IEEE_754
8+
9+
Author: Bora Canbula
10+
Email: bora.canbula@cbu.edu.tr
11+
Link: https://github.com/canbula/ieee754
12+
13+
Parameters
14+
----------
15+
number : str
16+
Floating point number to be converted.
17+
precision : int, optional
18+
Precision of the number, by default 2
19+
force_exponent : int, optional
20+
Force the exponent bits, by default None
21+
force_mantissa : int, optional
22+
Force the mantissa bits, by default None
23+
24+
Returns
25+
-------
26+
IEEE754
27+
IEEE 754 representation of the number
28+
"""
29+
530
def __init__(
631
self,
732
number: str = "0.0",
@@ -166,26 +191,173 @@ def __repr__(self) -> str:
166191

167192

168193
def half(x: str) -> IEEE754:
194+
"""
195+
IEEE 754 Half Precision Representation
196+
197+
Parameters
198+
----------
199+
x : str
200+
Floating point number to be converted.
201+
202+
Returns
203+
-------
204+
IEEE754
205+
IEEE 754 representation of the number
206+
207+
Examples
208+
--------
209+
>>> half(13.375)
210+
0 10010 1010110000
211+
212+
>>> half(13.375).hex()
213+
4AB0
214+
215+
>>> half(13.375).json()
216+
{'exponent-bits': 5, 'mantissa-bits': 10, 'bias': 15, 'sign': '0', 'exponent': '10010', 'mantissa': '1010110000', 'binary': '1101011', 'binary_output': '1101.011', 'hex': '4AB0', 'up scaled number': 107, 'scale': 3, 'number': 13.375}
217+
218+
Project
219+
-------
220+
https://github.com/canbula/ieee754
221+
"""
169222
return IEEE754(x, 0)
170223

171224

172225
def single(x: str) -> IEEE754:
226+
"""
227+
IEEE 754 Single Precision Representation
228+
229+
Parameters
230+
----------
231+
x : str
232+
Floating point number to be converted.
233+
234+
Returns
235+
-------
236+
IEEE754
237+
IEEE 754 representation of the number
238+
239+
Examples
240+
--------
241+
>>> single(13.375)
242+
0 10000010 10101100000000000000000
243+
244+
>>> single(13.375).hex()
245+
41560000
246+
247+
>>> single(13.375).json()
248+
{'exponent-bits': 8, 'mantissa-bits': 23, 'bias': 127, 'sign': '0', 'exponent': '10000010', 'mantissa': '10101100000000000000000', 'binary': '1101011', 'binary_output': '1101.011', 'hex': '41560000', 'up scaled number': 107, 'scale': 3, 'number': 13.375}
249+
250+
Project
251+
-------
252+
https://github.com/canbula/ieee754
253+
"""
173254
return IEEE754(x, 1)
174255

175256

176257
def double(x: str) -> IEEE754:
258+
"""
259+
IEEE 754 Double Precision Representation
260+
261+
Parameters
262+
----------
263+
x : str
264+
Floating point number to be converted.
265+
266+
Returns
267+
-------
268+
IEEE754
269+
IEEE 754 representation of the number
270+
271+
Examples
272+
--------
273+
>>> double(13.375)
274+
0 10000000010 1010110000000000000000000000000000000000000000000000
275+
276+
>>> double(13.375).hex()
277+
402AC00000000000
278+
279+
>>> double(13.375).json()
280+
{'exponent-bits': 11, 'mantissa-bits': 52, 'bias': 1023, 'sign': '0', 'exponent': '10000000010', 'mantissa': '1010110000000000000000000000000000000000000000000000', 'binary': '1101011', 'binary_output': '1101.011', 'hex': '402AC00000000000', 'up scaled number': 107, 'scale': 3, 'number': 13.375}
281+
282+
Project
283+
-------
284+
https://github.com/canbula/ieee754
285+
"""
177286
return IEEE754(x, 2)
178287

179288

180289
def quadruple(x: str) -> IEEE754:
290+
"""
291+
IEEE 754 Quadruple Precision Representation
292+
293+
Parameters
294+
----------
295+
x : str
296+
Floating point number to be converted.
297+
298+
Returns
299+
-------
300+
IEEE754
301+
IEEE 754 representation of the number
302+
303+
Examples
304+
--------
305+
>>> quadruple(13.375)
306+
0 100000000000010 1010110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
307+
308+
>>> quadruple(13.375).hex()
309+
4002AC00000000000000000000000000
310+
311+
>>> quadruple(13.375).json()
312+
{'exponent-bits': 15, 'mantissa-bits': 112, 'bias': 16383, 'sign': '0', 'exponent': '100000000000010', 'mantissa': '1010110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', 'binary': '1101011', 'binary_output': '1101.011', 'hex': '4002AC00000000000000000000000000', 'up scaled number': 107, 'scale': 3, 'number': 13.375}
313+
314+
Project
315+
-------
316+
https://github.com/canbula/ieee754
317+
"""
181318
return IEEE754(x, 3)
182319

183320

184321
def octuple(x: str) -> IEEE754:
322+
"""
323+
IEEE 754 Octuple Precision Representation
324+
325+
Parameters
326+
----------
327+
x : str
328+
Floating point number to be converted.
329+
330+
Returns
331+
-------
332+
IEEE754
333+
IEEE 754 representation of the number
334+
335+
Examples
336+
--------
337+
>>> octuple(13.375)
338+
0 1000000000000000010 10101100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
339+
340+
>>> octuple(13.375).hex()
341+
40002AC000000000000000000000000000000000000000000000000000000000
342+
343+
>>> octuple(13.375).json()
344+
{'exponent-bits': 19, 'mantissa-bits': 236, 'bias': 262143, 'sign': '0', 'exponent': '1000000000000000010', 'mantissa': '10101100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', 'binary': '1101011', 'binary_output': '1101.011', 'hex': '40002AC000000000000000000000000000000000000000000000000000000000', 'up scaled number': 107, 'scale': 3, 'number': 13.375}
345+
346+
Project
347+
-------
348+
https://github.com/canbula/ieee754
349+
"""
185350
return IEEE754(x, 4)
186351

187352

188353
if __name__ == "__main__":
354+
# you can call the precision functions by using their names
355+
x = 13.375
356+
print(half(x))
357+
print(single(x))
358+
print(double(x))
359+
print(quadruple(x))
360+
print(octuple(x))
189361
# with default options (Double Precision)
190362
x = 13.375
191363
a = IEEE754(x)
@@ -199,16 +371,9 @@ def octuple(x: str) -> IEEE754:
199371
for p in range(5):
200372
a = IEEE754(x, p)
201373
print("x = %f | b = %s | h = %s" % (13.375, a, a.hex()))
374+
print(a.json())
202375
# or you can use your own custom precision
203376
a = IEEE754(x, force_exponent=6, force_mantissa=12)
204377
print(f"{a}")
205378
# you can get more details with json
206379
print(a.json())
207-
208-
# you can also call the precision functions
209-
x = 13.375
210-
print(half(x))
211-
print(single(x))
212-
print(double(x))
213-
print(quadruple(x))
214-
print(octuple(x))

setup.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
from distutils.core import setup
22

3+
with open("README.md", "r") as fh:
4+
long_description = fh.read()
5+
36
setup(
47
name="ieee754",
58
packages=["ieee754"],
6-
version="0.4",
9+
version="0.5",
710
license="MIT",
811
description="A Python module which converts floating points numbers into IEEE-754 representation.",
12+
long_description=long_description,
13+
long_description_content_type="text/markdown",
914
author="Bora Canbula",
1015
author_email="bora.canbula@cbu.edu.tr",
1116
url="https://github.com/canbula/ieee754",
12-
download_url="https://github.com/canbula/ieee754/archive/refs/tags/v_04.tar.gz",
17+
download_url="https://github.com/canbula/ieee754/archive/refs/tags/v_05.tar.gz",
1318
keywords=[
1419
"IEEE-754",
1520
"precisions",

0 commit comments

Comments
 (0)