Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
10 years ago

Percentage calculation in VHDL

Hi All,

I am using MAX10 FPGA and need to calculate percentage. My data is 12 bit long. So I take the data, divide it by 100 by using the LPM_DIVIDE IP. I take the Quotient and multiply it to the desired percentage factor through LPM_MULTIPLIER. But LPM_DIVIDE IP produces Quotient and remainder. I am taking Quotient only that's why I am unable to get accurate results. So my question is that how should I use remainder to produce more accurate results. Thanks

Regards

USMAN

3 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Your 12bit data ranges from 0 to 4095, then if you divide it by 100 you simply lose the last two digits, namely 6-7 less significant bits of original data.

    A better solution would be starting with the multiplication into a 18bit register and THEN dividing by 100.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    ouh...

    first you do multiplication and store intermediate result with more than required bits , then you do division. <- this works well.

    i don't know the number range you deal with.

    ( but if it doesn't break your accuracy you can replace division by 100 = 2^2 * 25 with division by 96 = 2^5 * 3 it means that at least 5 bits of operand looses)