float point properties access

N

Neal D. Becker

Is there a way in python to access properties of floats? I need something
equiv to C DBL_EPSILON defined in <float.h>.
 
R

Robin Becker

Neal said:
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
.... n = 0
.... while 1:
.... e = 1.0/2**n
.... if (1.0+e==1.0): break
.... n += 1
.... pe = e
.... return pe
....
 
R

Robin Becker

Robin said:
you could try the traditional algorithm

... n = 0
... while 1:
... e = 1.0/2**n
... if (1.0+e==1.0): break
... n += 1
... pe = e
... return pe
...

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,212
Messages
2,571,102
Members
47,698
Latest member
TerraT521

Latest Threads

Top