Problem with function

Joined
Dec 25, 2009
Messages
1
Reaction score
0
Hello

I have problems, Namely I don't know why my function isn't acting. I wanted to write function, which will execute translation of types real to std_logic_vector. I did two arrays which between them compares and result I get variable std_logic_vector but I don't know why " if(pay_extra = pay_real(i))" don't works, when pay_extra = 0.1 to 1.0.

this is function and arrays:
Code:
-----------------------------------------------------
function przelicz(var: real) return bit_vector is 
	variable mon: bit_vector(4 downto 0);
begin
	for i in pay_bit'range loop
		if(var=pay_real(i))
			then mon:= pay_bit(i);
		end if;
	end loop;
return mon;

end;
-----------------------------------------------------

type tab_pay_bit is array(positive range<>) of bit_vector(4 downto 0);
constant pay_bit : tab_pay_bit :=(
"00000",			--0.0
"00001",			--0.1
"00010",			--0.2
"00011",			--0.3
"00100",			--0.4
"00101",			--0.5
"00110",			--0.6
"00111",			--0.7
"01000",			--0.8
"01001",			--0.9
"01010",			--1.0
"01011",			--1.1
"01100",			--1.2
"01101",			--1.3
"01110",			--1.4
"01111",			--1.5
"10000",			--1.6
"10001",			--1.7
"10010",			--1.8
"10011",			--1.9
"11000"			--2.0


); 

type tab_pay_real is array(positive range<>) of  Real;
constant pay_real : tab_pay_real(1 to 21) :=(
0.0,
0.1,
0.2,
0.3,
0.4,
0.5,
0.6,
0.7,
0.8,
0.9,
1.0,
1.1,
1.2,
1.3,
1.4,
1.5,
1.6,
1.7,
1.8,
1.9,
2.0
);                 

-----------------------------------------------------
-----------------------------------------------------

and call function this way:

pay_extra_out<=To_StdLogicVector(przelicz(abs(money-pay)));
where example: money = 2.0 and pay = 1.8 ;


Thanks for help

BTW.I am apologizing for my meager language
 
Joined
Jan 29, 2009
Messages
152
Reaction score
0
I think, due to imprecision, it's usually not safe to compare reals for equality.
The usual approach is to check whether they differ by less than a (maximum allowed) delta value - what that delta value should be like depends a bit on the range of floating point values you expect to get.
 

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,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top