user3056350
user3056350

Reputation: 67

how to calculate fractional part of a floating point number in verilog

I have floating point numbers. Lets say 4.6 and 3.8. We convert them to fixed point (Q4.4) by doing 4.6 x 2^4 = 74 and 3.8 x 2^4 = 61. We add both numbers 74 + 61 = 135. Now we convert this result 135 to floating point by doing 135 / 2^4 = 8.4. This will give integer part of result which is 8.

Now fractional part is calculated by doing (2^-2 + 2^-3 + 2^-4). Can someone tell how to get this value of 4 in verilog so that I can display it on 7 segment display as 8.4?

Upvotes: 0

Views: 1283

Answers (1)

wilcroft
wilcroft

Reputation: 1635

There's a couple of simple solutions.

One way would be to make a lookup table: for each of the 16 possible values, figure out what the decimal point should be (i.e. 0b0111 = 0.4375 ~= .4) and build a case statement to produce the correct value. This shouldn't be too hard or complicated, since you're only using 4 bits.

Another option would be to do the addition yourself, using a BCD adder, which would allow you to extract the highest order digit (i.e. 0b0111 = 250+125+62 = 437 -> 4).

Upvotes: 1

Related Questions