0

I'm trying to out the fixed point value in verilog. Generally, I use like this method (I'don't know what it was called) for example,

output = 0.248*5
output = ((0.248 << 8) * 5 ) >> 8;

But in his case the output has only integer.

I want to get fixed point fraction part.

How to handle to out the fixed point value in verilog?

nashile
  • 23
  • 2
  • 6
  • 1
    can you show the definition of output, how are you getting the value, display ? or passing to another module. Verilog does not have a fixed point type so you have to use scaled integers. You also need to define your fixed point format. – Morgan May 30 '16 at 12:12
  • @Morgan Thanks morgan. actually, I've got two types output means that one is display, one is passing to another module. – nashile May 31 '16 at 11:30

1 Answers1

0

To calculate and display with 8 fractional bits:

integer out;

initial begin
  out = ((0.248 * 2**8) * 5 );
  $display("%f", $itor(out)*(2.0**-8.0));
end
Morgan
  • 19,934
  • 8
  • 58
  • 84
  • would you let me know what's called this method? – nashile Jun 01 '16 at 12:08
  • Dear Morgan, what if I have to handle floating point calculation, tricky, can I convert floating point calculate to fixed point ? or should I have to another way? – nashile Jun 01 '16 at 12:12
  • Sorry @nashile do not quite understand your question. you have to define your variable, Verilog only has real (floating point) and integers. Fixed point value have to be scaled up by the number of fractional bits you need. to interpret the value ie for $display you scale back down, but the number is stored as an integer. its is just **scaled integer**. – Morgan Jun 01 '16 at 12:12
  • Do you need to handle floating point in synthesisable code? Floating point is very tricky. normally will need a separate core to process it. – Morgan Jun 01 '16 at 12:13
  • Do you need to handle floating point in synthesisable code? --> Yes, Morgan, Floating point is very tricky? I want to know more when I haveto handle floating point what am I supposed to that? – nashile Jun 01 '16 at 12:25
  • Simply, I think that I can handle by use the method as called scaled up whatever fixed point or floating point. doesn't it? I think the matter is precision, how much do I have the precision. am I right? Please advice me. – nashile Jun 01 '16 at 12:31
  • If you need to ask about floating point, that really needs to be another question as it is a massive subject. I assume your talking about adhering to a [floating point specification](https://en.wikipedia.org/wiki/Floating_point#IEEE_754:_floating_point_in_modern_computers) which is very strict about the arithmetic. I suggest you look into some thing like [fpu on open cores](http://opencores.org/project,fpu). Fixed point is pretty straight forward you decide how much precision you need. and you scale by the amount required. – Morgan Jun 01 '16 at 13:26
  • Thanks @Morgan, How about your idea simply to use scale up in floating point calculation? might be, I think it will works well, If I make sure the precision. – nashile Jun 01 '16 at 14:32
  • But by how much, the point with floating point is the scaling changes. You need to consider why you have to deal with floating point, is some one giving you floating point values. or do you just need to deal with fractional information? if all numbers can be converted to fixed point then that is a much easier, smaller and lower power solution. floating point allows very small precise numbers and very large imprecise numbers do you need that number range? – Morgan Jun 01 '16 at 14:36
  • Yes, @Morgan, someone give me floating point value. and I have to give the floating value to another person. Oh~ did you mean that the floating point can be controlled by scale up, but it's cost very high, so you recommend to find another way. am I understanding right? – nashile Jun 01 '16 at 14:42
  • the cost of floating point calculations is huge. fixed point is trivial. The question started with fixed point but you quickly moved to discuss floating point. I am not sure why you have a floating point requirement I think you just need fractional (fixed point). – Morgan Jun 01 '16 at 22:44