What return value should be besides '0' and '1' for this fun

F

fl

Guest
Hi,

I read the following code. I know that std_logic has value '0', '1', 'Z' and
'X' etc.

There are only two values are considered below. Do you think it is an
imperfect function or not?


Thanks,




FUNCTION to_integer( x : IN std_logic) RETURN integer IS
VARIABLE int: integer;
BEGIN
IF x = '0' THEN
int := 0;
ELSE
int := 1;
END IF;
RETURN int;
END;
 
On 5/22/2015 10:31 PM, fl wrote:
Hi,

I read the following code. I know that std_logic has value '0', '1', 'Z' and
'X' etc.

There are only two values are considered below. Do you think it is an
imperfect function or not?


Thanks,




FUNCTION to_integer( x : IN std_logic) RETURN integer IS
VARIABLE int: integer;
BEGIN
IF x = '0' THEN
int := 0;
ELSE
int := 1;
END IF;
RETURN int;
END;

Not sure why you say only two values are "considered". The input can be
any of a number of states and all are translated to an integer 1 except
for the input of '0' which is translated to 0. Is that what you want?
std_logic can also be 'H' or 'L' which are often equated to '1' and '0'
respectively.

--

Rick
 
On Fri, 22 May 2015 19:31:09 -0700, fl wrote:

Hi,

There are only two values are considered below. Do you think it is an
imperfect function or not?

As Rickman hints, this function is definitely erroneous in converting 'L'
to '1'.

If you are only allowed 0 and 1 as return values, there is no choice but
to resolve all the others 'U','Z' etc to either 0 or 1, and your choice
to resolve them (except for 'L') as 1 is probably as good as any.

I would recommend declaring a new Integer type (or at the very least a
subtype) restricted in range to (0 to 1) and return that type, to make it
crystal clear what this function does (and to catch unintended errors
such as passing 2 (or -2143863148) as a value to code which was only
written to handle 0,1.

-- Brian
 
On Saturday, May 23, 2015 at 2:53:43 AM UTC-7, Brian Drummond wrote:
On Fri, 22 May 2015 19:31:09 -0700, fl wrote:

Hi,

There are only two values are considered below. Do you think it is an
imperfect function or not?


As Rickman hints, this function is definitely erroneous in converting 'L'
to '1'.

If you are only allowed 0 and 1 as return values, there is no choice but
to resolve all the others 'U','Z' etc to either 0 or 1, and your choice
to resolve them (except for 'L') as 1 is probably as good as any.

I would recommend declaring a new Integer type (or at the very least a
subtype) restricted in range to (0 to 1) and return that type, to make it
crystal clear what this function does (and to catch unintended errors
such as passing 2 (or -2143863148) as a value to code which was only
written to handle 0,1.

-- Brian

Great thanks to both of you.
 

Welcome to EDABoard.com

Sponsor

Back
Top