Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: 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; 'root': 0.05; 'float': 0.07; 'reason,': 0.07; 'subject:Question': 0.07; '32-bit': 0.09; 'integers': 0.09; 'line:': 0.09; 'subject:number': 0.09; 'thrown': 0.09; 'python': 0.11; 'def': 0.12; '2):': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'precision.': 0.16; 'root:': 0.16; 'storing': 0.16; 'true:': 0.16; 'underlying': 0.16; 'wrote:': 0.18; "python's": 0.19; '(the': 0.22; 'code,': 0.22; 'aug': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'integer': 0.24; 'certain': 0.27; 'header:In-Reply- To:1': 0.27; 'function': 0.29; 'chris': 0.29; "doesn't": 0.30; 'largest': 0.30; "i'm": 0.30; '"",': 0.31; '13,': 0.31; '>>>>': 0.31; 'file': 0.32; 'running': 0.33; '(most': 0.33; 'subject:with': 0.35; 'convert': 0.35; 'test': 0.35; 'too': 0.37; 'being': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'recent': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'break': 0.61; 'range': 0.61; "you'll": 0.62; 'telling': 0.64; 'header:Reply- To:1': 0.67; 'exceed': 0.68; 'reply-to:no real name:2**0': 0.71; 'square': 0.74; 'built,': 0.84; 'reply-to:addr:python.org': 0.84; '2013': 0.98 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=bL0vfpOZ c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=0kkAYlmtguIA:10 a=UelSH6hkgPwA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=MC6yNK4giToA:10 a=pGLkceISAAAA:8 a=PpNCNzaWfwDcq_z9xiMA:9 a=ewXi6qsIcreUOujP:21 a=Wb5PcjVy1qws715G:21 a=wPNLvfGTeEIA:10 a=MSl-tDqOz04A:10 X-AUTH: mrabarnett:2500 Date: Tue, 13 Aug 2013 16:33:54 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/20130801 Thunderbird/17.0.8 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Question about function failing with large number References: <520A2295.5040605@gmail.com> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: python-list@python.org List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 44 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1376408037 news.xs4all.nl 15913 [2001:888:2000:d::a6]:35369 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:52462 On 13/08/2013 13:42, Chris Angelico wrote: > On Tue, Aug 13, 2013 at 1:12 PM, Anthony Papillion wrote: >> So I'm using the function below to test a large (617 digit) number for >> primality. For some reason, when I execute the code, I get an error >> telling me: >> >> OverflowError: long int too large to convert to float >> >> The error is being thrown on this line: >> >> for x in range(3, int(n**0.5)+1, 2): > > Python's integers are unbounded, storing arbitrary precision. Python's > floats are limited to the range of the underlying IEEE implementation. > You'll have a certain cutoff above which your system bombs. > >>>> float(1<<1023) > 8.98846567431158e+307 >>>> float(1<<1024) > Traceback (most recent call last): > File "", line 1, in > float(1<<1024) > OverflowError: long int too large to convert to float > > (The exact cutoff may depend on how your Python is built, I think; > that was running on a 32-bit Python on Windows.) > [snip] Here's a way to calculate the integer square root: def int_sqrt(number): root = 1 while True: new_root = (root + number // root) // 2 if new_root == root: break root = new_root return root It's result is the largest integer whose square doesn't exceed the original number.