Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Multi-dimensional list initialization

Reply
Thread Tools

Multi-dimensional list initialization

 
 
Mark Lawrence
Guest
Posts: n/a
 
      11-09-2012
On 09/11/2012 06:37, Steven D'Aprano wrote:
> On Fri, 09 Nov 2012 17:07:09 +1100, Chris Angelico wrote:
>
>> On Fri, Nov 9, 2012 at 12:39 PM, Mark Lawrence <(E-Mail Removed)>
>> wrote:
>>> On 07/11/2012 01:55, Steven D'Aprano wrote:
>>>>
>>>>
>>>> Who knows? Who cares? Nobody does:
>>>>
>>>> n -= n
>>>>
>>>>
>>> But I've seen this scattered through code:
>>>
>>> x := x - x - x

>>
>> Can you enlighten us as to how this is better than either:
>> x := -x
>> or
>> x := 0 - x
>> ? I'm not seeing it.

>
> I'm hoping that Mark intended it as an example of crappy code he has
> spotted in some other language rather than a counter-example of something
> you would do.


Correct, CORAL 66 and pointed out to me by a colleague when another team
member had resigned.

>
> To be pedantic... there may very well be some (rare) cases where you
> actually do want x -= x rather than just x = 0. Consider the case where x
> could be an INF or NAN. Then x -= x should give x = NAN rather than zero.
> That may be desirable in some cases.


Interesting what comes up when we get chatting here. I hope we don't
get punished for going off topic

>
> At the very least, the compiler should NOT optimize away x = x - x to
> x = 0 if x could be a float, complex or Decimal.
>


X was an int so almost certainly optimised away by the SDL compiler on
VMS of 1986 or 1987.

--
Cheers.

Mark Lawrence.

 
Reply With Quote
 
 
 
 
rusi
Guest
Posts: n/a
 
      11-09-2012
On Nov 9, 11:37*am, Steven D'Aprano <steve
(E-Mail Removed)> wrote:
> On Fri, 09 Nov 2012 17:07:09 +1100, Chris Angelico wrote:
> > On Fri, Nov 9, 2012 at 12:39 PM, Mark Lawrence <(E-Mail Removed)>
> > wrote:
> >> On 07/11/2012 01:55, Steven D'Aprano wrote:

>
> >>> Who knows? Who cares? Nobody does:

>
> >>> n -= n

>
> >> But I've seen this scattered through code:

>
> >> x := x - x - x

>
> > Can you enlighten us as to how this is better than either:
> > *x := -x
> > or
> > *x := 0 - x
> > ? I'm not seeing it.

>
> I'm hoping that Mark intended it as an example of crappy code he has
> spotted in some other language rather than a counter-example of something
> you would do.
>
> To be pedantic... there may very well be some (rare) cases where you
> actually do want x -= x rather than just x = 0. Consider the case where x
> could be an INF or NAN. Then x -= x should give x = NAN rather than zero.
> That may be desirable in some cases.


In x86 assembler
mov ax, 0
is 4 bytes
sub ax, ax
is 2
and therefore better (at least for those brought up on Peter Norton);
the most common being
xor ax, ax
 
Reply With Quote
 
 
 
 
Chris Angelico
Guest
Posts: n/a
 
      11-09-2012
On Sat, Nov 10, 2012 at 2:05 AM, rusi <(E-Mail Removed)> wrote:
> In x86 assembler
> mov ax, 0
> is 4 bytes


Three bytes actually, B8 00 00 if my memory hasn't failed me. BA for
DX, B9 ought to be BX and BB CX, I think. But yes, the xor or sub is
two bytes and one clock.

ChrisA
 
Reply With Quote
 
Dennis Lee Bieber
Guest
Posts: n/a
 
      11-09-2012
On Fri, 9 Nov 2012 17:07:09 +1100, Chris Angelico <(E-Mail Removed)>
declaimed the following in gmane.comp.python.general:

> On Fri, Nov 9, 2012 at 12:39 PM, Mark Lawrence <(E-Mail Removed)> wrote:
> > On 07/11/2012 01:55, Steven D'Aprano wrote:
> >>
> >>
> >> Who knows? Who cares? Nobody does:
> >>
> >> n -= n
> >>

> >
> > But I've seen this scattered through code:
> >
> > x := x - x - x

>
> Can you enlighten us as to how this is better than either:
> x := -x
> or
> x := 0 - x


Of course, if one has a language that, for some reason, evaluates
right-to-left (APL, anyone), then

x := x - x - x

becomes

x := x - 0

<G>

--
Wulfraed Dennis Lee Bieber AF6VN
http://www.velocityreviews.com/forums/(E-Mail Removed) HTTP://wlfraed.home.netcom.com/

 
Reply With Quote
 
Prasad, Ramit
Guest
Posts: n/a
 
      11-09-2012
Dennis Lee Bieber wrote:

>
> On Fri, 9 Nov 2012 17:07:09 +1100, Chris Angelico <(E-Mail Removed)>
> declaimed the following in gmane.comp.python.general:
>

> > On Fri, Nov 9, 2012 at 12:39 PM, Mark Lawrence <(E-Mail Removed)> wrote:

> > > On 07/11/2012 01:55, Steven D'Aprano wrote:
> > >>
> > >>
> > >> Who knows? Who cares? Nobody does:
> > >>
> > >> n -= n
> > >>
> > >
> > > But I've seen this scattered through code:
> > >
> > > x := x - x - x

> >
> > Can you enlighten us as to how this is better than either:
> > x := -x
> > or
> > x:= 0 - x

>
> Of course, if one has a language that, for somereason, evaluates
> right-to-left (APL, anyone), then
>
> x := x - x - x
>
> becomes
>
> x := x - 0
>


Is that not the same as x:=-x?


~Ramit


This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.
 
Reply With Quote
 
Ethan Furman
Guest
Posts: n/a
 
      11-09-2012
Prasad, Ramit wrote:
> Dennis Lee Bieber wrote:
>> Of course, if one has a language that, for some reason, evaluates
>> right-to-left (APL, anyone), then
>>
>> x := x - x - x
>>
>> becomes
>>
>> x := x - 0

>
> Is that not the same as x:=-x?


No, its the same as 'x = x'.

~Ethan~
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
initialization of array as a member using the initialization list aaragon C++ 2 11-02-2008 04:57 PM
array initialization in initialization list. toton C++ 5 09-28-2006 05:13 PM
Initialization of non-integral type in initialization list anongroupaccount@googlemail.com C++ 6 12-11-2005 09:51 PM
Initialization via ctor vs. initialization via assignment Matthias Kaeppler C++ 2 07-18-2005 04:25 PM
Default Initialization Vs. Value Initialization JKop C++ 10 09-22-2004 07:26 PM



Advertisments