Fortran (Was: The "does Python have variables?" debate)
Mark H Harris
harrismh777 at gmail.com
Mon May 12 00:26:53 EDT 2014
On 5/11/14 11:10 PM, Mark H Harris wrote:
> 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
Having said that, the accuracy was not my point; in the first place. My
point is that the sin() function is built-in, takes standard floats (32
bit, 64 bit, 128 bit) and BigFloats of arbitrary precision (and does
something meaningful with it). Let's take a look at Python Deciaml:
>>> =================== RESTART ===============
>>> from decimal import * (first I have to import)
>>> from math import * (than I have to import again)
>>> π
Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
π
NameError: name 'π' is not defined (whoops, don't know π )
>>> π=4*atan(1) (let's create it)
>>> sin(Decimal(π/4))
0.7071067811865475 (whoops sin doesn't do Decimals)
>>> from pdeclib import sin (let's get a sin that does do Decimals)
>>> sin(Decimal(π/4))
Decimal('0.707106781186547502751942956217516746261543')
>>> S=sin(Decimal(π/4)) (Whoops has the same problem as BigFloat)
>>> S**2
Decimal('0.499999999999999969383830021316170569348351')
>>>
Now let's fix it:
>>>
>>> from pdeclib import d
>>> n=d(1)
>>> from pdeclib import *
>>> n=d(1)
>>> π=atan(n/5)*16 - atan(n/239)*4
>>> S=sin(π/4)
>>> S**2
> Decimal('0.500000000000000000000000000000000000000000')
>>>
Also not bad, but slower. ;-)
marcus
More information about the Python-list
mailing list