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


Groups > comp.lang.python > #107048

Re: Serious error in int() function?

From blindanagram@nowhere.net
Newsgroups comp.lang.python
Subject Re: Serious error in int() function?
Date 2016-04-15 09:41 +0100
Message-ID <mailman.17.1460722036.6324.python-list@python.org> (permalink)
References (3 earlier) <zNSdna8abKekzJLKnZ2dnUU78fHNnZ2d@brightview.co.uk> <3MidnT9boJ97GpLKnZ2dnUU78LPNnZ2d@brightview.co.uk> <rid0hbt5r0pm5pjsjadb23g0f6jib2qrtt@4ax.com> <mailman.121.1460680575.15650.python-list@python.org> <5710A92F.3040306@nowhere.net>

Show all headers | View raw


On 15/04/2016 01:36, Dennis Lee Bieber wrote:
> On Thu, 14 Apr 2016 13:07:03 +0100, blindanagram@nowhere.net declaimed the
> following:
> 
>> On 14/04/2016 09:13, blindanagram@nowhere.net wrote:
>>> On 14/04/2016 08:59, blindanagram@nowhere.net wrote:
>>>> On 14/04/2016 07:52, ast wrote:
>>>
>>>> This means that the result will be correct provided it has 53 or less
>>>> bits - just short of 16 decimal digits (i.e for square numbers with less
>>>> than 32 digits).
>>>>
>>>> With an integer square root function (isqrt), this program:
>>>>
>>>> for exp in count(20):
>>>>   v = 2 ** exp - 1
>>>>   if isqrt(v) != sqrt(v):
>>>>     print(exp, isqrt(v), sqrt(v))
>>>>     break
>>>>
>>>> terminates with:
>>>>
>>>>   54 18014398509481983 1.8014398509481982e+16
>>>>
>>>> showing a first error for a 54 bit square root
>>>>
>>> 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. 

It can have up to about 30 significant digits because we know thaat it
is the square of a an integer that is representable in IEEE754 floating
point.

So when we consider its square root we only have to consider the squares
of integers that are representable and find the one that is 'nearest' to
our 30 bit integer.

When x is a representable integer the gap between (x- 1)^2, x^2 and (x +
1)^2 is approximately 2 * x and this means that ~30 digit perfect
squares will have different floating point representations because they
differ by a 15 digit value and will hence differ in their first ~15
significant digits.

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


Thread

Serious error in int() function? martin.spichty@gmail.com - 2016-04-13 00:41 -0700
  Re: Serious error in int() function? Marko Rauhamaa <marko@pacujo.net> - 2016-04-13 10:55 +0300
  Re: Serious error in int() function? Alain Ketterlin <alain@universite-de-strasbourg.fr.invalid> - 2016-04-13 10:00 +0200
  Re: Serious error in int() function? Peter Otten <__peter__@web.de> - 2016-04-13 10:03 +0200
  Re: Serious error in int() function? blindanagram@nowhere.net - 2016-04-13 09:04 +0100
  Re: Serious error in int() function? Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2016-04-13 23:51 +1200
  Re: Serious error in int() function? "ast" <nomail@com.invalid> - 2016-04-14 08:52 +0200
    Re: Serious error in int() function? Christian Gollwitzer <auriocus@gmx.de> - 2016-04-14 09:25 +0200
    Accuracy of math.sqrt(), was Re: Serious error in int() function? Peter Otten <__peter__@web.de> - 2016-04-14 09:46 +0200
    Re: Serious error in int() function? blindanagram@nowhere.net - 2016-04-14 08:59 +0100
      Re: Serious error in int() function? blindanagram@nowhere.net - 2016-04-14 09:13 +0100
        Re: Serious error in int() function? blindanagram@nowhere.net - 2016-04-14 13:07 +0100
          Re: Serious error in int() function? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2016-04-14 20:36 -0400
            Re: Serious error in int() function? Christian Gollwitzer <auriocus@gmx.de> - 2016-04-15 04:38 +0200
              Re: Serious error in int() function? John Pote <johnhpote@o2.co.uk> - 2016-04-15 12:05 +0100
                Re: Serious error in int() function? alister <alister.ware@ntlworld.com> - 2016-04-15 11:50 +0000
            Re: Serious error in int() function? blindanagram@nowhere.net - 2016-04-15 09:41 +0100
              Re: Serious error in int() function? blindanagram@nowhere.net - 2016-04-15 09:41 +0100

csiph-web