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; '#include': 0.07; 'i++)': 0.07; 'main()': 0.07; 'a.out': 0.09; 'part,': 0.09; 'sub': 0.09; 'subject:skip:m 10': 0.09; 'cc:addr:python-list': 0.10; 'python': 0.11; 'cc:name:python list': 0.16; 'cmp': 0.16; 'i);': 0.16; 'jmp': 0.16; 'jne': 0.16; 'loops': 0.16; 'mov': 0.16; 'reproduce': 0.16; 'suggested,': 0.16; 'wrote:': 0.16; '(in': 0.18; 'issue.': 0.20; 'machine': 0.21; 'cc:addr:python.org': 0.21; 'cc:2**1': 0.22; '64-bit': 0.22; 'break;': 0.22; 'gcc': 0.22; 'skip:$ 20': 0.22; 'skip:% 10': 0.22; 'bit': 0.23; '2015': 0.23; 'header:In- Reply-To:1': 0.24; 'linux': 0.26; 'compatible': 0.27; 'message- id:@mail.gmail.com': 0.28; 'looks': 0.29; '32-bit': 0.29; 'assembly': 0.29; 'cpu': 0.29; 'certainly': 0.31; 'push': 0.31; 'code': 0.31; 'run': 0.32; 'says': 0.32; 'int': 0.33; 'though.': 0.33; 'running': 0.34; 'add': 0.34; 'received:google.com': 0.34; 'skip:- 10': 0.34; 'could': 0.35; 'jason': 0.35; 'something': 0.35; 'but': 0.36; 'possible.': 0.36; 'there': 0.36; 'cases': 0.36; 'should': 0.37; 'subject:: ': 0.37; 'level': 0.37; 'difference': 0.38; 'goes': 0.39; 'unable': 0.39; 'system.': 0.39; 'where': 0.40; 'your': 0.60; 'close': 0.61; 'july': 0.63; 'different': 0.64; 'skip:$ 10': 0.67; 'function)': 0.84; 'oscar': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=+X2/EU0tfd8N/2Pw/d3z5UsSveKsRYOndWBHiOGiWs8=; b=lY7NOZQnbHVrzOPu1+bf+u4wDJJnyEPnwdj6II6c4uxxhVn7fzmMsrL0LmzmrtECI5 JosRCfi9I29Mb9PXLNNVsfGWR3AmpebXb/0+Y1o+gxWWyg0Omo/Ii8FZ7stfDldXA3yF Oy1Zu7+4apoWhSIUTYr5alIA15pJy3OMTnDXCNdRNe4ntXS9WwXrS/0wVuKAyCzFG36O plBGWtLzMGlyxPfFr0v0MLZtWKmcDp5Hsf3WS0Ta4y2CMLGPglBxn29VPq/En8qD6V4V oxMy5MeHLARZkvj95aS+EKag0Hq7eZt7cInYvPsL0nqP4TtGNXaOfBl974/4n9uJeZE3 gesw== X-Received: by 10.60.157.41 with SMTP id wj9mr34924288oeb.72.1435936458281; Fri, 03 Jul 2015 08:14:18 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: <55955048$0$1662$c3e8da3$5496439d@news.astraweb.com> From: Oscar Benjamin Date: Fri, 3 Jul 2015 16:13:58 +0100 Subject: Re: Bug in floating point multiplication To: Jason Swails Cc: "Steven D'Aprano" , python list Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list 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: 95 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1435936461 news.xs4all.nl 2948 [2001:888:2000:d::a6]:37343 X-Complaints-To: abuse@xs4all.nl X-Received-Bytes: 6463 X-Received-Body-CRC: 2551003892 Path: csiph.com!usenet.pasdenom.info!news.stben.net!border1.nntp.ams1.giganews.com!nntp.giganews.com!bcyclone02.am1.xlned.com!bcyclone02.am1.xlned.com!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Xref: csiph.com comp.lang.python:93466 On 2 July 2015 at 18:29, Jason Swails wrote: > > As others have suggested, this is almost certainly a 32-bit vs. 64-bit > issue. Consider the following C program: > > // maths.h > #include > #include > > int main() { > double x; > int i; > x = 1-pow(0.5, 53); > > for (i = 1; i < 1000000; i++) { > if ((int)(i*x) == i) { > printf("%d\n", i); > break; > } > } > > return 0; > } > > For the most part, this should be as close to an exact transliteration of > your Python code as possible. > > Here's what I get when I try compiling and running it on my 64-bit (Gentoo) > Linux machine with 32-bit compatible libs: > > swails@batman ~/test $ gcc maths.c > swails@batman ~/test $ ./a.out > swails@batman ~/test $ gcc -m32 maths.c > swails@batman ~/test $ ./a.out > 2049 I was unable to reproduce this on my system. In both cases the loops run to completion. A look at the assembly generated by gcc shows that something different goes on there though. The loop in the 64 bit one (in the main function) looks like: $ objdump -d a.out | less ... 400555: pxor %xmm0,%xmm0 400559: cvtsi2sdl -0xc(%rbp),%xmm0 40055e: mulsd -0x8(%rbp),%xmm0 400563: cvttsd2si %xmm0,%eax 400567: cmp -0xc(%rbp),%eax 40056a: jne 400582 40056c: mov -0xc(%rbp),%eax 40056f: mov %eax,%esi 400571: mov $0x400624,%edi 400576: mov $0x0,%eax 40057b: callq 400410 400580: jmp 40058f 400582: addl $0x1,-0xc(%rbp) 400586: cmpl $0xf423f,-0xc(%rbp) 40058d: jle 400555 ... Where is the 32 bit one looks like: $ objdump -d a.out.32 | less ... 804843e: fildl -0x14(%ebp) 8048441: fmull -0x10(%ebp) 8048444: fnstcw -0x1a(%ebp) 8048447: movzwl -0x1a(%ebp),%eax 804844b: mov $0xc,%ah 804844d: mov %ax,-0x1c(%ebp) 8048451: fldcw -0x1c(%ebp) 8048454: fistpl -0x20(%ebp) 8048457: fldcw -0x1a(%ebp) 804845a: mov -0x20(%ebp),%eax 804845d: cmp -0x14(%ebp),%eax 8048460: jne 8048477 8048462: sub $0x8,%esp 8048465: pushl -0x14(%ebp) 8048468: push $0x8048520 804846d: call 80482f0 8048472: add $0x10,%esp 8048475: jmp 8048484 8048477: addl $0x1,-0x14(%ebp) 804847b: cmpl $0xf423f,-0x14(%ebp) 8048482: jle 804843e ... So the 64 bit one is using SSE instructions and the 32-bit one is using x87. That could explain the difference you see at the C level but I don't see it on this CPU (/proc/cpuinfo says Intel(R) Core(TM) i5-3427U CPU @ 1.80GHz). -- Oscar