σ
ίΘ[c           @` s~   d  Z  d d l m Z m Z m Z m Z d d l Z d d l m	 Z
 d d d  Z d d  Z d	   Z d
   Z d   Z d S(   uτ   Time utilities.

In particular, routines to do basic arithmetic on numbers represented by two
doubles, using the procedure of Shewchuk, 1997, Discrete & Computational
Geometry 18(3):305-363 -- http://www.cs.berkeley.edu/~jrs/papers/robustr.pdf
i    (   t   absolute_importt   divisiont   print_functiont   unicode_literalsNi   (   t   unitsg      π?c         C` s&  t  |  |  \ } } t j | d k  re t | |  \ } } | | | 7} t  | |  \ } } n  t j | d k  rι | | } t | |  \ } }	 t  | |  \ }
 } | | 7} | |	 8} |
 | | } t  | |  \ } } n  t j |  } t  | |  \ } } | | | 7} | | f S(   uD  
    Return the sum of ``val1`` and ``val2`` as two float64s, an integer part
    and the fractional remainder.  If ``factor`` is not 1.0 then multiply the
    sum by ``factor``.  If ``divisor`` is not 1.0 then divide the sum by
    ``divisor``.

    The arithmetic is all done with exact floating point operations so no
    precision is lost to rounding error.  This routine assumes the sum is less
    than about 1e16, otherwise the ``frac`` part will be greater than 1.0.

    Returns
    -------
    day, frac : float64
        Integer and fractional part of val1 + val2.
    g      π?(   t   two_sumt   npt   anyt   two_productt   round(   t   val1t   val2t   factort   divisort   sum12t   err12t   carryt   q1t   p1t   p2t   d1t   d2t   q2t   dayt   extrat   frac(    (    s1   lib/python2.7/site-packages/astropy/time/utils.pyt   day_frac   s"    


c         C` sΣ   | d k	 rB t |   \ } } t |  \ } } | | | | f Sy |  j j t j  } Wn$ t k
 r |  j t j  d f SX| d k r€ t |  j	 d d | St j j |  j  } t |  j	 d d | Sd S(   u  Like ``day_frac``, but for quantities with units of time.

    The quantities are separately converted to days. Here, we need to take
    care with the conversion since while the routines here can do accurate
    multiplication, the conversion factor itself may not be accurate.  For
    instance, if the quantity is in seconds, the conversion factor is
    1./86400., which is not exactly representable as a float.

    To work around this, for conversion factors less than unity, rather than
    multiply by that possibly inaccurate factor, the value is divided by the
    conversion factor of a day to that unit (i.e., by 86400. for seconds).  For
    conversion factors larger than 1, such as 365.25 for years, we do just
    multiply.  With this scheme, one has precise conversion factors for all
    regular time units that astropy defines.  Note, however, that it does not
    necessarily work for all custom time units, and cannot work when conversion
    to time is via an equivalency.  For those cases, one remains limited by the
    fact that Quantity calculations are done in double precision, not in
    quadruple precision as for time.
    g        g      π?R   R   N(
   t   Nonet   quantity_day_fract   unitt   tot   uR   t	   Exceptiont   to_valueR   t   value(   R
   R   t   res11t   res12t   res21t   res22R   R   (    (    s1   lib/python2.7/site-packages/astropy/time/utils.pyR   :   s    c         C` s@   |  | } | |  } | | } | | } |  | } | | | f S(   uΡ  
    Add ``a`` and ``b`` exactly, returning the result as two float64s.
    The first is the approximate sum (with some floating point error)
    and the second is the error of the float64 sum.

    Using the procedure of Shewchuk, 1997,
    Discrete & Computational Geometry 18(3):305-363
    http://www.cs.berkeley.edu/~jrs/papers/robustr.pdf

    Returns
    -------
    sum, err : float64
        Approximate sum of a + b and the exact floating point error
    (    (   t   at   bt   xt   ebt   ea(    (    s1   lib/python2.7/site-packages/astropy/time/utils.pyR   b   s    




c         C` s   |  | } t  |   \ } } t  |  \ } } | | } | | } | | }	 | |	 8} | | }
 | |
 8} | | } | | } | | f S(   uί  
    Multiple ``a`` and ``b`` exactly, returning the result as two float64s.
    The first is the approximate product (with some floating point error)
    and the second is the error of the float64 product.

    Uses the procedure of Shewchuk, 1997,
    Discrete & Computational Geometry 18(3):305-363
    http://www.cs.berkeley.edu/~jrs/papers/robustr.pdf

    Returns
    -------
    prod, err : float64
        Approximate product a * b and the exact floating point error
    (   t   split(   R'   R(   R)   t   aht   alt   bht   blt   y1t   yt   y2t   y3t   y4(    (    s1   lib/python2.7/site-packages/astropy/time/utils.pyR   y   s    








c         C` s2   d |  } | |  } | | } |  | } | | f S(   uΔ   
    Split float64 in two aligned parts.

    Uses the procedure of Shewchuk, 1997,
    Discrete & Computational Geometry 18(3):305-363
    http://www.cs.berkeley.edu/~jrs/papers/robustr.pdf

    g      A(    (   R'   t   ct   abigR-   R.   (    (    s1   lib/python2.7/site-packages/astropy/time/utils.pyR,      s
    	



(   t   __doc__t
   __future__R    R   R   R   t   numpyR   t    R   R   R   R   R   R   R   R,   (    (    (    s1   lib/python2.7/site-packages/astropy/time/utils.pyt   <module>   s   "*(		