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

problem: to_ufixed(0.483,n1)

Altera_Forum
Honored Contributor II
1,721 Views

Hi all; 

 

I want to convert 0.483 to ufixed type. Manually, I get 0000.0111 (3 downto -4) when I calculate by hand. 

But, this value is not same with the value in modelsim which is 0000.1000. I write like this; 

 

ip1<=to_ufixed(0.483,n1); 

 

What the correct value of 0.483 in ufixed? Do I calculate in wrong way?..needs help, thanks
0 Kudos
10 Replies
Altera_Forum
Honored Contributor II
439 Views

Its 0000.1000 if you round to nearest. 

the to_ufixed function does have extra options with regards to rounding and saturation. You'll have to read the function definitions in the fixed_pkg source code or user guide to set them.
0 Kudos
Altera_Forum
Honored Contributor II
439 Views

0.483 * 2^4 = 7.728, which gets rounded up to 8, which is 1000 in binary. To me it seems Modelsim is right.

0 Kudos
Altera_Forum
Honored Contributor II
439 Views

Sorry, what actually the formula to convert to ufixed? thanks for reply

0 Kudos
Altera_Forum
Honored Contributor II
439 Views

Y x 2^f, where f is the number of fractional bits.

0 Kudos
Altera_Forum
Honored Contributor II
439 Views

I try 1 example (0.25) for my better understand; 

 

0.25x2^2 = 0.5 which get rounds 1, which is 1 in binary. 

 

Do I right? thanks for reply
0 Kudos
Altera_Forum
Honored Contributor II
439 Views

0.25*2^2 = 1

0 Kudos
Altera_Forum
Honored Contributor II
439 Views

 

--- Quote Start ---  

I try 1 example (0.25) for my better understand; 

 

0.25x2^2 = 0.5 which get rounds 1, which is 1 in binary. 

 

Do I right? thanks for reply 

--- Quote End ---  

 

 

No, 0.25 x 2^2 = 1 

 

Answer is 0.01
0 Kudos
Altera_Forum
Honored Contributor II
439 Views

Sorry for the miss typing..and I think I misunderstanding the concept. 

I try another example (9.75): 

 

9.75x2^2=39 which is 100111 in binary. The answer is 1001.11 

 

Do I right? thanks for reply
0 Kudos
Altera_Forum
Honored Contributor II
439 Views

Yes this is right

0 Kudos
Altera_Forum
Honored Contributor II
439 Views

Okay I undersatand now, many thanks Tricky and Daixiwen!

0 Kudos
Reply