7.4 Verilog Real to Integer Conversion
Category Verilog Tutorial Advanced
Keywords: Fixed-point number, Floating-point number, $realtobits, $bitstoreal
This section mainly introduces the functions for converting between real numbers and integers: $realtobits, $bitstoreal, and also explains how real type (same as double float in C language) variables are represented by multi-bit binary codes.
Binary Representation of Decimals
Binary Representation of Decimals
When representing decimal integers in binary, the operation of dividing the data by 2 and taking the remainder is required.
When representing the fractional part in binary, it is the exact opposite, requiring the operation of multiplying the data by 2 and then determining whether the integer part is greater than 1.
The process of obtaining the binary representation of the fractional part 0.3125 of 2.3125 is as follows:
Calculation Process | Judgment | Binary Bit |
---|---|---|
0.3125 x 2 = 0.625 | < 1 | 0 |
0.625 x 2 = 1.25 | ≥ 1 | 1 |
(1.25 - 1) x 2 = 0.5 | < 1 | 0 |
0.5 x 2 = 1 | ≥ 1 | 1 |
Result: The fractional part is represented by a 4-bit binary number | stop | 0101 |
So the binary representation of 2.3125 is dec2bin(2.3125) = bin(10.0101). Here, the decimal point is just used to distinguish the position of the fractional part, i.e., the fractional part is represented by a 4-bit binary code. In practice, the decimal point identifier does not exist.
When representing decimals in binary, there is also a shortcut method. Suppose the fractional part is represented by N bits, then the binary representation value of a decimal num is:
dec2bin(num) = bin(num x 2^N)
For example, the conversion process of the decimal 2.3125 is as follows:
2.3125 x 2^4 = 37 = bin(100101)
If the fractional part width is 4, then dec2bin(2.3125) = bin(10.0101).
Binary to Decimal
When converting binary integers to decimals, it is necessary to perform multiplication and accumulation operations for each bit with an exponent of 2.
When converting binary decimals to decimals, it is necessary to perform multiplication and accumulation operations for each bit with an exponent of 1/2.
For example, when the fractional part width is 4, the decimal representation of the binary fraction bin(11.1001) is:
bin2dec(1001) = 1 x 2^(-1) + 0 x 2^(-2) + 0 x 2^(-3) + 1 x 2^(-4) = 0.5625
Then the binary fraction with a fractional part width of 4, bin(11.1001), represents the decimal 3.5626.
The quick method for converting a binary fraction num with an N-bit width to a decimal is:
bin2dec(num) = num / 2^N
For example, the calculation process for the binary fraction bin(11.1001) is:
bin(11.1001) = bin(111001) / (2^4) = 57/16 = 3.5625.
In fact, whether a multi-bit variable represents an integer or a decimal, the Verilog compiler does not distinguish, and it is only calculated on the decimal level by humans.
Floating-Point Number Representation
A fixed-point number is a number whose decimal point position is fixed and does not change. An integer is a fixed-point number because the decimal point is always in the last position.
A floating-point number is a number whose decimal point position is not fixed. For example, 1.2 x 1.3 = 1.56, the position of the decimal point is constantly floating, hence the name floating-point number.
Of course, the position of the decimal point of a decimal can also be fixed, which is called a fixed decimal. However, this representation method will reduce the precision of the decimal. For example, if only one data bit is fixed behind the decimal point to represent the decimal, then 1.2 x 1.3 = 1.5, and some data is lost.
The general representation method of decimal floating-point numbers is:
`
7.4 Verilog Real to Integer Conversion