Continue to Site

# Passing real values in "tasks or functions" in Verilog

Status
Not open for further replies.

#### kjm

##### Junior Member level 1
Hi,
I want to pass a real value to the function (or task) in verilog. That function will do few calculations in real data type and should return a real value to my module. How can I implement this ? Please explain with a small example.

Code:
module top;
function real mult(input real A,B);
mult = A*B;
endfunction

real X,Y;
initial begin
X =2.5;Y=5.5;
$display("%g",mult(X,Y)); end endmodule ### maulin sheth Points: 2 Helpful Answer Positive Rating ### kjm Points: 2 Helpful Answer Positive Rating Thanks dave_59, this is very useful. But I am having a small problem. I am trying to round off a real value ( For eg. 0.002625 to 0.003) as shown in code below, but here its giving wrong result. Could you please suggest any solution? module top; function real round_off(input real a); real ax1000; integer a_temp; begin ax1000 = a * 1000; a_temp = ax1000; round_off = a_temp / 1000; end endfunction real X; initial begin X =0.002625;$display("%g",round_off(X));
end
endmodule

add a .0 to the 1000 used for division

i.e. round_off = a_temp / 1000.0;

as written before you were performing an integer divide and assigning it to a real, so of course you get a 0 answer.

morris_mano

### morris_mano

Points: 2

round_off = a_temp / 1000.0;

Without the .0, all the operands are integers, and the result of the operation is an integer.

kjm

Points: 2