Question

I need to use rational numbers in my Verilog code. I looked for any resource but I couldn't find anything about this issue. How can I define rational numbers in Verilog.

Was it helpful?

Solution

Verilog has a real data type which is used for real (floating-point) numbers:

real data;
initial data = 1.55;

OTHER TIPS

There are two main ways to implement rational numbers in an HDL:

1) If you want a fixed denominator that is always a power of two, you can simply use fixed point numbers that have the normal two's-complement representation, but have bits with an index less than zero, so just like a [3:0] "0111" would be 0*2**3 + 1*2**2 + 1*2**1 + 1*2**0 = 7, a [1:-2] "0111" would be 0*2**1 + 1*2**0 + 1*2**-1 + 1*2**-2 = 1.75 . In regular Verilog you can use negative indicies like this, but you will have to handle the shifting and scaling when you do operations manually (e.g. put each operation in it's own function or module). With SystemVerilog or VHDL, you can create (or use an existing) fixed-point package that handles this automatically with data types.

2) If you want to have arbitrary denominators, you can simple use two regs to represent each value, where one is the numerator and one is the denominator. Again, you will have to handle the implementation of mathematical operations manually. And again, if you use SystemVerilog or VHDL, you can wrap this behavior up into a data type.

Good luck!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top