Passing real values in "tasks or functions" in Verilog

Status
Not open for further replies.

kjm

Junior Member level 1
Joined
May 22, 2012
Messages
16
Helped
5
Reputation
10
Reaction score
5
Trophy points
1,283
Location
India
Activity points
1,389
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.

Thanks in Advance.
 

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
 
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.
 
Change your last assignment to

round_off = a_temp / 1000.0;

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

    kjm

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…