3

I wanted to use floating point numbers in System Verilog using the real data type. I tried the following code, but it doesn't seem to work. I'm getting 2.000000 where I expect 2.500000.

Module:

module real_check(input [31:0]a, [31:0]b,
                  output real c);
    assign c = a/b;
endmodule

Test bench:

module tb_real_check();

real c;
reg[31:0] a, b;
real_check dut(a, b, c);

initial 
begin

  a <= 5;
  b <= 2;
  #50;
  $display("Answer : %f ", c);

end
endmodule
0

1 Answer 1

8

You are doing integer division. When calculating int/int (reg[31:0]/reg[31:0]), the result is an integer. So, when you calculate 5/2, you get 2.5 truncated to 2 which is then converted into a floating point (real) number as 2.000.... If you want floating point division (which doesn't discard the decimal), you need to have your operands (dividend & divisor) be floating point.

So, to fix your code, your module needs to take two floating point numbers as input:

module real_check(input real a, real b,
                  output real c);

And in your test case, you need to declare a and b as floating point numbers:

real a, b;
Sign up to request clarification or add additional context in comments.

3 Comments

What if I want a certain bits floating point? I mean if I want a float point a = 0.5, I want it 4 bits width. Can I get some thing like 4'b0100 ? I am building a lookup table for Newton-Raphson division. The table contains 128 24-bits values, value X = 48/17 - 32/17*D, which D is 8-bit binary and the region is [0.5, 1]. I am struggling write a .sv code to automatically generate the lookup table.
@ShuaiyuJiang you mean a floating point with a specified precision? So, instead of, say, 32-bits of precision (single-precision), you have 4?
Sorry, Let me simplify my question. I wanted a 24-bits output logic a[23:0], and a = 48/17 (for example). I know if I just use logic type, the answer will be 2, because the remainder is truncated. I tried use statement "real", and I got a = 2.823529..... The question is, I actually want a binary logic out, not a real number, is there any methods to help me get a logic output of 48/17? God, my English presentation is bad, can you understand what I mean? TAT

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.