float point properties access

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Neal D. Becker

    #1

    float point properties access

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


  • Robin Becker

    #2
    Re: float point properties access

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

    --
    Robin Becker

    Comment

    • Robin Becker

      #3
      Re: float point properties access

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

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

      def dbl_epsilon(_ep s=[]):
      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(eto p)
      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

      Comment

      Working...