zsh-users
 help / color / mirror / code / Atom feed
* Airthmetic confusion...
@ 2005-05-16 17:55 Meino Christian Cramer
  2005-05-16 20:43 ` Matthias Berndt
  0 siblings, 1 reply; 11+ messages in thread
From: Meino Christian Cramer @ 2005-05-16 17:55 UTC (permalink / raw)
  Cc: zsh-users

Hi,

 I got a little confused here:

   (( a = 1 + 2 ))

 works fine, but 
  

   (( b = 1 )) ; (( a = 1 - b ))

 does not  but

   (( a = 1 + b )) 

 does. Seems I cannot asign "0" to a variable.

 But how can it be possible to assure, that no calculation in 
 a script will never result in zero ?

 Keep zshing!
 Meino

		



   


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Airthmetic confusion...
  2005-05-16 17:55 Airthmetic confusion Meino Christian Cramer
@ 2005-05-16 20:43 ` Matthias Berndt
  2005-05-16 20:59   ` Mike Hernandez
  2005-05-17  3:14   ` Meino Christian Cramer
  0 siblings, 2 replies; 11+ messages in thread
From: Matthias Berndt @ 2005-05-16 20:43 UTC (permalink / raw)
  To: zsh-users; +Cc: Meino Christian Cramer

On Mon, 16 May 2005 19:55:29 +0200 (CEST)
Meino Christian Cramer <Meino.Cramer@gmx.de> wrote:

> Hi,
> 
>  I got a little confused here:
> 
>    (( a = 1 + 2 ))
> 
>  works fine, but 
>   
> 
>    (( b = 1 )) ; (( a = 1 - b ))
> 
>  does not  but
> 
>    (( a = 1 + b )) 
> 
>  does.

I don't know your problem, but these expression works fine here. Did you
make something special before or in your startup-scripts?

Matthias Berndt


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Airthmetic confusion...
  2005-05-16 20:43 ` Matthias Berndt
@ 2005-05-16 20:59   ` Mike Hernandez
  2005-05-16 21:46     ` Dan Nelson
                       ` (2 more replies)
  2005-05-17  3:14   ` Meino Christian Cramer
  1 sibling, 3 replies; 11+ messages in thread
From: Mike Hernandez @ 2005-05-16 20:59 UTC (permalink / raw)
  To: Matthias Berndt, zsh-users

On 5/16/05, Matthias Berndt <Berndt.Matthias@gmx.de> wrote:
> I don't know your problem, but these expression works fine here. Did you
> make something special before or in your startup-scripts?

Actually what happens is that the operation and assignment are
performed but the return code of the expression evaluates to 1, which
is an error. I tried on my machine and get the same results:

(mike@mhernandez)(24/pts)(04:45pm:05/16/05)-
(%:~)- (( x = 0 ))
(mike@mhernandez)(25/pts)(04:46pm:05/16/05)-
(%:~)- echo $?
1
(mike@mhernandez)(26/pts)(04:46pm:05/16/05)-
(%:~)- echo $x
0

If you set a variable to any integer other than 0, positive or
negative, the return code is 0, which is successful exit, for example:

(mike@mhernandez)(30/pts)(04:53pm:05/16/05)-
(%:~)- (( r = -5 ))
(mike@mhernandez)(31/pts)(04:53pm:05/16/05)-
(%:~)- echo $?
0

This occurs with a regular assignment as above, or if the assignment
contains some variables (as in x = b - 4, if b was 4 the return code
would be 1, yet the math is done and x is set to equal 0).

There must be a reason why assigning 0 to a variable is considered an
error (a reason which I could speculate about, but don't know for
sure).

Mike


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Airthmetic confusion...
  2005-05-16 20:59   ` Mike Hernandez
@ 2005-05-16 21:46     ` Dan Nelson
  2005-05-17  1:19       ` Mike Hernandez
  2005-05-16 23:39     ` Matthias Berndt
  2005-05-17  3:13     ` Meino Christian Cramer
  2 siblings, 1 reply; 11+ messages in thread
From: Dan Nelson @ 2005-05-16 21:46 UTC (permalink / raw)
  To: Mike Hernandez; +Cc: Matthias Berndt, zsh-users

In the last episode (May 16), Mike Hernandez said:
> If you set a variable to any integer other than 0, positive or
> negative, the return code is 0, which is successful exit, for example:
> 
> (mike@mhernandez)(30/pts)(04:53pm:05/16/05)-
> (%:~)- (( r = -5 ))
> (mike@mhernandez)(31/pts)(04:53pm:05/16/05)-
> (%:~)- echo $?
> 0
> 
> This occurs with a regular assignment as above, or if the assignment
> contains some variables (as in x = b - 4, if b was 4 the return code
> would be 1, yet the math is done and x is set to equal 0).
> 
> There must be a reason why assigning 0 to a variable is considered an
> error (a reason which I could speculate about, but don't know for
> sure).

That's because within an arithmentic context, 0 is false and 1 is true,
so the following are equivalent:

true && echo true
(( 1 )) && echo true
(( 2 > 1 )) && echo true   # since $(( 2 > 1 )) evaluates to 1

An assignment expression evaluates to the value of the variable, so 
(( a=0 )) is the same as (( 0 )) .

-- 
	Dan Nelson
	dnelson@allantgroup.com


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Airthmetic confusion...
  2005-05-16 20:59   ` Mike Hernandez
  2005-05-16 21:46     ` Dan Nelson
@ 2005-05-16 23:39     ` Matthias Berndt
  2005-05-17  3:13     ` Meino Christian Cramer
  2 siblings, 0 replies; 11+ messages in thread
From: Matthias Berndt @ 2005-05-16 23:39 UTC (permalink / raw)
  To: Mike Hernandez; +Cc: zsh-users

On Mon, 16 May 2005 16:59:27 -0400
Mike Hernandez <sequethin@gmail.com> wrote:

> Actually what happens is that the operation and assignment are
> performed but the return code of the expression evaluates to 1, which
> is an error. I tried on my machine and get the same results:
> 
> (mike@mhernandez)(24/pts)(04:45pm:05/16/05)-
> (%:~)- (( x = 0 ))
> (mike@mhernandez)(25/pts)(04:46pm:05/16/05)-
> (%:~)- echo $?
> 1
> (mike@mhernandez)(26/pts)(04:46pm:05/16/05)-
> (%:~)- echo $x
> 0

I hope, I don't missunderstood ... the first posting, but 'echo $x' is
'0'. So the value of 'x' is calculated correctly, but the returncode is
false.
 
> There must be a reason why assigning 0 to a variable is considered an
> error (a reason which I could speculate about, but don't know for
> sure).

Are you sure there is a reason or isn't it probably a bug?


Matthias Berndt


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Airthmetic confusion...
  2005-05-16 21:46     ` Dan Nelson
@ 2005-05-17  1:19       ` Mike Hernandez
  0 siblings, 0 replies; 11+ messages in thread
From: Mike Hernandez @ 2005-05-17  1:19 UTC (permalink / raw)
  To: Dan Nelson, zsh-users

On 5/16/05, Dan Nelson <dnelson@allantgroup.com> wrote:
> That's because within an arithmentic context, 0 is false and 1 is true,

Ok so you're saying that since 0 is false in the arithmetic context,
it gets translated by the shell to be 1, because 1 is false in the
shell context? That makes sense... but it does make things very
confusing ;) True translates to true, and false translates to false,
which is totally sound and logical... but the fact that 1 is true in
one context and false in another makes it seem that true is translated
into false *sigh*. I had to read this email 10 times before I sent it
to make sure I even typed what I meant ;)


Mike


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Airthmetic confusion...
  2005-05-16 20:59   ` Mike Hernandez
  2005-05-16 21:46     ` Dan Nelson
  2005-05-16 23:39     ` Matthias Berndt
@ 2005-05-17  3:13     ` Meino Christian Cramer
  2005-05-17  3:48       ` Bart Schaefer
  2 siblings, 1 reply; 11+ messages in thread
From: Meino Christian Cramer @ 2005-05-17  3:13 UTC (permalink / raw)
  To: sequethin; +Cc: Berndt.Matthias, zsh-users

From: Mike Hernandez <sequethin@gmail.com>
Subject: Re: Airthmetic confusion...
Date: Mon, 16 May 2005 16:59:27 -0400

Hi,

 I see that "0" is false and !0 is true...but...we
 are doing arithmetic and not logical evaluation.

 Suppose the following situation:
 There is a script which first line set 
 
		set -e
 
 (exit on error), because your script do something not-undoable.
 Inside the script there is a claculation, which has to do correct
 thing in the sense of for example not accessing inexistant variables
 or not accessing arrays out of bounds.

 The calculations are of non-trivial nature (that's why we let the
 computer do such boring tidy things ;)

 Now a calculation returns "0" as its result and BANG! the script
 exits.

 ?

 Not what was intended, I think.

 As a sideffect a previously existant variable become inexistant by
 assigning a "0"? Not very logical I think.... :O)

 Arithmetic evaluation should be sperated from logical evaluation.


 Happy zshing!
 Meino





 
 


> On 5/16/05, Matthias Berndt <Berndt.Matthias@gmx.de> wrote:
> > I don't know your problem, but these expression works fine here. Did you
> > make something special before or in your startup-scripts?
> 
> Actually what happens is that the operation and assignment are
> performed but the return code of the expression evaluates to 1, which
> is an error. I tried on my machine and get the same results:
> 
> (mike@mhernandez)(24/pts)(04:45pm:05/16/05)-
> (%:~)- (( x = 0 ))
> (mike@mhernandez)(25/pts)(04:46pm:05/16/05)-
> (%:~)- echo $?
> 1
> (mike@mhernandez)(26/pts)(04:46pm:05/16/05)-
> (%:~)- echo $x
> 0
> 
> If you set a variable to any integer other than 0, positive or
> negative, the return code is 0, which is successful exit, for example:
> 
> (mike@mhernandez)(30/pts)(04:53pm:05/16/05)-
> (%:~)- (( r = -5 ))
> (mike@mhernandez)(31/pts)(04:53pm:05/16/05)-
> (%:~)- echo $?
> 0
> 
> This occurs with a regular assignment as above, or if the assignment
> contains some variables (as in x = b - 4, if b was 4 the return code
> would be 1, yet the math is done and x is set to equal 0).
> 
> There must be a reason why assigning 0 to a variable is considered an
> error (a reason which I could speculate about, but don't know for
> sure).
> 
> Mike
> 


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Airthmetic confusion...
  2005-05-16 20:43 ` Matthias Berndt
  2005-05-16 20:59   ` Mike Hernandez
@ 2005-05-17  3:14   ` Meino Christian Cramer
  1 sibling, 0 replies; 11+ messages in thread
From: Meino Christian Cramer @ 2005-05-17  3:14 UTC (permalink / raw)
  To: Berndt.Matthias; +Cc: zsh-users

From: Matthias Berndt <Berndt.Matthias@gmx.de>
Subject: Re: Airthmetic confusion...
Date: Mon, 16 May 2005 22:43:32 +0200

Hi Matthias !

 ...check the return codes after each expression...

 Happy zshing!
 Meino


> On Mon, 16 May 2005 19:55:29 +0200 (CEST)
> Meino Christian Cramer <Meino.Cramer@gmx.de> wrote:
> 
> > Hi,
> > 
> >  I got a little confused here:
> > 
> >    (( a = 1 + 2 ))
> > 
> >  works fine, but 
> >   
> > 
> >    (( b = 1 )) ; (( a = 1 - b ))
> > 
> >  does not  but
> > 
> >    (( a = 1 + b )) 
> > 
> >  does.
> 
> I don't know your problem, but these expression works fine here. Did you
> make something special before or in your startup-scripts?
> 
> Matthias Berndt
> 


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Airthmetic confusion...
  2005-05-17  3:13     ` Meino Christian Cramer
@ 2005-05-17  3:48       ` Bart Schaefer
  2005-05-17 11:26         ` Mike Hernandez
  2005-05-17 14:57         ` Meino Christian Cramer
  0 siblings, 2 replies; 11+ messages in thread
From: Bart Schaefer @ 2005-05-17  3:48 UTC (permalink / raw)
  To: zsh-users

On May 17,  5:13am, Meino Christian Cramer wrote:
}
}  I see that "0" is false and !0 is true...but...we
}  are doing arithmetic and not logical evaluation.

No, that's not true.

First of all, the closest thing to "logical" evaluation that the
shell does is the &&, ||, etc., subexpressions WITHIN arithmetic
expressions.  The && and || constructs that test process success
or failure are NOT "logical" operators -- for one thing, they do
not obey the usual Boolean precedence rules.  "set -e" is not a
"logical" comparison by any stretch.

Second, when you write (( expression )) then you are explicitly
instructing the shell to do BOTH arithmetic evaluation AND shell
expression success/failure.  That's what the double parens MEAN,
when not preceded by a dollar sign.

If you want ONLY arithmetic evaluation, use $(( expression )),
and apply the colon command if necessary, e.g.

    : $(( x = a - b ))

or

    x=$(( a - b ))

Those are both successful shell expressions that result in zero-
value assignments to x.

As with any other programming language, you have to write exactly
what you mean, and you have to follow the language's semantic rules,
not the rules as you think they should be.

}  As a sideffect a previously existant variable become inexistant by
}  assigning a "0"?

What makes you believe that's happening?  Assignments can only create
variables, they can't unset them.


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Airthmetic confusion...
  2005-05-17  3:48       ` Bart Schaefer
@ 2005-05-17 11:26         ` Mike Hernandez
  2005-05-17 14:57         ` Meino Christian Cramer
  1 sibling, 0 replies; 11+ messages in thread
From: Mike Hernandez @ 2005-05-17 11:26 UTC (permalink / raw)
  To: Bart Schaefer, zsh-users

On 5/16/05, Bart Schaefer <schaefer@brasslantern.com> wrote:
> If you want ONLY arithmetic evaluation, use $(( expression )),
> and apply the colon command if necessary, e.g.
> 
>     : $(( x = a - b ))
> 
> or
> 
>     x=$(( a - b ))
> 
> Those are both successful shell expressions that result in zero-
> value assignments to x.
> 

Thanks for shedding light on this Bart! I was going crazy trying to
figure this out :)

Mike


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Airthmetic confusion...
  2005-05-17  3:48       ` Bart Schaefer
  2005-05-17 11:26         ` Mike Hernandez
@ 2005-05-17 14:57         ` Meino Christian Cramer
  1 sibling, 0 replies; 11+ messages in thread
From: Meino Christian Cramer @ 2005-05-17 14:57 UTC (permalink / raw)
  To: schaefer; +Cc: zsh-users

From: Bart Schaefer <schaefer@brasslantern.com>
Subject: Re: Airthmetic confusion...
Date: Tue, 17 May 2005 03:48:52 +0000

Thanks a lot for your explicit explanations.

Meino


> On May 17,  5:13am, Meino Christian Cramer wrote:
> }
> }  I see that "0" is false and !0 is true...but...we
> }  are doing arithmetic and not logical evaluation.
> 
> No, that's not true.
> 
> First of all, the closest thing to "logical" evaluation that the
> shell does is the &&, ||, etc., subexpressions WITHIN arithmetic
> expressions.  The && and || constructs that test process success
> or failure are NOT "logical" operators -- for one thing, they do
> not obey the usual Boolean precedence rules.  "set -e" is not a
> "logical" comparison by any stretch.
> 
> Second, when you write (( expression )) then you are explicitly
> instructing the shell to do BOTH arithmetic evaluation AND shell
> expression success/failure.  That's what the double parens MEAN,
> when not preceded by a dollar sign.
> 
> If you want ONLY arithmetic evaluation, use $(( expression )),
> and apply the colon command if necessary, e.g.
> 
>     : $(( x = a - b ))
> 
> or
> 
>     x=$(( a - b ))
> 
> Those are both successful shell expressions that result in zero-
> value assignments to x.
> 
> As with any other programming language, you have to write exactly
> what you mean, and you have to follow the language's semantic rules,
> not the rules as you think they should be.
> 
> }  As a sideffect a previously existant variable become inexistant by
> }  assigning a "0"?
> 
> What makes you believe that's happening?  Assignments can only create
> variables, they can't unset them.
> 


^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2005-05-17 14:57 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-05-16 17:55 Airthmetic confusion Meino Christian Cramer
2005-05-16 20:43 ` Matthias Berndt
2005-05-16 20:59   ` Mike Hernandez
2005-05-16 21:46     ` Dan Nelson
2005-05-17  1:19       ` Mike Hernandez
2005-05-16 23:39     ` Matthias Berndt
2005-05-17  3:13     ` Meino Christian Cramer
2005-05-17  3:48       ` Bart Schaefer
2005-05-17 11:26         ` Mike Hernandez
2005-05-17 14:57         ` Meino Christian Cramer
2005-05-17  3:14   ` Meino Christian Cramer

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).