Search code examples
floating-pointvhdl

I need to select certain digits from a floating number. Is it possible? if yes, how?


An example: 0.6345482635429

I want to select digits from (10^-6) to (10^-9) as in select (263) and save it. I applied this in Matlab. I am trying to apply it in VHDL. The example is in decimal perspective rather than the floating point format im using.

Is it possible to convert the floating number to decimal and then select the required number/digits from the decimal format?

I am applying a 32-bit floating point format. I dont know how to select the same range of digits for a floating number in VHDL.

I did not try anything yet since i originally do not know how to do so.

Thank you!


Solution

  • It is not possible for a 32-bit floating-point object to preserve the information needed to determine the digits of arbitrary numbers with nine decimal digits (except for a degenerate floating-point format with a one-bit exponent or with a two-bit exponent and no sign bit).

    Floating-point formats represent mathematical numbers, not numerals. Especially, binary formats do not represent decimal numerals. With care, it is possible to use floating-point to compute the decimal digits that would result from various mathematics, but this requires knowledge and good design. Often, it is a bad idea, and there is a better solution. For VHDL, it is likely a very bad idea. For nine-digit numbers using 32-bit floating-point, it is impossible, as the information present in a normal 32-bit format is insufficient to preserve nine decimal digits, let alone compute with them.

    The IEEE-754 basic 32-bit binary floating-point format has 24-bit significands. 224 = 16,777,216, which is not even eight digits. If the number represented is around .634 as in your example, the high bit of the significand corresponds to 2−1, and the low bit corresponds to 2−24, which is about .0000000596046. This is not fine enough to preserve digits after the seventh decimal place.

    If you wanted to use multiple 32-bit floating-point objects to represent the number or the digits, some extended-precision techniques might work. However, it is quite likely there is a better solution, perhaps something that directly represents a string of decimal digits.