Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #24894

Re: 2 + 2 = 5

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'output': 0.04; 'sys': 0.05; 'compile-time': 0.07; 'computed': 0.07; 'subject: + ': 0.07; 'python': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'terry': 0.09; 'url:github': 0.09; '(must': 0.16; 'idle,': 0.16; 'message- id:@dough.gmane.org': 0.16; 'reason.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'wrote:': 0.17; 'jan': 0.18; 'input': 0.18; 'import': 0.21; 'constant': 0.22; 'ctypes': 0.22; 'this:': 0.23; 'paul': 0.24; 'script': 0.24; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'values': 0.26; 'guess': 0.27; 'header:X-Complaints-To:1': 0.28; 'run': 0.28; 'running.': 0.29; "i'm": 0.29; 'maybe': 0.29; 'folder': 0.30; 'print': 0.32; 'to:addr:python-list': 0.33; 'version': 0.34; 'entered': 0.34; 'false': 0.35; 'skip:f 40': 0.35; 'pm,': 0.35; 'received:org': 0.36; 'michael': 0.36; 'author': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'prompt': 0.78; 'heh.': 0.84; 'received:fios.verizon.net': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: 2 + 2 = 5
Date Wed, 04 Jul 2012 23:38:17 -0400
References <7x7guj2u8a.fsf@ruckus.brouhaha.com> <op.wgxvkeuvg7njmm@michael-think>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host pool-74-109-121-73.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:13.0) Gecko/20120614 Thunderbird/13.0.1
In-Reply-To <op.wgxvkeuvg7njmm@michael-think>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1808.1341459524.4697.python-list@python.org> (permalink)
Lines 60
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1341459524 news.xs4all.nl 6909 [2001:888:2000:d::a6]:60309
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:24894

Show key headers only | View raw


On 7/4/2012 4:37 PM, Michael Ross wrote:
> Am 04.07.2012, 21:37 Uhr, schrieb Paul Rubin <phr-2012@nightsong.com>:
>
>> I just came across this (https://gist.github.com/1208215):
>>
>>     import sys
>>     import ctypes
>>     pyint_p = ctypes.POINTER(ctypes.c_byte*sys.getsizeof(5))
>>     five = ctypes.cast(id(5), pyint_p)
>>     print(2 + 2 == 5) # False
>>     five.contents[five.contents[:].index(5)] = 4
>>     print(2 + 2 == 5) # True (must be sufficiently large values of 2
>> there...)
>>
>> Heh.  The author is apparently anonymous, I guess for good reason.
>
>
> Neat.
>
> Playing with it, i'm wondering:
>
>
> This:
>
>      import sys
>      import ctypes
>      pyint_p = ctypes.POINTER(ctypes.c_byte*sys.getsizeof(5))
>      five = ctypes.cast(id(5), pyint_p)
>      five.contents[five.contents[:].index(5)] = 4
>
>      print ( 2 + 2 == 5 )
>      print 5
>      print 5 - 2
>
> put into a script and run prints:
>
>      True
>      4
>      3

The compile-time optimizer computed the contant 5-2 in C.

> while entered at the python prompt it prints:
>
>      True
>      4
>      2

It must not run for interactive input with the undisclosed version you 
are running.

If I run the script in 3.3 Idle, I get the same output you got. If I 
then enter '5-2' interactively, I still get 3. Maybe the constant folder 
is always on now.

-- 
Terry Jan Reedy


Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Re: 2 + 2 = 5 Terry Reedy <tjreedy@udel.edu> - 2012-07-04 23:38 -0400
  Re: 2 + 2 = 5 Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-07-05 05:32 +0000
    Re: 2 + 2 = 5 Hans Mulder <hansmu@xs4all.nl> - 2012-07-05 15:57 +0200
      Re: 2 + 2 = 5 Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-07-05 16:16 +0000

csiph-web