Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: What exactly is "exact" (was Clean Singleton Docstrings) Date: Mon, 18 Jul 2016 20:15:10 +1000 Lines: 38 Message-ID: References: <7046c74e-e5ea-4dde-8847-8c556756a563@googlegroups.com> <5789B601.9000604@stoneleaf.us> <9d2a0934-bf26-42a8-9bb2-e4e75b2d2ad3@googlegroups.com> <1468652038.1427719.667878897.460955C0@webmail.messagingengine.com> <578a027c$0$1616$c3e8da3$5496439d@news.astraweb.com> <69d8d886-c18a-4c05-8bca-9a05afacd9a9@googlegroups.com> <578ca188$0$1505$c3e8da3$5496439d@news.astraweb.com> <877fcjcllf.fsf@elektro.pacujo.net> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de OFhXoKeyC3NU5NaF4S/nrgxx0l4Tq8L5PFYzKC/+XYVA== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'value,': 0.03; 'binary': 0.05; 'failing': 0.05; 'cc:addr:python-list': 0.09; '(use': 0.09; 'operator,': 0.09; 'python': 0.10; 'def': 0.13; '0.2': 0.16; '2016': 0.16; 'blame': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'precision.': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'sorts': 0.16; 'subject:Singleton': 0.16; 'surprising': 0.16; 'tcp': 0.16; 'wrote:': 0.16; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'work,': 0.21; '64-bit': 0.22; 'bit': 0.23; 'absolute': 0.23; 'accuracy': 0.23; 'tried': 0.24; 'header:In-Reply-To:1': 0.24; 'mon,': 0.24; "i've": 0.25; 'compare': 0.27; 'message- id:@mail.gmail.com': 0.27; 'arithmetic': 0.29; 'programmers': 0.30; 'relative': 0.30; 'certain': 0.31; 'maybe': 0.33; 'run': 0.33; 'point': 0.33; 'problem': 0.33; 'adjust': 0.33; 'instead,': 0.33; 'except': 0.34; 'received:google.com': 0.35; "isn't": 0.35; 'should': 0.36; 'smaller': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'stuff': 0.38; 'why': 0.39; 'rather': 0.39; 'where': 0.40; 'ever': 0.60; "you'll": 0.61; 'skip:u 10': 0.61; 'real': 0.62; 'different': 0.63; "they're": 0.66; 'results': 0.66; 'jul': 0.72; 'intelligent': 0.76; 'introduce': 0.79; 'chrisa': 0.84; 'concept.': 0.84; 'tie': 0.84; 'to:none': 0.91; 'scary.': 0.91; 'hands': 0.96 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:cc; bh=uImUTILkRkRo5rtA+uAa+1U+yMQiQNCiGMkXM1YC2Sk=; b=dyY7PLz3uDK9PMckmNhS72I6wxl3eSVy0nwfXWC4b1+VHtIwSEiOVYcb9zmpVTWSBn qMjb1aDKF+32aIYptsTkpWDHL7zZpzWbt90LrhZSP76JDt14mBUK3/Wb0bkeGNBKtXAL 431s5g3qB7Pd4yaj3c4POSLIK1UWhn1wHYmcVd2rwtn4m9AyHg+SigwQ15mHDN3BZYTd FuHvtpQe7Xoz7fjuMfWe5i/q7yyEuZYVlfM2cOxuzyDazVzrB1q0vIyRF5tr0YGhiGDv +GpniTQ/H3fb5fIRFwjMsGlfS6u0AjXzdyTbkrYNwxPeS1mRqTIFcvuRhunA7brzW+gi +Yaw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:cc; bh=uImUTILkRkRo5rtA+uAa+1U+yMQiQNCiGMkXM1YC2Sk=; b=XLIZ2c9cIKf2kahMvM4ZcR5Ok5KDcqdh9OyTSNeOl/vmJdhWQ0iDs8Es/2blVudiXO 214V5U4wG5YN6nzaCZ7UuvfQwrszGsA/J5i01zYChYRiT9ux8UCij7ymewJ6DWycGTd2 mdbd7NG94szW8s8//FOd6klDj5aprDnTNy8TVNkobdfD0uhqz7nZ0GeYmPqzJBjl8C6V FFfKL9U/84onrn9QynfV+FPKHWIsFcQkSPzW6dV0E+m5rbmwES6BbXSLXRKMgnrE8akM KaycWcnivYsRAlG2DedVS1NbgskCpiBG6SDvQH6wgvP0wdWaVS/aK0iRgahry6I+8q3s hOhg== X-Gm-Message-State: ALyK8tIvVp2hAm6CHXwA6jZbXw+2lFZ+kyGYhG2h3k53KycbHX6z80G+e0/NFNK/2c8096yWdmYlgFKaQXtQ6w== X-Received: by 10.28.193.140 with SMTP id r134mr30299195wmf.81.1468836911563; Mon, 18 Jul 2016 03:15:11 -0700 (PDT) In-Reply-To: <877fcjcllf.fsf@elektro.pacujo.net> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: <7046c74e-e5ea-4dde-8847-8c556756a563@googlegroups.com> <5789B601.9000604@stoneleaf.us> <9d2a0934-bf26-42a8-9bb2-e4e75b2d2ad3@googlegroups.com> <1468652038.1427719.667878897.460955C0@webmail.messagingengine.com> <578a027c$0$1616$c3e8da3$5496439d@news.astraweb.com> <69d8d886-c18a-4c05-8bca-9a05afacd9a9@googlegroups.com> <578ca188$0$1505$c3e8da3$5496439d@news.astraweb.com> <877fcjcllf.fsf@elektro.pacujo.net> Xref: csiph.com comp.lang.python:111600 On Mon, Jul 18, 2016 at 8:00 PM, Marko Rauhamaa wrote: > Python programmers (among others) frequently run into issues with > surprising results in floating-point arithmetics. For better or worse, > Scheme has tried to abstract the concept. You don't need to explain the > ideas of IEEE 64-bit floating-point numbers or tie the hands of the > implementation. Instead, what you have is "reliable" arithmetics and > "best-effort" arithmetics, a bit like TCP is "reliable" and UDP is > "best-effort". The problem with that is that failing to explain IEEE floating point and just calling it "inexact" scares people off unnecessarily. I've seen a lot of very intelligent people who think that you should never compare floats with the == operator, because floats randomly introduce "inaccuracy". And then you get these sorts of functions: EPSILON = 0.000001 # Adjust to control numeric accuracy def is_equal(f1, f2, epsilon=EPSILON): if abs(f1) > abs(f2): f1, f2 = f2, f1 return abs(f2-f1) < f1*epsilon and interminable debates about how to pick an epsilon, whether it should be relative to the smaller value (as here) or the larger (use f2 instead), or maybe should be an absolute value, or maybe it should be relative to the largest/smallest value that was ever involved in the calculation, or........ Floating point numbers are a representation of real numbers that involves a certain amount of precision. They're ultimately no different from grade-school arithmetic where you round stuff off so you don't need an infinite amount of paper, except that they work with binary rather than decimal, so people think "0.1 + 0.2 ought to be exactly 0.3, why isn't it??", and blame floats. Explain what they REALLY do and how they work, and you'll find they're not so scary. ChrisA