Programmable Devices
CPLDs, FPGAs, SoC FPGAs, Configuration, and Transceivers
20693 Discussions

fancy conversion from real to unsigned

Altera_Forum
Honored Contributor II
1,030 Views

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.
0 Kudos
5 Replies
Altera_Forum
Honored Contributor II
348 Views

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.
0 Kudos
Altera_Forum
Honored Contributor II
348 Views

Or you can do handy things like this: 

 

constant CALIBRATION_CONSTANT_LOGAIN : ufixed(-1 downto -16) := to_ufixed(CALIBRATION_CURRENT_LOGAIN, 0, 16)
0 Kudos
Altera_Forum
Honored Contributor II
348 Views

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

0 Kudos
Altera_Forum
Honored Contributor II
348 Views

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

0 Kudos
Altera_Forum
Honored Contributor II
348 Views

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?
0 Kudos
Reply