Serious error in int() function?

blindanagram at nowhere.net blindanagram at nowhere.net
Thu Apr 14 08:07:03 EDT 2016


On 14/04/2016 09:13, blindanagram at nowhere.net wrote:
> On 14/04/2016 08:59, blindanagram at 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.




More information about the Python-list mailing list