Serious error in int() function?

Christian Gollwitzer auriocus at gmx.de
Thu Apr 14 22:38:29 EDT 2016


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.

No, you need to count the significant digits in binary. 1e30 is not an 
even number in binary floating point.

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





More information about the Python-list mailing list