f

#### What return value should be besides '0' and '1' for this function?

```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;
```
 0
fl
5/23/2015 2:31:09 AM
comp.lang.vhdl 6430 articles. 2 followers.

3 Replies
509 Views

Similar Articles

[PageSpeed] 52

```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
```
 0
rickman
5/23/2015 2:40:06 AM
```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
```
 0
Brian
5/23/2015 9:52:30 AM
```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.
```
 0
fl
5/24/2015 1:47:32 AM

Similar Artilces:

Return value of 'each', 'current', 'next', 'end'
These array functions: 'each', 'current', 'next', 'end' They return a reference or a value. let's say I want to modify the last value of an array without iterating through all of them. I would do: <? end(\$array) ; current(\$array)="new value" ; ?> is that posible?? Thanks. Hermann.Richter@gmail.com wrote: > These array functions: 'each', 'current', 'next', 'end' > > They return a reference or a value. They return values. > let's say I want to modify the last value of an array without it...

'^=' and '~='?
Hello, What is the difference between '^=' and '~='? Thanks, Duckhye ...

'is not' or '!='
A newbie question to you; what is the difference between statements like: if x is not None: and if x != None: Without any context, which one should be preferred? IMHO, the latter is more readable. On 2014-08-18 21:35, ElChino wrote: > A newbie question to you; what is the difference between statements > like: > if x is not None: > and > if x != None: > > Without any context, which one should be preferred? > IMHO, the latter is more readable. > "x == y" tells you whether x and y refer to objects that are equal. "x is y&qu...

A function with 'and' , 'not' , 'null' , 'car' and 'cdr'
What's this ? (defun enigma (x) (and (not (null x)) (or (null (car x)) (enigma (cdr x))))) "I suppose I should learn Lisp, but it seems so foreign." - Paul Graham, Nov 1983 On Wed, Oct 07 2015, CAI GENGYANG wrote: > What's this ? > > > (defun enigma (x) > (and (not (null x)) > (or (null (car x)) > (enigma (cdr x))))) Bad taste? It returns T if the list X contains nil as an element. It would be clearer to write (some #'null x). Helmut CAI GENGYANG ...

``awk '!a[\$0]++' file'' and ``awk '{if(!(\$0 in rec)) {rec[\$0]=1; print \$0;}}' file ''
Hi all, Currently, I'm reading the book on awk programming language, but meet the following two examples which I cann't understand so well: awk '!a[\$0]++' file and awk '{if(!(\$0 in rec)) {rec[\$0]=1; print \$0;}}' file Could someone here please give me some hints or explanations on the logic of above codes? Regards -- ..: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :. In article <meuejm\$gsf\$1@aspen.stu.neva.ru>, Hongyi Zhao <hongyi.zhao@gmail.com> wrote: > Hi all, > > Currently, I'm reading the bo...

'11' + '1' is '111'?
'11' + '1' == '111' is well known. but it suprises me '11'+'1' IS '111'. Why? Obviously they are two differnt object. Is this special feature of imutable object? On Thu, Oct 29, 2009 at 5:43 PM, metal <metal29a@gmail.com> wrote: > '11' + '1' == '111' is well known. > > but it suprises me '11'+'1' IS '111'. > > Why? Obviously they are two differnt object. > > Is this special feature of imutable object? It's an implementation detail used to optimize performance. CPy...

'''''''''''''The Running Update/Append Queries Using VBA code Ordeal''''''''''''''
Hello fellow programmers, I am trying to run an append/update query from code, a command button on a form initiates the queries. the format i am using is; _____________________________________________________ SELECT "criteria" FROM "criteria" WHERE "criteria" UPDATE/APPEND "field selections" RecordSource "qryExample" = above text strings" _______________________________________________________________________ When i am running a SELECT query in this manner it works fine with no problems, and accepts the values of specified linked for...

Basic array stuff... Why [0] & ['key'] and [1] & ['value'] 'duplication'?
I've been studying this for hours, searching the www & usenet, and still can't figure out why 'each' returns an array of four key/value pairs, when it looks like just two pairs would suffice... That is, it looks to me like [0] & ['key'] are the same, and also [1] & ['value'], so what's the point of duplicating them? I'm assuming that in fact there are serious differences between [0] and ['key'] and between [1] and [value], but I haven't a clue what that might be. Any hints, URLs, explanaitons, etc. most welcome! Thank you. ...

'''''''''''''The Running Update/Append Queries Using VBA code Ordeal'''''''''''''' #2
Hi, Thanks for ur help there HJ. I know how to do the tasks you specified there. I would like for the update query to use field values from some of the fields on the form (frmInvoices) such as InvoiceNumber, DateFrom, DateTo. My problem is that an append/update query can't find the values in the open Form (frmInvoices) when I specify them as; [Forms]![frmInvoices]![InvoiceNumber] a select query has no problem finding the field values on a form. please help. Aaron Hi Aaron, Could you post the entire code that you are having trouble with? Now it is not possible to see what goes wron...

if str_mo not in ('','.') and str_da not in ('','.') and str_yy not in ('','.') Any shorter ?
Hi, there. =20 I'm just curious if it ever dawned on anybody how to abbreviate this line : if str_mo not in ('','.') and str_da not in ('','.') and str_yy not in ('','.')=20 =20 Igor Kurbeko Clinical Programmer Analyst 678 336 4328 ikurbeko@atherogenics.com =20 no brain no pain =20 how about: if not (str_mo in ('','.') or str_da in ('','.') or str_yy in ('','.')) OR if not (missing(str_mo) or missing(str_da) or missing(str_yy)) Eric On 22 Oct 03 21:13:37 GMT, ikurbeko@ATHER...

shouldn't 'string'.find('ugh') return 0, not -1 ?
the subject pretty much says it all. if I check a string for for a substring, and this substring isn't found, should't the .find method return 0 rather than -1? this breaks the if check.find('something'): do(somethingElse) idiom, which is a bit of a pity I think. cheers, -jelle jelle <jelleferinga@gmail.com> writes: > the subject pretty much says it all. > if I check a string for for a substring, and this substring isn't found, > should't the .find method return 0 rather than -1? How would you treat the case of 'something' being at ...

{ '0':'c->c->a' ,'1':'a->b->a' .........}
Hi, have anybody a hint , how i get a dict from non unique id's and their different related values. Thanks for advance Chris ###random data # a=range(10)*3 def seqelem(): i=random.randint(0,2) elem=['a','b','c'][i] return elem s=[seqelem() for t in range(30)] print zip(a,s) ## favored result: { '0':'c->c->a' ,'1':'a->b->a' .........} Hi Chris, I may have time to look at the rest of your code later. For now I just want to comment on one line: On Nov 7, 12:24=A0pm, chris <o...