Fortran (Was: The "does Python have variables?" debate)

Mark H Harris harrismh777 at gmail.com
Mon May 12 00:10:49 EDT 2014


On 5/11/14 10:10 PM, Dave Angel wrote:
> On 05/11/2014 02:54 PM, Mark H Harris wrote:
>
>>
>>  >julia> sin(BigFloat(π/4))
>>  > 7.0710678118654750275194295621751674626154323953749278952436611913748
>>  > 20215180412e-01 with 256 bits of precision
>>
>
> That answer doesn't seem to come anywhere near 256 bits of precision.
>
> Using Python 3.2,
>
>  >>> x=70710678118654750275194295621751674626154323953749278952436611913748
>  >>> x*x
> 4999999999999999693838300213161705693483516931249926767981110058185818806614907837502621065882204197129973479350206261627418690991407504
>
>
> Not that this is surprising, but it does make a terrible ad for how
> great Julia is.
>

Dave, you get the golden egg!  I expected D'Aprano to catch it first!

Yes, BigFloat does the same dumb thing Python's Decimal does.  π/4 is 
not a BigFloat, and BigFloat simply makes the 16 digit float into a 256 
bit float, the sin of which will only be 16 digits accurate (more or less).

It has nothing to do with the language (Python vs. Julia) it has to do 
with the way the BigFloat is formed.  So let's fix it by forming the π 
constant as a BigFloat constant:

 >julia> n = BigFloat(1)
 >1e+00 with 256 bits of precision

 >julia> π = atan(n/5)*16 - atan(n/239)*4
 >3.141592653589793238462643383279502884197169399375105820974944592307816406286198e+00 >with 256 bits of precision

 >julia> S = sin(π/4)
 >7.07106781186547524400844362104849039284835937688474036588339868995366239231051e-01 >with 256 bits of precision

 >julia> S * S
 >4.999999999999999999999999999999999999999999999999999999999999999999999999999957e-01 >with 256 bits of precision


Not too bad...


marcus







More information about the Python-list mailing list