float point properties access

Robin Becker robin at SPAMREMOVEjessikat.fsnet.co.uk
Fri Dec 3 10:05:23 EST 2004


Robin Becker wrote:
> Neal D. Becker wrote:
> 
>> Is there a way in python to access properties of floats?  I need 
>> something
>> equiv to C DBL_EPSILON defined in <float.h>.
>>
>>
> you could try the traditional algorithm
> 
>  >>> def dbl_epsilon():
> ...     n = 0
> ...     while 1:
> ...         e = 1.0/2**n
> ...         if (1.0+e==1.0): break
> ...         n += 1
> ...         pe = e
> ...     return pe
> ...
>  >>> print dbl_epsilon()
> 2.22044604925e-016
>  >>>
> 

on looking further I find my 'traditional' algorithm is actually 
something like this

def dbl_epsilon(_eps=[]):
	if not _eps:
		etop = 1.0
		ebot = 0.0
		eps = ebot+(etop-ebot)/2.0
		while eps!=ebot and eps!=etop:
			epsp1 = 1.0 - eps
			if epsp1<1.0: etop = eps
			else: ebot = eps
			eps = ebot+(etop-ebot)/2.0
		_eps.append(etop)
		assert (1.0-etop)<1.0 and (1.0-ebot)==1.0, 'Error in epsilon calculation'
	return _eps[0]

print dbl_epsilon()

which gives 5.55111512313e-017


-senility is making me stupidly yrs-
Robin Becker



More information about the Python-list mailing list