-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPythonFloatpointconv.txt
More file actions
144 lines (74 loc) · 2.79 KB
/
PythonFloatpointconv.txt
File metadata and controls
144 lines (74 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import os
def binaryOfFraction(fraction):
binary = str()
while (fraction):
fraction *= 2
if (fraction >= 1):
int_part = 1
fraction -= 1
else:
int_part = 0
binary += str(int_part)
return binary
def binaryOfFraction(fraction):
binary = str()
while (fraction):
fraction *= 2
if (fraction >= 1):
int_part = 1
fraction -= 1
else:
int_part = 0
binary += str(int_part)
return binary
def floatingPoint(real_no):
sign_bit = 0
if(real_no < 0):
sign_bit = 1
real_no = abs(real_no)
int_str = bin(int(real_no))[2 : ]
#print(int_str)
fraction_str = binaryOfFraction(real_no - int(real_no))
if(real_no<1):
ind = fraction_str.index('1')
exp_str=bin(-(ind+1)+127)[2:]#right shift of decimal
exp_str = ('0' * (8- len(exp_str)))+exp_str
if(ind==len(fraction_str)-1):
mant_str="0"
mant_str = fraction_str[ind + 1 : ]
mant_str = mant_str + ('0' * (23 - len(mant_str)))#filling with 0s in LSb if bits are less than 23
mant_str=mant_str[0:23]#if bits are more than 23
else:
ind = int_str.index('1')
exp_str = bin((len(int_str) - ind - 1) + 127)[2 : ]
if (real_no==1):
exp_str="0"+bin((len(int_str) - ind - 1) + 127)[2: ]
exp_str = ('0' * (8- len(exp_str)))+exp_str
mant_str = int_str[ind + 1 : ] + fraction_str
mant_str = mant_str + ('0' * (23 - len(mant_str)))
if(len(mant_str)>23):
mant_str=mant_str[0:23]
return sign_bit, exp_str, mant_str
# Driver Code
if __name__ == "__main__":
#sign_bit, exp_str, mant_str = floatingPoint(0.75)
#ieee_32 = str(sign_bit) + exp_str + mant_str
#Printing the ieee 32 represenation.
infile=open("C:\\Users\\rohit\\Downloads\\five_values.txt","r")
file=open("C:\\Users\\rohit\\Downloads\\five_values_fp.txt","a")
lines=infile.readlines()
file.truncate(0)
infile.close()
for i in lines:
try:
x=float(i)/255
sign_bit, exp_str, mant_str = floatingPoint(x)
ieee_32 = str(sign_bit) + exp_str + mant_str
#print(ieee_32)
file.write(ieee_32+"\n")
except ValueError:
file.write("0"*32+"\n")
file.close()
#sign_bit, exp_str, mant_str = floatingPoint(int(i)/256)
#ieee_32 = str(sign_bit) + exp_str + mant_str
#print(ieee_32)