Forum Discussion

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

fancy conversion from real to unsigned

Hi Experts,

could someone give me a hint to make conversion: having a float number:

CALIBRATION_CURRENT_LOGAIN : real:= 0.89552238805970152;

i wanted to convert it to unsigned(15 downto 0) (declared as UWord) as:

constant CALIBRATION_CONSTANT_LOGAIN : UWord := TO_UNSIGNED(integer(CALIBRATION_CURRENT_LOGAIN*real(2**15)),UWord'length);

for some funny reasons I get unsigned value as "70F0" instead of "72A0" as it is supposed to be. any ideas where the error comes from?

thanks

d.

5 Replies

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

    Nope. cant see any problems there.

    But may I suggest you look into the new IEEE float_fix_pkg. It means you can start using things like:

    constant CALIBRATION_CONSTANT_LOGAIN : ufixed(-1 downto -16);

    Meaning you dont need to keep putting in annoying Bias multiplies in.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Or you can do handy things like this:

    constant CALIBRATION_CONSTANT_LOGAIN : ufixed(-1 downto -16) := to_ufixed(CALIBRATION_CURRENT_LOGAIN, 0, 16)
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    I never experienced Quartus issues with real numbers. Did you try a number with less digits?

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

    actually this is simulated first in modelsim, then implemented in quartus. both of them give exactly the same wrong value :) thanks for hints using the new type. I did not know about it is it a vhdl2000 standard? if so i'd rather avoid as all stuff is 93 standard

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

    Its part of the 2008 standard - but you can compile and use the package fine in Quartus. You just import it as part of your project or an external library. Get it from here (these are '93 compliant version of the VHDL - 2008 files) :

    http://www.eda.org/fphdl/

    Given you had a problem in modelsim, Im guessing there is problem elsewhere. I tried the code you posted exactly as is in modelsim and it worked fine. Are you sure you havent done something like declare the constant you posted in a package then declared it again in the source file, but with the new "wrong" value?