real number to 16 bit signed number

H

hari

hi
a) i want to convert a real number to 16 bit signed binary number.
the real number is 2E15-1.so how should i do it.do we have any
default packages for that.

thanks
hari
 
A

Alan Fitch

hari said:
hi
a) i want to convert a real number to 16 bit signed binary number.
the real number is 2E15-1.so how should i do it.do we have any
default packages for that.

Assuming you mean SIGNED from Numeric_std, you could use

library IEEE;
use IEEE.NUMERIC_STD.all;

....

signal S : SIGNED(15 downto 0)
signal V : STD_LOGIC_VECTOR(15 downto 0);
signal I : INTEGER range -2**15 to 2**15-1;
signal R : real;

....

S <= to_signed(INTEGER(R),16));

If you want to go to std_logic_vector, then

V <= STD_LOGIC_VECTOR(to_signed(INTEGER(R),16)));

If you meant just an integer, then they are closely related so you can
just
do

I <= INTEGER(R);


The conversion rounds.

regards

Alan

--
Alan Fitch
Consultant

DOULOS - Developing Design Know-how
VHDL * Verilog * SystemC * Perl * Tcl/Tk * Verification * Project
Services

Doulos Ltd. Church Hatch, 22 Market Place, Ringwood, Hampshire, BH24
1AW, UK
Tel: +44 (0)1425 471223 mail:
(e-mail address removed)
Fax: +44 (0)1425 471573 Web:
http://www.doulos.com

The contents of this message may contain personal views which are not
the
views of Doulos Ltd., unless specifically stated.
 
P

paris

hari said:
hi
a) i want to convert a real number to 16 bit signed binary number.
the real number is 2E15-1.so how should i do it.do we have any
default packages for that.

thanks
hari

i guess you meant 2 "to the power of" 15 minus 1 = 32768-1 am i right? if
so, then it's an integer and therefore it's trivial to convert it, but then,
the whole thing seem too trivial
 
H

hari

sorry the real number is 0.9990234375 and i want to convert it to a 16
bit binary number .can u guys help me out.
thanks
hari
 
H

hari

i have to do it in vhdl.but yes the c code should be ok.i can try
converting it to vhdl.
thanks
hariprasath
 
H

hamilton

OK,

float sample;
int answer;

answer = int( sample );


simplified would be:

if( sample < 1.0000 )
answer = 0;



Did I miss something or did you not give enough information ??
 
Joined
Jan 2, 2023
Messages
1
Reaction score
0
sorry the real number is 0.9990234375 and i want to convert it to a 16
bit binary number .can u guys help me out.
thanks
hari
You need to use method as described S <= to_signed(INTEGER(R),16));
the only catch is that you first need to convert the real data R to integer by scaling meaningfully, which means just multiply the R * 32768 first so that +1.0 and -1.0 are replaced by two highest and lowest of 16 bits. and then use that R for above formula. so in the end you will have something like S <= to_signed(INTEGER(R*32768),16)); although this step R*32768 has to be done seperately first.
i hope it helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,828
Messages
2,569,736
Members
45,518
Latest member
vlonesshirt

Latest Threads

Top