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


Groups > comp.lang.python > #45248

Re: Python for philosophers

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!eweka.nl!lightspeed.eweka.nl!194.134.4.91.MISMATCH!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'value,': 0.04; 'cpython': 0.05; 'say,': 0.05; 'subject:Python': 0.06; '64-bit': 0.07; 'memory.': 0.07; 'sys': 0.07; '32-bit': 0.09; 'compile-time': 0.09; 'driver,': 0.09; 'main()': 0.09; 'stack,': 0.09; 'python': 0.11; 'def': 0.12; '65536': 0.16; '754': 0.16; 'accuracy.': 0.16; 'declared': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'governed': 0.16; 'happily': 0.16; 'infinity,': 0.16; 'integer,': 0.16; 'integers.': 0.16; 'overflow.': 0.16; 'true:': 0.16; 'tweak': 0.16; 'wrote:': 0.18; 'skip:p 40': 0.19; 'stack': 0.19; 'meant': 0.20; 'seems': 0.21; 'machine': 0.22; 'import': 0.22; 'proposed': 0.22; "aren't": 0.24; 'certainly': 0.24; 'closely': 0.24; 'entries': 0.24; 'integer': 0.24; 'library,': 0.24; 'looks': 0.24; 'options': 0.25; 'script': 0.25; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; 'point': 0.28; "we'd": 0.29; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; 'that.': 0.31; 'usually': 0.31; '100000': 0.31; 'adequate': 0.31; 'cells': 0.31; 'there.': 0.32; 'probably': 0.32; 'option': 0.32; 'skip:- 30': 0.32; "we're": 0.32; 'run': 0.32; 'quite': 0.32; 'running': 0.33; 'core': 0.34; "i'd": 0.34; 'could': 0.34; "can't": 0.35; 'test': 0.35; 'but': 0.35; 'received:google.com': 0.35; '14,': 0.36; 'accuracy': 0.36; 'ram': 0.36; 'reality': 0.36; 'done': 0.36; 'performance': 0.37; 'system,': 0.38; 'needed': 0.38; 'to:addr:python-list': 0.38; 'fact': 0.38; 'rather': 0.38; 'that,': 0.38; 'recent': 0.39; 'though,': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'truly': 0.60; 'most': 0.60; 'increased': 0.61; 'numbers': 0.61; 'course': 0.61; 'simple': 0.61; 'save': 0.62; 'real': 0.63; 'skip:n 10': 0.64; 'more': 0.64; 'different': 0.65; 'size.': 0.65; 'limit': 0.70; 'consequences': 0.74; 'increasing': 0.74; 'million': 0.74; 'hey,': 0.75; 'boom.': 0.84; 'hassle': 0.84; "it'd": 0.84; 'onwards': 0.84; 'pike': 0.84; 'saying:': 0.84; 'rusi': 0.91; '2013': 0.98
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:content-type; bh=5d1Fu/H/2mDqryF+0bhfodV9D4/g7/zpFGzFOa45DiE=; b=IjO9gRdgYs7T5riEgyWNpxceh+ud0RXX+KbJEplToXai/Lj3cBahDs+pvBZSQ1Lpkn 4gmIuiQy9hTchgZz8o2wgWDjhzyRZF1ixRUn2nZDXIjBN0qYSv/R1mnPqNevToTSzcsZ 85Yyzgpd5ic62Iks8XY6dL9oyKVnBfWtiSD9Jjt+P3t4ZOVkDjXxbRuttPYqo2kBLPmr zkejctcXihLKqDBpjj9FBAsbNBEiVZSTlDxuWVD1atNC9blOAX++HZ4oByZ3xHDBL8ge WoS9W9mLY85JMrWdzoX9QUzpSb4UVpw0r3ivgOibBrMMGggqQ/gXPqCGXYc2frFhOJrY AdIA==
MIME-Version 1.0
X-Received by 10.52.122.109 with SMTP id lr13mr16331814vdb.91.1368462284634; Mon, 13 May 2013 09:24:44 -0700 (PDT)
In-Reply-To <5e2c6b41-e9af-4604-ad05-47fbea32ae15@zo5g2000pbb.googlegroups.com>
References <CAMbF=C_GF_AKO2dD_=bH6VN2GVcSbC=KtHFLw62CKq2yitcfyQ@mail.gmail.com> <mailman.1581.1368368230.3114.python-list@python.org> <avaqo7Fsj3pU1@mid.individual.net> <519052df$0$29997$c3e8da3$5496439d@news.astraweb.com> <5e2c6b41-e9af-4604-ad05-47fbea32ae15@zo5g2000pbb.googlegroups.com>
Date Tue, 14 May 2013 02:24:44 +1000
Subject Re: Python for philosophers
From Chris Angelico <rosuav@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
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.1631.1368462293.3114.python-list@python.org> (permalink)
Lines 80
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1368462293 news.xs4all.nl 15962 [2001:888:2000:d::a6]:58039
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:45248

Show key headers only | View raw


On Tue, May 14, 2013 at 12:53 AM, rusi <rustompmody@gmail.com> wrote:
> int fact(int n, int acc)
> {
>   return !n? acc : fact(n-1,acc*n);
> }
> ---------------------------------
> When I run these, the C happily keeps giving answers until a million
>
> However examined closely we find that though the C is giving answers
> its giving junk after around 12
> fact 17 is -288522240
> And 35 onwards its 0 (!!)

That'll depend on your integer size. If it's a 32-bit integer, you
can't store numbers greater than 2**31-1 (if you declared it as
'unsigned int', you could go all the way to 2**32-1). I'm sure you
could write this to use the GNU Multi-Precision library, but it'd be a
lot more complicated. However, as far as I know, the Turing machine
never promises that; its cells aren't meant to be infinite integers.

The Python script is, of course, governed by sys.setrecursionlimit().
But by adding a simple driver, we can test the real limit:

import sys

def fact(n,acc=1):
    return acc if not n else fact(n-1,n*acc)

n=2
while True:
        sys.setrecursionlimit(n+2)
        print("fact",n,"has",len(str(fact(n))),"digits")
        n*=2

On my 64-bit system, running a recent trunk build of CPython 3.4, it
can calculate 8192! but not 16384! (segfault). The limit seems to be
12772; after that, boom. Your crash-point will quite probably vary,
and I'd say there'll be compile-time options that would change that.

Of course, after playing with this in Python, I had to try out Pike.
Using the exact same code you proposed for C, but with a different
main() to save me the hassle of keying in numbers manually, I get
this:

fact 8192 has 28504 digits - 0.026 secs
fact 16384 has 61937 digits - 0.097 secs
fact 32768 has 133734 digits - 0.389 secs
fact 65536 has 287194 digits - 1.628 secs
fact 131072 has 613842 digits - 7.114 secs
fact 262144 has 1306594 digits - 31.291 secs
fact 524288 has 2771010 digits - 133.146 secs

It's still going. One core consumed, happily working on 1048576!, and
not actually using all that much memory. Hmm.... looks like the Pike
optimizer has turned this non-recursive. Okay, let's tweak it so it's
not tail-recursion-optimizable:

  return n?n*fact(n-1,1):1;

Now it bombs at 65536, saying:

Svalue stack overflow. (99624 of 100000 entries on stack, needed 256
more entries)

Hey, it's better than a segfault, which is what C would have done :)
And it's a tweakable value, though I don't know what the consequences
are of increasing it (presumably increased RAM usage for all Pike
programs).

Your final conclusion is of course correct; nothing we build can be
truly infinite. But we can certainly give some very good
approximations, if we're prepared to pay for them. The reality is,
though, that we usually do not want to pay for approximations to
infinity; why is IEEE 754 floating point so much more used than, say,
arbitrary-precision rational? Most of the time, we'd rather have good
performance and adequate accuracy than abysmal performance and perfect
accuracy. But hey, if you want to render a Mandelbrot set and zoom in
to infinity, the option IS there.

ChrisA

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


Thread

Re: Python for philosophers Citizen Kant <citizenkant@gmail.com> - 2013-05-12 16:17 +0200
  Re: Python for philosophers Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-05-12 14:30 +0000
    Re: Python for philosophers alex23 <wuwei23@gmail.com> - 2013-05-12 21:19 -0700
  Re: Python for philosophers rusi <rustompmody@gmail.com> - 2013-05-12 09:00 -0700
  Re: Python for philosophers Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2013-05-13 12:34 +1200
    Re: Python for philosophers Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-05-13 02:41 +0000
      Re: Python for philosophers rusi <rustompmody@gmail.com> - 2013-05-13 07:53 -0700
        Re: Python for philosophers Chris Angelico <rosuav@gmail.com> - 2013-05-14 02:24 +1000
          Re: Python for philosophers rusi <rustompmody@gmail.com> - 2013-05-13 11:14 -0700
            Re: Python for philosophers Chris Angelico <rosuav@gmail.com> - 2013-05-14 04:22 +1000
          Re: Python for philosophers 88888 Dihedral <dihedral88888@googlemail.com> - 2013-05-18 16:56 -0700
            Re: Python for philosophers Chris Angelico <rosuav@gmail.com> - 2013-05-19 10:04 +1000
              Re: Python for philosophers 88888 Dihedral <dihedral88888@googlemail.com> - 2013-05-18 19:30 -0700
                Re: Python for philosophers Michael Torrie <torriem@gmail.com> - 2013-05-18 22:46 -0600
    Re: Python for philosophers Schneider <js@globe.de> - 2013-05-16 16:13 +0200
  Re: Python for philosophers Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-05-13 03:25 +0000
    Re: Python for philosophers Grant Edwards <invalid@invalid.invalid> - 2013-05-13 14:33 +0000
  Re: Python for philosophers Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-05-13 07:13 +0000
    Re: Python for philosophers alex23 <wuwei23@gmail.com> - 2013-05-13 02:05 -0700
    Re: Python for philosophers Neil Cerutti <neilc@norwich.edu> - 2013-05-13 13:52 +0000

csiph-web