Fixed point division numbers - verilog

I have a little problem . I want to do a program that divides two numbers with fixed point . My output looks okay but when I test the program on a
local test site I get some strange results. Can someone help me?Thanks.
module divider(
input[7:0] a,b,
output reg [15:0] q,
output reg [7:0] r,frac
);
reg[7:0] c2, r2;
integer c;
always #(*)
begin
c = 8'b00000000;
r = a;
frac = 8'b00000000;
repeat (30)
begin
if (r >= b )
begin
c = c + 1;
r = r - b;
end
end
q[15:8] = c;
repeat (8)
begin
if(r != 0)
begin
r = r*10;
c2 = 8'b00000000;
repeat(30)
begin
if (r>=b)
begin
c2 = c2 + 1;
r = r-b;
end
end
frac = frac*10 + c2;
end
end
q[7:0] = frac;
end
endmodule
Output local site:
error: a = 1, b = 2, q = 0.019531, expected q = 0.500000
error: a = 1, b = 4, q = 0.097656, expected q = 0.250000
error: a = 1, b = 5, q = 0.007813, expected q = 0.199219
error: a = 1, b = 7, q = 0.570313, expected q = 0.140625
error: a = 1, b = 8, q = 0.488281, expected q = 0.125000
error: a = 1, b = 9, q = 0.777344, expected q = 0.109375
error: a = 1, b = 10, q = 0.003906, expected q = 0.097656
error: a = 1, b = 11, q = 0.363281, expected q = 0.089844
error: a = 1, b = 14, q = 0.785156, expected q = 0.070313
error: a = 1, b = 15, q = 0.664063, expected q = 0.066406
error: a = 1, b = 16, q = 0.441406, expected q = 0.062500
error: a = 1, b = 17, q = 0.937500, expected q = 0.058594
error: a = 1, b = 18, q = 0.386719, expected q = 0.054688
error: a = 1, b = 19, q = 0.207031, expected q = 0.050781
error: a = 1, b = 20, q = 0.019531, expected q = 0.046875
error: a = 1, b = 21, q = 0.187500, expected q = 0.046875
error: a = 1, b = 22, q = 0.679688, expected q = 0.042969
error: a = 1, b = 23, q = 0.695313, expected q = 0.042969
error: a = 1, b = 25, q = 0.015625, expected q = 0.039063
error: a = 1, b = 27, q = 0.589844, expected q = 0.035156
error: a = 1, b = 28, q = 0.890625, expected q = 0.035156
error: a = 1, b = 29, q = 0.824219, expected q = 0.031250
error: a = 1, b = 30, q = 0.832031, expected q = 0.031250
error: a = 1, b = 31, q = 0.804688, expected q = 0.031250
error: a = 1, b = 32, q = 0.207031, expected q = 0.031250
error: a = 1, b = 33, q = 0.121094, expected q = 0.027344
error: a = 1, b = 34, q = 0.437500, expected q = 0.027344
error: a = 1, b = 35, q = 0.570313, expected q = 0.027344
error: a = 1, b = 36, q = 0.914063, expected q = 0.027344
error: a = 1, b = 37, q = 0.000000, expected q = 0.023438
error: a = 1, b = 38, q = 0.339844, expected q = 0.023438
error: a = 1, b = 40, q = 0.097656, expected q = 0.023438

You don't necessarily need radix 10 for division on FPGA. Radix2 division (e.g. shift and subtract) would be much faster and easier to implement. I found this example for radix2 division.

Related

How to Sort a table of strings according to the order defined by another table of strings (Lua)

I have 2 lua tables:
OrderTbl = {'Hello', 'Question', 'Answer', 'Bye'}
UnsortedTbl = {'Question', 'Bye, 'Bye', 'Question', 'Hello', 'Something'}
How to sort UnsortedTbl in order given by OrderTbl? (Fields not found in OrderTbl are placed in the end of result table, unsorted)
I have translated a sample of code from Java, and it works with numbers. Here it is:
function first(arr, low, high, x, n)
if high >= low then
-- (low + high)/2
local mid = low + math.floor((high - low) / 2)
if (mid == 1 or x > arr[mid - 1]) and arr[mid] == x then
return mid
end
if x > arr[mid] then return first(arr, (mid + 1), high, x, n) end
return first(arr, low, (mid - 1), x, n)
end
return nil
end
-- Sort A1 according to the order
-- defined by A2
function sortAccording(A1, A2)
local m=#A1
local n=#A2
-- The temp array is used to store a copy
-- of A1{} and visited{} is used to mark the
-- visited elements in temp{}.
local temp = {}
local visited = {}
for i = 1, m do
temp[i] = A1[i]
visited[i] = 0
end
-- Sort elements in temp
table.sort(temp)
-- for index of output which is sorted A1{}
local ind = 0
-- Consider all elements of A2{}, find them
-- in temp{} and copy to A1{} in order.
for i = 1, n do
-- Find index of the first occurrence
-- of A2[i] in temp
local f = first(temp, 1, m, A2[i], m+1)
-- If not present, no need to proceed
if not f then
-- continue
else
-- Copy all occurrences of A2[i] to A1{}
j = f
while j < m and temp[j] == A2[i] do
A1[ind] = temp[j]
ind = ind + 1
visited[j] = 1
j = j + 1
end
end
end
-- Now copy all items of temp{} which are
-- not present in A2{}
for i = 1, m do
if visited[i] == 0 then
ind = ind + 1
A1[ind] = temp[i]
end
end
end
function printArray(arr)
for i = 1, #arr do
print(arr[i] .. " ")
end
end
-- Driver program to test above function.
local A1 = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8}
local A2 = {2, 1, 4, 3, 6, 5, 8, 7}
sortAccording(A1, A2)
printArray(A1)
I don't quite understand how to make it work with strings. Could you help me?
You can use the form of table.sort that accepts a custom comparator:
local OrderTbl = {'Hello', 'Question', 'Answer', 'Bye'}
local UnsortedTbl = {'Question', 'Bye', 'Bye', 'Question', 'Hello', 'Something', 'Else'}
-- convert the order to hash that can be easily queried
for idx, val in ipairs(OrderTbl) do OrderTbl[val] = idx end
local maxIdx = #OrderTbl + 1 -- this will mark "missing" elements
-- pass a custom comparator that will check OrderTbl
table.sort(UnsortedTbl, function(a, b)
local pa = OrderTbl[a] or maxIdx -- desired index of a
local pb = OrderTbl[b] or maxIdx -- desired index of b
if pa == pb then return a < b end -- sort by name
return pa < pb -- sort by index
end)

python error UnboundLocalError and issue with logic

This is what I've written:
def logic( arr , r , l ):
arr = []
i == 0
if (arr[i]) == (i +1) * i :
print('True')
i = i + 1
else :
print('False')
return(arr )
arr = list(map(int, input().split()))
r = int(input())
l = int(input())
print(logic(arr,r,l))
This is what we need:
You task is to calculate a boolean array b, where b[i] = true if there exists an integer x,
such that a[i] = (i + 1) * x and l ≤ x ≤ r. Otherwise, b[i] should be set to false.
Example
For a = [8, 5, 6, 16, 5], l = 1, and r = 3, the output should be logic(a, l, r) = [false, false, true, false, true].
For a[0] = 8, we need to find a value of x such that 1 * x = 8, but the only value that would work is x = 8 which doesn't satisfy the boundaries 1 ≤ x ≤ 3, so b[0] = false.
For a[1] = 5, we need to find a value of x such that 2 * x = 5, but there is no integer value that would satisfy this equation, so b[1] = false.
For a[2] = 6, we can choose x = 2 because 3 * 2 = 6 and 1 ≤ 2 ≤ 3, so b[2] = true.
Below is the error:
UnboundLocalError: local variable 'i' referenced before assignment
You are using i == 0. This is an equals to. This is not asigning the variable. I think you are looking for i = 0. == sign is used in If statements etc.
Also may I ask why you are using the parameters arr , r & l in your logic function. But assign them inside the function?
EDIT: I have not programmed in python for a while and missed the indent

How do I iterate through a column, multiplying each value against the value in two other fixed cells?

I'm trying to multiply a column with 2 fixed cells using VBA, but it is giving me back the same result, 2800, for the whole range:
For q = 4 To 9
For w = 3 To 8
Cells(q, 3).Value = Cells(12, 2) * Cells(13, 2) * Cells(w, 2).Value
Next w
Next q
This is no wonder.
You have an outer loop q and an inner loop w.
For all q the calculation is done with w = 8 in the end.
Therefore you write into all cells Cells(q,3)the latest result for w = 8 and that's why all results all the same.
the execution sequence in your code is:
q = 4, w = 3
q = 4, w = 4
q = 4, w = 5
...
q = 4, w = 8 > this is the shown result for in Cells(4,3)
q = 5, w = 3
...
q = 5, w = 8 > this is the shown result for in Cells(5,3)
...
q = 9, w = 3
...
q = 9, w = 8 > this is the shown result for in Cells(9,3)
What you need is:
For w = 3 To 5
Cells(w+1, 3).Value = Cells(12, 2) * Cells(13, 2) * Cells(w, 2).Value
Next w

Why those two pieces of code return different outputs?

Code snippet #1:
d = census_df[census_df.SUMLEV==50].copy()
d['max'] = d[['POPESTIMATE2010','POPESTIMATE2011','POPESTIMATE2012','POPESTIMATE2013','POPESTIMATE2014','POPESTIMATE2015']].max(axis=1)
d['min'] = d[['POPESTIMATE2010','POPESTIMATE2011','POPESTIMATE2012','POPESTIMATE2013','POPESTIMATE2014','POPESTIMATE2015']].min(axis=1)
d['diff'] = d['max'] - d['min']
d[d['diff'] == d['diff'].max()].iloc[0]['CTYNAME']
Code snippet #2:
d = census_df[census_df["SUMLEV"] == 50]
d= d.groupby(["STNAME"])["POPESTIMATE2015'].nlargest(3)
d["max"] = d[['POPESTIMATE2010','POPESTIMATE2011','POPESTIMATE2012','POPESTIMATE2013','POPESTIMATE2014','POPESTIMATE2015']].max(axis=1)
d["min"] = d[['POPESTIMATE2010','POPESTIMATE2011','POPESTIMATE2012','POPESTIMATE2013','POPESTIMATE2014','POPESTIMATE2015']].min(axis=1)
d["Diff"] = d["max"] - d["min"]
ans = d[d["Diff"] == d["Diff"]].max().iloc[0]["CTYNAME"]

Creating multiple files by changing values in a single file

I have a file named myfile.txt given below:
&cntrl
pTopt = 298.15, pdens = 0.997, prcut = 12.0, pion = t,
pihot = t, prQM = 5.5, prSM = 5.3, prQI=3.0, piguess = f,
pinit = t, pnstep = 5000, pnscale = 100,
pnstat = 5, pnout = 5, pnrst = 5, pioutc = t, pioutv = t, pnoutc=5,
pnoutv = 5,
msolute = t, nosa = 1, pichrg = t
gfileOut = 'as-1.out',
gfileEnout = 'as-1.en',
gfileInfo = 'as-1.info',
gfileStart = 'init.in',
gfileRst = 'as-1.rst',
gfileTraj = 'as-1.traj',
gfileVeloc = 'as-1.vel',
gfileQmen = 'as-1.qmen'
&end
Using the above given single file I want to create 10 files but I want to manipulate the values of last eight variables in a way that value of the variable in every new file changes as the number of file changes i.e if ten files are created then the value of last eight variables like gfileOut in tength file should be 'as-10.out'.
For doing this I have a code given below:
#!/usr/bin/python3
for i in range(10):
f = open('file' +str(i)+'.txt','w')
f.write("&cntrl pTopt = 298.15, pdens = 0.997, prcut = 12.0,pion=t,"+"\n"+
"pihot = t, prQM = 5.5, prSM = 5.3, prQI=3.0, piguess = f,"+"\n"+
"pinit = t, pnstep = 5000, pnscale = 100,"+"\n"+"pnstat = 5, pnout = 5,
pnrst = 5, pioutc = t, pioutv = t, pnoutc = 5, pnoutv = 5,"+"\n"+
"msolute = t, nosa = 1, pichrg = t"+"\n"+'gfileOut = as-' +str(i)+ ".out,"+"\n"+
'gfileEnout = as-' +str(i)+ '.en,'+"\n"+'gfileInfo = as-' +str(i)+".info,"+"\n"+
'gfileStart = init' +str(i)+ ".in,"+"\n"+'gfileRst = as' +str(i)+ ".rst,"+"\n"+
'gfileTraj = as' +str(i)+ ".traj,"+"\n"
+'gfileVeloc = as' +str(i)+ ".vel,"+"\n"+'gfileQmen = as' +str(i)+ '.qmen'+"\n"+"&end ")
f.close()
The above given code produces right output but I want a way to read myfile.txt and change the values of last eight variables as mentioned above and then use this file to create ten new files.
str.format handles the inserts into the string to be written to each of the files.
# Write the files.
for i in range(1, 11):
with open('file' +str(i)+ '.txt','w') as f:
f.write(
('&cntrl pTopt = 298.15, pdens = 0.997, prcut = 12.0, pion=t,\n'
'pihot = t, prQM = 5.5, prSM = 5.3, prQI=3.0, piguess = f,\n'
'pinit = t, pnstep = 5000, pnscale = 100,\n'
'pnstat = 5, pnout = 5, pnrst = 5, pioutc = t, pioutv = t, pnoutc = 5, pnoutv = 5,\n'
'msolute = t, nosa = 1, pichrg = t\n'
'gfileOut = as-{index}.out,\n'
'gfileEnout = as-{index}.en,\n'
'gfileInfo = as-{index}.info,\n'
'gfileStart = init{index}.in,\n'
'gfileRst = as{index}.rst,\n'
'gfileTraj = as{index}.traj,\n'
'gfileVeloc = as{index}.vel,\n'
'gfileQmen = as{index}.qmen\n'
'&end ').format(index=i))
Note: The string contains {index} which is replaced by the value of i that range(1, 10) sets.
Edit: Re-done post due to misunderstanding the details of the question alerted by the 1st comment. Sorry.
Edit: Looked at need to read from file, so this may help.
template.txt:
&cntrl pTopt = 298.15, pdens = 0.997, prcut = 12.0, pion=t,
pihot = t, prQM = 5.5, prSM = 5.3, prQI=3.0, piguess = f,
pinit = t, pnstep = 5000, pnscale = 100,
pnstat = 5, pnout = 5, pnrst = 5, pioutc = t, pioutv = t, pnoutc = 5, pnoutv = 5,
msolute = t, nosa = 1, pichrg = t
gfileOut = as-{index}.out,
gfileEnout = as-{index}.en,
gfileInfo = as-{index}.info,
gfileStart = init{index}.in,
gfileRst = as{index}.rst,
gfileTraj = as{index}.traj,
gfileVeloc = as{index}.vel,
gfileQmen = as{index}.qmen
&end
main script:
# Read template file.
with open('template.txt') as r:
content = r.read()
# Write the files.
for i in range(1, 11):
with open('file' +str(i)+ '.txt','w') as f:
f.write(content.format(index=i))

Resources