Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: John Pote Newsgroups: comp.lang.python Subject: Re: Serious error in int() function? Date: Fri, 15 Apr 2016 12:05:18 +0100 Lines: 92 Message-ID: References: <52f7516c-8601-4252-ab16-bc30c59c8306@googlegroups.com> <570f3e30$0$665$426a74cc@news.free.fr> <3MidnT9boJ97GpLKnZ2dnUU78LPNnZ2d@brightview.co.uk> <5710CAEE.4080406@o2.co.uk> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 8bit X-Trace: news.uni-berlin.de M671SaTjkrbgp0dhKCkmoAbfsB8hCcy8iBGM/XveaPcQ== 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; 'root': 0.04; 'base.': 0.05; 'binary': 0.05; 'context': 0.05; 'so?': 0.07; 'squares': 0.07; 'trailing': 0.07; 'bits.': 0.09; 'indicates': 0.09; 'integer,': 0.09; 'python': 0.10; 'subject:error': 0.11; 'output': 0.13; '>>>': 0.15; 'mathematics': 0.15; 'result.': 0.15; '1e+30': 0.16; '2.00': 0.16; 'binary,': 0.16; 'binary.': 0.16; 'decimal.': 0.16; 'digits.': 0.16; 'exponent': 0.16; 'integer.': 0.16; 'multiplied': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'skip:1 40': 0.16; 'skip:1 60': 0.16; 'still,': 0.16; 'warranty.': 0.16; 'writen': 0.16; 'wrote:': 0.16; 'integer': 0.18; 'odd': 0.18; '>>>': 0.20; 'foundation,': 0.20; 'math': 0.20; '1998,': 0.22; 'converted': 0.22; 'strict': 0.22; 'bit': 0.23; "python's": 0.23; 'import': 0.24; 'written': 0.24; 'header:In- Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'example': 0.26; 'sense': 0.26; 'point.': 0.27; '2.0': 0.27; 'correct': 0.28; 'function': 0.28; "skip:' 10": 0.28; '(it': 0.29; 'arithmetic': 0.29; 'decimal': 0.29; 'equally': 0.29; 'themselves': 0.29; 'point': 0.33; 'point,': 0.33; 'shift': 0.33; 'true.': 0.33; 'except': 0.34; 'could': 0.35; 'but': 0.36; 'should': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'being': 0.37; 'expect': 0.37; 'say': 0.37; 'no,': 0.38; 'christian': 0.38; 'mean': 0.38; 'data': 0.39; 'does': 0.39; 'received:192': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'software': 0.40; 'term': 0.60; 'care': 0.60; 'john': 0.61; 'skip:u 10': 0.61; 'received:uk': 0.61; 'school': 0.62; 'details': 0.62; 'charset:windows-1252': 0.62; 'course': 0.62; 'digital': 0.63; '2000': 0.63; 'more': 0.63; 'to,': 0.63; 'more.': 0.63; 'times': 0.63; 'world': 0.64; 'between': 0.65; '8bit%:31': 0.66; '>>>>>': 0.66; 'converter': 0.66; 'quantity': 0.66; 'readers': 0.66; 'here': 0.66; 'power': 0.72; '.....': 0.76; 'square': 0.76; '/with': 0.84; 'adc': 0.84; 'confusing': 0.84; 'gollwitzer': 0.84; 'significance': 0.84; 'skip:1 50': 0.84; 'temperature': 0.84; 'absolutely': 0.88; 'dennis': 0.91; 'taught': 0.97 User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:38.0) Gecko/20100101 Thunderbird/38.7.2 In-Reply-To: X-Content-Filtered-By: Mailman/MimeDel 2.1.21 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 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: <5710CAEE.4080406@o2.co.uk> X-Mailman-Original-References: <52f7516c-8601-4252-ab16-bc30c59c8306@googlegroups.com> <570f3e30$0$665$426a74cc@news.free.fr> <3MidnT9boJ97GpLKnZ2dnUU78LPNnZ2d@brightview.co.uk> Xref: csiph.com comp.lang.python:107042 On 15/04/2016 03:38, Christian Gollwitzer wrote: > Am 15.04.16 um 02:36 schrieb Dennis Lee Bieber: >>>> I should also have said that the square root of integer squares with >>>> between 15 and 30 decimal digits will only be correct if the square >>>> numbers themselves are exactly representable in 53 bits. So we can >>>> expect failures for squares with 16 or more digits. >>> >>> However, if a number with 31 or less digits is known to be the >>> square of >>> an integer, the IEEE754 sqrt function will (I believe) give the correct >>> result. >> >> How could it EXCEPT by having ~15 significant digits and an >> exponent -- >> since that is all the data that is provided by a double precision >> floating >> point. That is, for example, >> >>>>> 1000000000000000.0 * 1000000000000000.0 >> 1e+30 >>>>> import math >>>>> math.sqrt(1e30) >> 1000000000000000.0 >>>>> >> >> only has ONE significant digit -- even though it has thirty 0s before >> the >> decimal point. > As I was taught in school and university the number of significant digits are the number of digits written after the point, be that decinal, binary or any other base. But away from the world of mathematics and strict engineering we often refer to the number of significant digits as the number of digits that have significance in the value of the quantity being represented. eg a temperature or voltage. For example we might say that a 10 bit analogue to digital converter has, in binary, 10 significant digits and in decimal approximately 3. What can be confusing here is that if readings from such a converter are writen down as whole numbers with a decimal point 1.0, 2.0 ..... 1023.0 then the readings are /shown /with 1 significant digit. We could equally write them 1.00, 2.00 ..... 1023.00 and they are now shown to 2 significant digits. Of course these trailing 0s are of no practical 'significance' as the ADC cannot affect them. Indeed it would not even output them. So context is everything. Care needs to be taken in understanding the use of this term by writers and readers that the context is clear, are binary or decimal digits being refered to, are we using 'significant' digits in the strict mathematical/engineering sense or a more colloquial sense. Then again we might write 1023.0 in an exponent form 0·1023.10^4 ( 0 point 1023 times 10 to the power 4) in which case we would say the reading is shown to 4 significant digits > No, you need to count the significant digits in binary. 1e30 is not an > even number in binary floating point. How so? Whether or not an integer is odd or even has nothing to do with its representation in binary of decimal, floating point or integer. If you mean the mantissa is odd that may well be true. But the exponent may also shift the mantissa left by on1 place or more. In which case the value the floating point number is holding must be even. (it is multiplied by 2^n where n >= 1) My Python 2.7.9 does this >>> "%50.10f"%1e30 ' 1000000000000000019884624838656.0000000000' >>> This indicates that 1e30 is not representable in Python's floating point representation. John > Apfelkiste:AsynCA chris$ bc > bc 1.06 > Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc. > This is free software with ABSOLUTELY NO WARRANTY. > For details type `warranty'. > obase=2 > 10^30 > 11001001111100101100100111001101000001000110011101001110110111101010\ > 01000000000000000000000000000000 > sqrt(10^30) > 11100011010111111010100100110001101000000000000000 > > Still, the floating point arithmetic should round to the correct > answer if both are converted to decimal. > > Christian > >