3
‡Œðiv  ã               @   sP   d Z ddlmZ ddlmZmZmZ ddlmZm	Z	 d
dd„Z
dd	„ ZdgZdS )zD
``python-future``: pure Python implementation of Python 3 round().
é    )Údivision)ÚPYPYÚPY26Úbind_method)ÚDecimalÚROUND_HALF_EVENNc             C   s¶   d}|dkrd}d}t | dƒr(| j|ƒS tdƒ|  }dtt| ƒƒkrNt| ƒ} t| tƒr^| }ntsntj| ƒ}nt	| ƒ}|dk r�t
|| ƒ| }n|j|td�}|rªt|ƒS t|ƒS dS )	a©  
    See Python 3 documentation: uses Banker's Rounding.

    Delegates to the __round__ method if for some reason this exists.

    If not, rounds a number to a given precision in decimal digits (default
    0 digits). This returns an int when called with one argument,
    otherwise the same type as the number. ndigits may be negative.

    See the test_round method in future/tests/test_builtins.py for
    examples.
    FNTr   Ú	__round__Ú10Znumpy)Úrounding)Úhasattrr   r   ÚreprÚtypeÚfloatÚ
isinstancer   Ú
from_floatÚfrom_float_26ÚnewroundÚquantizer   Úint)ÚnumberÚndigitsZ
return_intÚexponentÚdÚresult© r   ú:/tmp/pip-build-6m2fasei/future/future/builtins/newround.pyr      s(    


r   c       	      C   s¦   ddl }ddlm} t| ttfƒr*t| ƒS |j| ƒs>|j| ƒrJtt	| ƒƒS |j
d| ƒdkr`d}nd}t| ƒjƒ \}}dd„ }||ƒd }||t|d|  ƒ| ƒ}|S )	aî  Converts a float to a decimal number, exactly.

    Note that Decimal.from_float(0.1) is not the same as Decimal('0.1').
    Since 0.1 is not exactly representable in binary floating point, the
    value is stored as the nearest representable value which is
    0x1.999999999999ap-4.  The exact equivalent of the value in decimal
    is 0.1000000000000000055511151231257827021181583404541015625.

    >>> Decimal.from_float(0.1)
    Decimal('0.1000000000000000055511151231257827021181583404541015625')
    >>> Decimal.from_float(float('nan'))
    Decimal('NaN')
    >>> Decimal.from_float(float('inf'))
    Decimal('Infinity')
    >>> Decimal.from_float(-float('inf'))
    Decimal('-Infinity')
    >>> Decimal.from_float(-0.0)
    Decimal('-0')

    r   N)Ú_dec_from_tripleg      ð?é   c             S   s$   | dkrt tt| ƒƒƒd S dS d S )Nr   é   )ÚlenÚbinÚabs)r   r   r   r   Ú
bit_length_   s    z!from_float_26.<locals>.bit_lengthé   )ÚmathÚdecimalr   r   r   Úlongr   ÚisinfÚisnanr   Úcopysignr!   Úas_integer_ratioÚstr)	ÚfZ_mathr   ÚsignÚnr   r"   Úkr   r   r   r   r   =   s    r   )N)Ú__doc__Ú
__future__r   Zfuture.utilsr   r   r   r%   r   r   r   r   Ú__all__r   r   r   r   Ú<module>   s   
0,