B
    u9a                 @   sp  d dl mZmZmZ d dlmZ d dlZd dlZd dlZd dl	Z	d dl
Z
d dlZd dlZdZedZedZedZejjZd\ZZee d	kZed
krejd  ZneZejepejZej edZ!ej edZ"dd Z#dd Z$dd Z%dd Z&dd Z'dd Z(dd Z)dd Z*d)dd Z+G d!d" d"e,Z-G d#d$ d$ej.Z/G d%d& d&ej.Z0d'd( Z1ed
krle1  dS )*    )run_unittestverboserequires_IEEE_754)supportNgh㈵>naninfz-inf)g 7yACg):@g7yAC__main__zmath_testcases.txtzcmath_testcases.txtc             C   s.   t dt d| d }|dk r*|d  }|S )a  Convert a non-NaN float x to an integer, in such a way that
    adjacent floats are converted to adjacent integers.  Then
    abs(ulps(x) - ulps(y)) gives the difference in ulps between two
    floats.

    The results from this function will only make sense on platforms
    where native doubles are represented in IEEE 754 binary64 format.

    Note: 0.0 and -0.0 are converted to 0 and -1, respectively.
    z<qz<dr   l            )structunpackpack)xn r   /usr/lib/python3.7/test_math.pyto_ulps#   s    
r   c             C   s   t t| } t| s t| r$| S tdtd| d }tdtd|d d }t|rtdtd|d d }| | S ||  S dS )a  Return the value of the least significant bit of a
    float x, such that the first float bigger than x is x+ulp(x).
    Then, given an expected result x and a tolerance of n ulps,
    the result y should be such that abs(y-x) <= n * ulp(x).
    The results from this function will only make sense on platforms
    where native doubles are represented in IEEE 754 binary64 format.
    z<qz<dr      N)absfloatmathisnanisinfr	   r
   r   )r   r   Zx_nextZx_prevr   r   r   ulp4   s    
r   c             C   s   | rdt | | d @  S dS )zANumber of '1' bits in binary expansion of a nonnnegative integer.r   r   )count_set_bits)r   r   r   r   r   a   s    r   c             C   sD   ||  d? }|sdS |dkr | S | | dB }t | |t || S dS )zProduct of integers in range(start, stop, 2), computed recursively.
    start and stop should both be odd, with start <= stop.

    r   N)partial_product)startstopZ
numfactorsZmidr   r   r   r   e   s    r   c             C   sd   d }}xJt t|  D ]6}|t| |d ? d dB | |? d dB 9 }||9 }qW || t|  > S )zFactorial of nonnegative integer n, via "Binary Split Factorial Formula"
    described at http://www.luschny.de/math/factorial/binarysplitfact.html

    r   )reversedrange
bit_lengthr   r   )r   innerZouterir   r   r   py_factorials   s
    *r!   c             C   sL   t t| t| }t | | }||ks0||kr4dS d}|||||S dS )zGiven finite floats `expected` and `got`, check that they're
    approximately equal to within the given number of ulps or the
    given absolute tolerance, whichever is bigger.

    Returns None on success and an error message on failure.
    NzAerror = {:.3g} ({:d} ulps); permitted error = {:.3g} or {:d} ulps)r   r   format)expectedgotulp_tolabs_tolZ	ulp_errorZ	abs_errorZfmtr   r   r   ulp_abs_check~   s    r'   c          	   c   s   t | }x|D ]x}d|kr.|d|d }| s8q|d\}}| \}}}| }|d }	|dd }
||t|t|	|
fV  qW W dQ R X dS )zParse a file with test values

    -- starts a comment
    blank lines, or lines containing only a comment, are ignored
    other lines are expected to have the form
      id fn arg -> expected [flag]*

    z--Nz->r   r   )openindexstripsplitr   )fnamefplinelhsrhsidfnarg
rhs_piecesexpflagsr   r   r   parse_mtestfile   s    	

r7   c          	   c   s   t | }x|D ]}|ds| s(q|d\}}| \}}}}| }	|	d |	d  }
}|	dd }||t|t|t|
t||fV  qW W dQ R X dS )zParse a file with test values

    Empty lines or lines starting with -- are ignored
    yields id, fn, arg_real, arg_imag, exp_real, exp_imag
    z--z->r   r      N)r(   
startswithr*   r+   r   )r,   r-   r.   r/   r0   r1   r2   Zarg_realZarg_imagr4   Zexp_realZexp_imagr6   r   r   r   parse_testfile   s    

r:              c             C   s   || krdS d}t | tr.t |tr.t|}nt |trJt | trJt| } t | trt |trt| rxt|rxd}n$t| st|rnt| |||}|dk	rd}|| |}|d|7 }|S dS dS )a  Compare arguments expected and got, as floats, if either
    is a float, using a tolerance expressed in multiples of
    ulp(expected) or absolutely (if given and greater).

    As a convenience, when neither argument is a float, and for
    non-finite floats, exact equality is demanded. Also, nan==nan
    as far as this function is concerned.

    Returns None on success and an error message on failure.
    Nz	not equalzexpected {!r}, got {!r}z ({}))
isinstancer   intr   r   r   r'   r"   )r#   r$   r%   r&   failurefail_fmtZfail_msgr   r   r   result_check   s&    
rA   c               @   s   e Zd Zdd Zdd ZdS )MyIndexablec             C   s
   || _ d S )N)value)selfrC   r   r   r   __init__   s    zMyIndexable.__init__c             C   s   | j S )N)rC   )rD   r   r   r   	__index__   s    zMyIndexable.__index__N)__name__
__module____qualname__rE   rF   r   r   r   r   rB      s   rB   c               @   s  e Zd ZdnddZdd Zdd Zd	d
 Zdd Zdd Zdd Z	dd Z
dd Zdd Zedd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zejd%d& Zd'd( Zd)d* Zd+d, Zeeed-d.d/ Zd0d1 Zd2d3 Z d4d5 Z!d6d7 Z"d8d9 Z#ed:d; Z$ee%d<dd=d> Z&d?d@ Z'dAdB Z(dCdD Z)dEdF Z*edGdH Z+dIdJ Z,dKdL Z-dMdN Z.dOdP Z/dQdR Z0eee12dSdTkdUdVdW Z3dXdY Z4dZd[ Z5d\d] Z6d^d_ Z7ed`da Z8edbdc Z9e:e;dddedf Z<edgdh Z=edidj Z>dkdl Z?dmS )o	MathTestsr;           c             C   s,   t ||||}|dk	r(| d|| dS )aa  Compare arguments expected and got, as floats, if either
        is a float, using a tolerance expressed in multiples of
        ulp(expected) or absolutely, whichever is greater.

        As a convenience, when neither argument is a float, and for
        non-finite floats, exact equality is demanded. Also, nan==nan
        in this function.
        Nz{}: {})rA   failr"   )rD   namer$   r#   r%   r&   r?   r   r   r   ftest   s    	zMathTests.ftestc             C   s8   |  dtjd |  dtjd | tjdtj  d S )Npig-DT!	@egiW
@r8   )rN   r   rO   rP   assertEqualZtau)rD   r   r   r   testConstants  s    zMathTests.testConstantsc             C   s   |  ttj | dtdtj | dtdtjd  | dtdd |  ttjt |  ttjt |  ttjdt	  |  ttjdt	  | 
ttt d S )Nzacos(-1)zacos(0)r   r8   zacos(1)r   )assertRaises	TypeErrorr   ZacosrN   rO   
ValueErrorINFNINFeps
assertTruer   NAN)rD   r   r   r   testAcos  s    zMathTests.testAcosc             C   s   |  ttj | dtdd | dtdd |  ttjd |  ttjd | ttt |  ttjt | 	t
tt d S )Nzacosh(1)r   r   zacosh(2)r8   g5qB?rS   )rT   rU   r   ZacoshrN   rV   rQ   rW   rX   rZ   r   r[   )rD   r   r   r   	testAcosh  s    zMathTests.testAcoshc             C   s   |  ttj | dtdtj d  | dtdd | dtdtjd  |  ttjt |  ttjt |  ttjdt	  |  ttjdt	  | 
ttt d S )Nzasin(-1)rS   r8   zasin(0)r   zasin(1)r   )rT   rU   r   ZasinrN   rO   rV   rW   rX   rY   rZ   r   r[   )rD   r   r   r   testAsin!  s    zMathTests.testAsinc             C   s   |  ttj | dtdd | dtdd | dtdd | ttt | ttt | t	tt
 d S )	Nzasinh(0)r   zasinh(1)r   g'ya64?z	asinh(-1)rS   g'ya64)rT   rU   r   ZasinhrN   rQ   rW   rX   rZ   r   r[   )rD   r   r   r   	testAsinh,  s    zMathTests.testAsinhc             C   s   |  ttj | dtdtj d  | dtdd | dtdtjd  | dtttjd	  | d
tttj d	  | t	tt
 d S )Nzatan(-1)rS      zatan(0)r   zatan(1)r   z	atan(inf)r8   z
atan(-inf))rT   rU   r   atanrN   rO   rW   rX   rZ   r   r[   )rD   r   r   r   testAtan5  s    zMathTests.testAtanc             C   s   |  ttj | dtdd | dtdd | dtdd |  ttjd	 |  ttjd
 |  ttjt |  ttjt | 	t
tt d S )Nzatanh(0)r   z
atanh(0.5)g      ?gz?zatanh(-0.5)g      gzr   rS   )rT   rU   r   ra   rN   ZatanhrV   rW   rX   rZ   r   r[   )rD   r   r   r   	testAtanh>  s    zMathTests.testAtanhc             C   s  |  ttj | dtddtj d  | dtddtj d  | dtddd | d	tddtjd  | d
tddtjd  | dtdttj | dtddtj | dtddtj | tddd | tddd | tdtd | 	t
tdt | dtdttj  | dtddtj  | dtddtj  | tddd | tddd | tdtd | 	t
tdt | dttttjd d  | dttdtjd  | dttdtjd  | dttdtjd  | dttdtjd  | dttttjd  | 	t
ttt | dttttj d d  | dttdtj d  | dttdtj d  | dttdtj d  | d ttdtj d  | d!ttttj d  | 	t
ttt | d"tdttj | d#tddtjd  | d$tddtjd  | tdtd | 	t
tdt | d%tdttj  | d&tddtj d  | d'tddtj d  | tdtd | 	t
tdt | 	t
ttt | 	t
ttd | 	t
ttd | 	t
ttd | 	t
ttd | 	t
ttt | 	t
ttt d S )(Nzatan2(-1, 0)rS   r   r8   zatan2(-1, 1)r   r`   zatan2(0, 1)zatan2(1, 1)zatan2(1, 0)zatan2(0., -inf)g        zatan2(0., -2.3)gffffffzatan2(0., -0.)g       gffffff@zatan2(-0., -inf)zatan2(-0., -2.3)zatan2(-0., -0.)zatan2(inf, -inf)   zatan2(inf, -2.3)zatan2(inf, -0.)zatan2(inf, 0.)zatan2(inf, 2.3)zatan2(inf, inf)zatan2(-inf, -inf)zatan2(-inf, -2.3)zatan2(-inf, -0.)zatan2(-inf, 0.)zatan2(-inf, 2.3)zatan2(-inf, inf)zatan2(2.3, -inf)zatan2(2.3, -0.)zatan2(2.3, 0.)zatan2(-2.3, -inf)zatan2(-2.3, -0.)zatan2(-2.3, 0.))rT   rU   r   Zatan2rN   rO   rX   rQ   rW   rZ   r   r[   )rD   r   r   r   	testAtan2I  sf     "zMathTests.testAtan2c             C   s  |  ttj | tttd | dtdd | dtdd | dtdd | d	td
d | dtdd | dtdd G dd d}G dd d}| dt| d |  ttj|  | }dd |_|  ttj| |  ttj|d d S )Ng      ?z	ceil(0.5)r   z	ceil(1.0)g      ?z	ceil(1.5)g      ?r8   z
ceil(-0.5)g      r   z
ceil(-1.0)g      rS   z
ceil(-1.5)g      c               @   s   e Zd Zdd ZdS )z$MathTests.testCeil.<locals>.TestCeilc             S   s   dS )N*   r   )rD   r   r   r   __ceil__  s    z-MathTests.testCeil.<locals>.TestCeil.__ceil__N)rG   rH   rI   rg   r   r   r   r   TestCeil  s   rh   c               @   s   e Zd ZdS )z&MathTests.testCeil.<locals>.TestNoCeilN)rG   rH   rI   r   r   r   r   
TestNoCeil  s   ri   zceil(TestCeil())rf   c              W   s   | S )Nr   )argsr   r   r   <lambda>      z$MathTests.testCeil.<locals>.<lambda>)	rT   rU   r   ZceilrQ   r>   typerN   rg   )rD   rh   ri   tr   r   r   testCeil  s     
zMathTests.testCeilc             C   s  |  tddd |  tddd |  tddd |  tddd |  td	d
d | ttj |  tddd |  tdd
d |  ttdt |  ttd
t |  ttdt |  ttd
t |  tdtd |  tdtd |  tttt |  tttt |  tttt |  tttt | ttt	d | ttt	t | ttt	t | ttt	t	 | t
ttt	 |  ttdt	d d S )Nr   rf   g      ?g        ig      rd   g      @g      @g       g      g       @)rQ   r   copysignrT   rU   rW   rX   rZ   r   r[   r   r   )rD   r   r   r   testCopysign  s0    zMathTests.testCopysignc             C   s   |  ttj | jdttj d dtdd | dtdd | jdttjd dtdd | dttjd	 y0| ttt	 | ttt
 W n4 tk
r   |  ttjt	 |  ttjt
 Y nX | ttt d S )
Nz
cos(-pi/2)r8   r   r   )r&   zcos(0)z	cos(pi/2)zcos(pi)rS   )rT   rU   r   ZcosrN   rO   r   rZ   r   rW   rX   rV   r[   )rD   r   r   r   testCos  s    $"zMathTests.testCosc             C   s   |  ttj | dtdd | dtddtdd   d | ttt | ttt | t	tt
 d S )Nzcosh(0)r   r   zcosh(2)-2*cosh(1)**2r8   rS   )rT   rU   r   coshrN   rQ   rW   rX   rZ   r   r[   )rD   r   r   r   testCosh  s    &zMathTests.testCoshc             C   sr   |  ttj | dttjd | dttjd d | dttj d d | d	td
d
 d S )Nzdegrees(pi)g     f@zdegrees(pi/2)r8   g     V@zdegrees(-pi/4)r`   g     Fz
degrees(0)r   )rT   rU   r   ZdegreesrN   rO   )rD   r   r   r   testDegrees  s
    zMathTests.testDegreesc             C   s   |  ttj | dtddtj  | dtdd | dtdtj | ttt | ttd | 	t
tt |  ttjd d S )	Nzexp(-1)rS   r   zexp(0)r   zexp(1)g        i@B )rT   rU   r   r5   rN   rP   rQ   rW   rX   rZ   r   r[   OverflowError)rD   r   r   r   testExp  s    zMathTests.testExpc             C   sN   |  ttj | dtdd | dtdd | dtdd d S )Nzfabs(-1)rS   r   zfabs(0)r   zfabs(1))rT   rU   r   ZfabsrN   )rD   r   r   r   testFabs  s    zMathTests.testFabsc             C   s   |  tdd |  tdd d}xXtddD ]J}||9 }|  t|| |  tt|| |  t|t| q4W | ttjd | ttjd | ttjdd   | ttjd	 | ttjtj d S )
Nr   r   g        i  rS   g      
   d   g}Ô%I)	rQ   r   	factorialr   r   r!   rT   rV   rO   )rD   Ztotalr    r   r   r   testFactorial  s    zMathTests.testFactorialc             C   s(   |  ttjdd  |  ttjd d S )Nry   rz   g}Ô%IT)rT   rv   r   r{   )rD   r   r   r   testFactorialHugeInputs  s    z!MathTests.testFactorialHugeInputsc             C   s>  |  ttj | tttd | dtdd | dtdd | dtdd | d	td
d | dtdd | dtdd | dtdd | dtdd G dd d}G dd d}| dt| d |  ttj|  | }dd |_|  ttj| |  ttj|d d S )Ng      ?z
floor(0.5)r   z
floor(1.0)g      ?r   z
floor(1.5)g      ?zfloor(-0.5)g      rS   zfloor(-1.0)g      zfloor(-1.5)g      zfloor(1.23e167)gNݯbzfloor(-1.23e167)gNݯc               @   s   e Zd Zdd ZdS )z&MathTests.testFloor.<locals>.TestFloorc             S   s   dS )Nrf   r   )rD   r   r   r   	__floor__  s    z0MathTests.testFloor.<locals>.TestFloor.__floor__N)rG   rH   rI   r   r   r   r   r   	TestFloor  s   r   c               @   s   e Zd ZdS )z(MathTests.testFloor.<locals>.TestNoFloorN)rG   rH   rI   r   r   r   r   TestNoFloor  s   r   zfloor(TestFloor())rf   c              W   s   | S )Nr   )rj   r   r   r   rk      rl   z%MathTests.testFloor.<locals>.<lambda>)	rT   rU   r   ZfloorrQ   r>   rm   rN   r   )rD   r   r   rn   r   r   r   	testFloor  s$    
zMathTests.testFloorc             C   s  |  ttj | dtddd | dtddd | dtddd	 | d
tddd | dtddd | dtddd | tttd	 | ttd	t | tttt |  ttjd	d |  ttjt	d	 |  ttjt
d	 |  ttjt	d | tdt	d | tdt	d | tdt
d | tdt
d | tddd | tdt
d d S )Nzfmod(10, 1)ry   r   g        zfmod(10, 0.5)g      ?zfmod(10, 1.5)g      ?g      ?zfmod(-10, 1)ig       zfmod(-10, 0.5)zfmod(-10, 1.5)g      g      @g      )rT   rU   r   ZfmodrN   rZ   r   r[   rV   rW   rX   rQ   )rD   r   r   r   testFmod$  s(    zMathTests.testFmodc                s     ttj  fdd}|dtdd |dtdd |d	td
d |dtdd  ttd t  ttd t  ttt	d  d S )Nc                sB   || \}}\}}t || tks*||kr> d| ||f  d S )Nz%s returned %r, expected %r)r   rY   rL   )rM   resultr#   mantr5   ZemantZeexp)rD   r   r   	testfrexp=  s    z&MathTests.testFrexp.<locals>.testfrexpz	frexp(-1)rS   )g      r   zfrexp(0)r   )r   r   zfrexp(1)r   )g      ?r   zfrexp(2)r8   )g      ?r8   )
rT   rU   r   frexprQ   rW   rX   rZ   r   r[   )rD   r   r   )rD   r   	testFrexp:  s    zMathTests.testFrexpz2fsum is not exact on machines with double roundingc                s>  ddl m} |j|j   fdd}g dfdgdfdddd	d
ddgd	fdddgdfdddgdfdddgdfdddgdfdd tddD tdfdd tddD tdfd d td!D d"g dfd#dd$gd%fd&d'd(d)gdfd*d td+d,d-D d.g td/fg}xt|D ]|\}\}}yt	|}W nR t
k
r^   | d0|||f  Y n* tk
r   | d1|||f  Y nX | || qW dd2lm}m}	m}
 xtd!D ]~}d3dd4dd5d6gd7 }d}x8td8D ],}|	d| d3 | }||7 }|| qW |
| ||}| ||t	| qW d S )9Nr   )
float_infoc                s   d\}}xb| D ]Z}t |\}}tt ||  }}||krT||| K }|}n||| K }||7 }qW tttt|d   | }|dkrd|d > }|d|  t||@ o|d| d @  }||7 }t ||S )zFull precision summation.  Compute sum(iterable) without any
            intermediate accumulation of error.  Based on the 'lsum' function
            at http://code.activestate.com/recipes/393090/

            )r   r   r8   r   r   rd   )	r   r   r>   ldexpmaxlenbinr   bool)iterableZtmantZtexpr   r   r5   tailh)etinymant_digr   r   msum_  s    
"(z MathTests.testFsum.<locals>.msumg        g}Ô%ITg      ?g}Ô%Ig0.++gd~QJg      gd~Qg      @Cg      g      g?Cg      9g     @Cg     @Cg     @Cg?Cg      ?g      <g?Cc             S   s   g | ]}d | qS )g      ?r   ).0r   r   r   r   
<listcomp>  s    z&MathTests.testFsum.<locals>.<listcomp>r   i  z0x1.df11f45f4e61ap+2c             S   s   g | ]}d | | qS )g      r   )r   r   r   r   r   r     s    z-0x1.62a2af1bd3624p-1c             S   s    g | ]}d |d  d |  qS )g333333?r   r   )r   r    r   r   r   r     s    i  g-g 7yACgؗҜ<g7yACg7yACg?g7yAgc             S   s,   g | ]$}d | d |d   d |d   qS )g       @2   4   r   )r   r   r   r   r   r     s    ii  r8   g      z0x1.5555555555555p+970zDtest %d failed: got OverflowError, expected %r for math.fsum(%.100r)zAtest %d failed: got ValueError, expected %r for math.fsum(%.100r))randomgaussshuffle   igh$.5g#B;ry      )sysr   r   min_expr   r   fromhex	enumerater   Zfsumrv   rL   rV   rQ   r   r   r   append)rD   r   r   Ztest_valuesr    Zvalsr#   actualr   r   r   jsvr   )r   r   r   testFsumL  sT    


zMathTests.testFsumc             C   sz  t j}| |ddd | |ddd | |ddd | |ddd | |ddd | |ddd | |ddd | |ddd | |ddd	 | |dd
d	 | |ddd d}d}d}|| }|| }| |||| | |||| | || || | ||| | | ||| | | || || | || | | | || | | d}|| }|| }| |||| | |||| | || || | ||| | | ||| | | || || | || | | | || | | | t|dd | t|dd | |tdtdd	 d S )Nr   r   rS   r   i   x   T      il    j9W    P)}l    "o  `T"-    i	 l   8P9cC/[N
S9bXl	   9@{wM275k l   Z'^%#>;*Q!/61^g      ^@g      U@)r   gcdrQ   rT   rU   rB   )rD   r   cr   yabr   r   r   testGcd  sP    
zMathTests.testGcdc             C   s   |  ttj | dtddd | dtddd | tttt | tttt | tttt | tttt |  t	tjt
t
 | ttdt | tttd d S )	Nz
hypot(0,0)r   z
hypot(3,4)rd   r`   r;   g      ?g       )rT   rU   r   ZhypotrN   rQ   r[   rW   rX   rv   	FLOAT_MAXrZ   r   )rD   r   r   r   	testHypot  s    zMathTests.testHypotc          	   C   s@  |  ttj | dtddd | dtddd | dtddd | d	tddd
 |  ttjdd |  ttjdd | tddd | tddd | ttdt | ttdt | 	t
ttd xFddddd gD ]0}| tt| t | tt| t | td| d | td| d | td| d | td| d | 	t
tt|  |  ttjd| |  ttjd| | td|d | td|d | tt|t | tt|t | 	t
tt| qW d S )Nz
ldexp(0,1)r   r   z
ldexp(1,1)r8   zldexp(1,-1)rS   g      ?zldexp(-1,1)r~   g      ?i@B g      ig        g          i+i l    d(	 l      Fx:^V ry   (   )rT   rU   r   r   rN   rv   rQ   rW   rX   rZ   r   r[   )rD   r   r   r   r   	testLdexp  s6    zMathTests.testLdexpc             C   s  |  ttj | dtdtj d | dtdd | dttjd | dtdd	d
 | dtdd dd | dtdd dd	 | dtdd d |  ttjd |  ttjdd   |  ttjt | tt	t	 | 
ttt d S )Nzlog(1/e)r   rS   zlog(1)r   zlog(e)z	log(32,2)r   r8   r;   zlog(10**40, 10)ry   r   zlog(10**40, 10**20)l      Fx:^V zlog(10**1000)i  gO+@g      )rT   rU   r   logrN   rP   rV   rX   rQ   rW   rZ   r   r[   )rD   r   r   r   testLog  s    zMathTests.testLogc             C   sl   |  ttj x6ddd dd gD ] }| t|tt| q"W |  ttjd | ttt d S )Nr8   Z   i,  rS   )	rT   rU   r   Zlog1pZassertAlmostEqualr   rV   rQ   rW   )rD   r   r   r   r   	testLog1p  s
     zMathTests.testLog1pc             C   s   |  ttj | tdd | tdd | tdd | tdd d | tdd	 d
 | tdd d |  ttjd |  ttjt | ttt	 d S )Nr   g        r8   g      ?r`   g       @i  g     @i   g      @i  g     @@g      )
rT   rU   r   log2rQ   rV   rX   rZ   r   r[   )rD   r   r   r   testLog2  s    zMathTests.testLog2ry   c             C   s8   dd t ddD }dd t ddD }| || d S )Nc             S   s   g | ]}t t d |qS )g      ?)r   r   r   )r   r   r   r   r   r   ,  s    z+MathTests.testLog2Exact.<locals>.<listcomp>ii   c             S   s   g | ]}t |qS r   )r   )r   r   r   r   r   r   -  s    )r   rQ   )rD   r   r#   r   r   r   testLog2Exact'  s    zMathTests.testLog2Exactc             C   s   |  ttj | dtdd | dtdd | dtdd | d	tdd
 d |  ttjd |  ttjdd
   |  ttjt | tt	t	 | 
ttt d S )Nz
log10(0.1)g?rS   zlog10(1)r   r   z	log10(10)ry   zlog10(10**1000)i  g     @@g      )rT   rU   r   Zlog10rN   rV   rX   rQ   r   rW   rZ   r   r[   )rD   r   r   r   	testLog100  s    zMathTests.testLog10c                s     ttj  fdd}|dtdd |dtdd  ttd	tf  ttd
tf tt} t	|d   t	|d  d S )Nc                sF   || \}}\}}t || tks.t || rB d| ||f  d S )Nz%s returned %r, expected %r)r   rY   rL   )rM   r   r#   Zv1Zv2Ze1Ze2)rD   r   r   testmodf?  s    z$MathTests.testModf.<locals>.testmodfz	modf(1.5)g      ?)g      ?g      ?z
modf(-1.5)g      )g      g      g        g       r   r   )
rT   rU   r   ZmodfrQ   rW   rX   r[   rZ   r   )rD   r   Zmodf_nanr   )rD   r   testModf<  s    
zMathTests.testModfc             C   s  |  ttj | dtddd | dtddd | dtddd | dtddd	 | ttdt | ttdt | tdtd
 | tdtd
 | t	tt
d | t	tdt
 | t	tdt
 | tdt
d | tdtd | tddd | tddd | tddd | tddd
 | tddd
 |  ttjdd |  ttjdd |  ttjdd |  ttjdt | t	tdt
 | tttt | ttdt | ttdt | ttdt | ttdd
 | ttdd
 | ttdd | ttdd | ttdd | tttd | t	ttt
 | tdtd | tddd | tddd | tddd | tddd
 | tddd
 |  ttjdd |  ttjdd |  ttjdd |  ttjdt | t	tdt
 | tttt | ttdt | ttdt | ttdt | ttdd
 | ttdd
 | ttdd | ttdd | ttdd | tttd | t	ttt
 | tdtd
 | tddd |  ttjdd | tddd
 | tddd
 | tddd
 | tddd
 |  ttjdd | tddd | tdtd
 | t	tdt
 | td
td
 | td
dd
 | td
dd
 | td
dd
 | td
dd
 | td
dd
 | td
dd
 | td
dd
 | td
dd
 | td
td
 | td
t
d
 | tddd
 | tddd
 | tt
dd
 | tddd
 | tddd
 | tt
dd
 |  ttjdd |  ttjdd | tdtd | tdtd | tdtt | tdtt | tdtt | tdtt | tdtd | tdtd | tdtt | tdtt | tdtd | tdtd | tdtd | tdtd | tdtt | tdtt | dtddd | d tddd! | d"tdd
d | d#tddd
 | d$tddd
 | d%tddd& | d'tddd( | d)tddd* |  ttjdd& |  ttjdd	 d S )+Nzpow(0,1)r   r   zpow(1,0)zpow(2,1)r8   z	pow(2,-1)rS   g      ?g      ?g        g      @gffffff@g       @g       g       gffffffg      g      g      .ggffffff?g?g?g?ggggffffffz	(-2.)**3.g       z	(-2.)**2.g      @z	(-2.)**1.z	(-2.)**0.z
(-2.)**-0.z
(-2.)**-1.g      z
(-2.)**-2.g      ?z
(-2.)**-3.g      )rT   rU   r   powrN   rQ   rW   rX   rZ   r   r[   rV   )rD   r   r   r   testPowO  s    zMathTests.testPowc             C   sr   |  ttj | dtdtj | dtdtjd  | dtdtj d  | d	td
d
 d S )Nzradians(180)   zradians(90)r   r8   zradians(-45)ir`   z
radians(0)r   )rT   rU   r   ZradiansrN   rO   )rD   r   r   r   testRadians  s
    zMathTests.testRadiansc          0      s  ddl m   fdd}ddddd	d
dddddddddddddddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3d4g0}x||D ]t}j|d5^ | \}}}t|}t|}t|}	||||	 t||}
|
	 |		  W d Q R X qW td6}xzt
d7d8D ]l}|dkr$q|| }xNt
d9D ]B}|| }t||}
||||
 t| |}
|| ||
 q6W qW x@td:d;d<d=ttgD ]*}tt| t|t qW x6d>D ].}t|t| t|t| qW xtd=d;d:d?tgD ]}t tt| W d Q R X t tt| W d Q R X t t|d: W d Q R X t t|d; W d Q R X qW d S )@Nr   )Fractionc                s    |  | |  }}} t|t|d  || | }|t| t|t|d kr||d t|d  dS )z
            Check that r matches remainder(x, y) according to the IEEE 754
            specification. Assumes that x, y and r are finite and y is nonzero.
            r8   N)ZassertLessEqualr   rQ   r>   )r   r   rZfxZfyfrr   )r   rD   r   r   validate_spec  s    z.MathTests.testRemainder.<locals>.validate_specz-4.0 1 -0.0z-3.8 1  0.8z-3.0 1 -0.0z-2.8 1 -0.8z-2.0 1 -0.0z-1.8 1  0.8z-1.0 1 -0.0z-0.8 1 -0.8z-0.0 1 -0.0z 0.0 1  0.0z 0.8 1  0.8z 1.0 1  0.0z 1.8 1 -0.8z 2.0 1  0.0z 2.8 1  0.8z 3.0 1  0.0z 3.8 1 -0.8z 4.0 1  0.0z&0x0.0p+0 0x1.921fb54442d18p+2 0x0.0p+0z?0x1.921fb54442d18p+0 0x1.921fb54442d18p+2  0x1.921fb54442d18p+0z?0x1.921fb54442d17p+1 0x1.921fb54442d18p+2  0x1.921fb54442d17p+1z?0x1.921fb54442d18p+1 0x1.921fb54442d18p+2  0x1.921fb54442d18p+1z?0x1.921fb54442d19p+1 0x1.921fb54442d18p+2 -0x1.921fb54442d17p+1z?0x1.921fb54442d17p+2 0x1.921fb54442d18p+2 -0x0.0000000000001p+2z00x1.921fb54442d18p+2 0x1.921fb54442d18p+2  0x0p0z?0x1.921fb54442d19p+2 0x1.921fb54442d18p+2  0x0.0000000000001p+2z?0x1.2d97c7f3321d1p+3 0x1.921fb54442d18p+2  0x1.921fb54442d14p+1z?0x1.2d97c7f3321d2p+3 0x1.921fb54442d18p+2 -0x1.921fb54442d18p+1z?0x1.2d97c7f3321d3p+3 0x1.921fb54442d18p+2 -0x1.921fb54442d14p+1z?0x1.921fb54442d17p+3 0x1.921fb54442d18p+2 -0x0.0000000000001p+3z00x1.921fb54442d18p+3 0x1.921fb54442d18p+2  0x0p0z?0x1.921fb54442d19p+3 0x1.921fb54442d18p+2  0x0.0000000000001p+3z?0x1.f6a7a2955385dp+3 0x1.921fb54442d18p+2  0x1.921fb54442d14p+1z?0x1.f6a7a2955385ep+3 0x1.921fb54442d18p+2  0x1.921fb54442d18p+1z?0x1.f6a7a2955385fp+3 0x1.921fb54442d18p+2 -0x1.921fb54442d14p+1z?0x1.1475cc9eedf00p+5 0x1.921fb54442d18p+2  0x1.921fb54442d10p+1z?0x1.1475cc9eedf01p+5 0x1.921fb54442d18p+2 -0x1.921fb54442d10p+1z 1  0.c  0.4z-1  0.c -0.4z 1 -0.c  0.4z-1 -0.c -0.4z 1.4  0.c -0.4z-1.4  0.c  0.4z 1.4 -0.c -0.4z-1.4 -0.c  0.4z$0x1.dp+1023 0x1.4p+1023  0x0.9p+1023z$0x1.ep+1023 0x1.4p+1023 -0x0.ap+1023z$0x1.fp+1023 0x1.4p+1023 -0x0.9p+1023)casez1p-1074i   rz   g        g       g       @gffffff)gffffffg       g        gffffff@gffffff@)	fractionsr   ZsubTestr+   r   r   r   Z	remainderrQ   hexr   r[   rX   rW   assertIsNaNrT   rV   )rD   r   Z	testcasesr   Zx_hexZy_hexZexpected_hexr   r   r#   r   Ztinyr   mrC   r   )r   rD   r   testRemainder  s    



"


zMathTests.testRemainderc             C   s   |  ttj | dtdd | dttjd d | dttj d d y0| ttt | ttt	 W n4 t
k
r   |  t
tjt |  t
tjt	 Y nX | ttt d S )Nzsin(0)r   z	sin(pi/2)r8   r   z
sin(-pi/2)rS   )rT   rU   r   ZsinrN   rO   rZ   r   rW   rX   rV   r[   )rD   r   r   r   testSinl  s    zMathTests.testSinc             C   s   |  ttj | dtdd | dtdd tdd  d | dtdtd d | ttt | ttt | 	t
tt d S )Nzsinh(0)r   zsinh(1)**2-cosh(1)**2r   r8   rS   zsinh(1)+sinh(-1))rT   rU   r   ZsinhrN   rs   rQ   rW   rX   rZ   r   r[   )rD   r   r   r   testSinhy  s    &zMathTests.testSinhc             C   s   |  ttj | dtdd | dtdd | dtdd | ttt |  ttjd |  ttjt | 	t
tt d S )	Nzsqrt(0)r   zsqrt(1)r   zsqrt(4)r`   r8   rS   )rT   rU   r   sqrtrN   rQ   rW   rV   rX   rZ   r   r[   )rD   r   r   r   testSqrt  s    zMathTests.testSqrtc             C   s   |  ttj | dtdd | dttjd d | dttj d d y0| ttt | ttt	 W n,   |  t
tjt |  t
tjt	 Y nX | ttt d S )Nztan(0)r   z	tan(pi/4)r`   r   z
tan(-pi/4)rS   )rT   rU   r   ZtanrN   rO   rZ   r   rW   rX   rV   r[   )rD   r   r   r   testTan  s    zMathTests.testTanc             C   s   |  ttj | dtdd | jdtdtd dtdd | dttd | dttd | t	tt
 d S )	Nztanh(0)r   ztanh(1)+tanh(-1)r   rS   )r&   z	tanh(inf)z
tanh(-inf))rT   rU   r   tanhrN   r   rW   rX   rZ   r   r[   )rD   r   r   r   testTanh  s    zMathTests.testTanhZTANH_PRESERVES_ZERO_SIGNr   z,system tanh() function doesn't copy the signc             C   s8   |  tdd |  tdtdtdd d S )Ng       g      ?)rQ   r   r   rp   )rD   r   r   r   testTanhSign  s    zMathTests.testTanhSignc             C   s&  |  tdd |  tdd |  ttdt |  ttdt |  tdd |  tdd |  tdd |  tdd |  tdd |  td	d
 G dd dt}G dd dt}|  t| d | ttj | ttjdd | ttj|  d S )Nr   rS   g      ?g      g?gg!r   g-?Yic               @   s   e Zd Zdd ZdS )z'MathTests.test_trunc.<locals>.TestTruncc             S   s   dS )N   r   )rD   r   r   r   	__trunc__  s    z1MathTests.test_trunc.<locals>.TestTrunc.__trunc__N)rG   rH   rI   r   r   r   r   r   	TestTrunc  s   r   c               @   s   e Zd ZdS )z)MathTests.test_trunc.<locals>.TestNoTruncN)rG   rH   rI   r   r   r   r   TestNoTrunc  s   r   r   r8   )rQ   r   Ztruncrm   r>   objectrT   rU   )rD   r   r   r   r   r   
test_trunc  s     zMathTests.test_truncc             C   s   |  td |  td |  td |  td | ttd | ttd | ttd d S )Ng        g       g      ?g      r   r   z-inf)rZ   r   ZisfiniteassertFalser   )rD   r   r   r   testIsfinite  s    zMathTests.testIsfinitec             C   sx   |  ttd |  ttd |  ttdd  | ttd | td | td d S )Nr   z-nanr   g        g      ?)rZ   r   r   r   r   )rD   r   r   r   	testIsnan  s    zMathTests.testIsnanc             C   s   |  ttd |  ttd |  td |  td | ttd | td | td d S )Nr   z-infg      g      r   g        g      ?)rZ   r   r   r   r   )rD   r   r   r   	testIsinf  s    zMathTests.testIsinfc             C   s   |  ttj d S )N)rZ   r   r   r   )rD   r   r   r   test_nan_constant  s    zMathTests.test_nan_constantc             C   sJ   |  ttj | tjd | tjtd | tj td d S )Ng        r   z-inf)rZ   r   r   r   ZassertGreaterrQ   r   )rD   r   r   r   test_inf_constant  s    zMathTests.test_inf_constantzrequires verbose modec             C   s   yt d}W n   | d Y nX |dkr8| d yt d}W n tk
rZ   Y nX | d yt d}W n tk
r   Y nX | d d S )	Ni 6ez6underflowing exp() should not have raised an exceptionr   z)underflowing exp() should have returned 0i ʚ;z.overflowing exp() didn't trigger OverflowErrorg      z sqrt(-1) didn't raise ValueError)r   r5   rL   rv   r   rV   )rD   r   r   r   r   test_exceptions  s     

zMathTests.test_exceptionsc          	   C   sp  dh}d }t jdkrNt d }yttt|d}W n tk
rL   Y nX d}g }xtt	D ]\}}}}	}
}}|	dks`|dkrq`|dkrq`|d k	r|dk r||krq`t
t|}d	|ksd
|krd}
nd|krd}
y||}W n0 tk
r   d}Y n tk
r   d}Y nX d\}}t|
|||}|d kr4q`|||||}|| q`W |rl| dd|  d S )NZtan0064darwinr   .z{}: {}({!r}): {}g        )ZrectZpolar)ry   r;   invalidzdivide-by-zerorV   overflowrv   )r;   g        zFailures in test_testfile:
  z
  )r   platformZmac_vertuplemapr>   r+   rV   r:   	test_filegetattrr   rv   rA   r"   r   rL   join)rD   ZSKIP_ON_TIGERZosx_versionZversion_txtr@   failuresr1   r2   arZaiZerZeir6   funcr   r%   r&   r?   msgr   r   r   test_testfile  sL    



zMathTests.test_testfilec          	   C   s2  d}g }xt tD ]\}}}}}tt|}d|ks<d|krBd}nd|krNd}y||}	W n. tk
rr   d}	Y n tk
r   d}	Y nX d\}
}|dkrd	}
n>|d
krd}n0|dkr|dkr|dk rd}
n|dk rd}
nd}
t||	|
|}|d krq|||||}|| qW |r.| 	dd
|  d S )Nz{}: {}({!r}): {}r   zdivide-by-zerorV   r   rv   )r;   g        Zgamma   ZlgammagV瞯<Zerfcg        g      ?ry   g      $@rz   i  zFailures in test_mtestfile:
  z
  )r7   math_testcasesr   r   rV   rv   rA   r"   r   rL   r   )rD   r@   r   r1   r2   r3   r#   r6   r   r$   r%   r&   r?   r   r   r   r   test_mtestfileG  sB    

zMathTests.test_mtestfilec             C   s   t |s| d| d S )NzExpected a NaN, got {!r}.)r   r   rL   r"   )rD   rC   r   r   r   r     s    
zMathTests.assertIsNaNN)r;   rK   )@rG   rH   rI   rN   rR   r\   r]   r^   r_   rb   rc   re   ro   r   rq   rr   rt   ru   rw   rx   r|   r   Zcpython_onlyr}   r   r   r   unittestZskipIfHAVE_DOUBLE_ROUNDINGr   r   r   r   r   r   r   Zrequires_mac_verr   r   r   r   r   r   r   r   r   r   r   	sysconfigZget_config_varr   r   r   r   r   r   r   Z
skipUnlessr   r   r   r   r   r   r   r   r   rJ      sr   

		="
Y+   	
			 9ErJ   c               @   s   e Zd ZejZdd Zdd Zdd Zdd Zd	d
 Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd ZdS ) IsCloseTestsc             O   s*   | j | j||f||d||f d d S )Nz%s and %s should be close!)r   )rZ   isclose)rD   r   r   rj   kwargsr   r   r   assertIsClose  s    zIsCloseTests.assertIsClosec             O   s*   | j | j||f||d||f d d S )Nz%s and %s should not be close!)r   )r   r   )rD   r   r   rj   r   r   r   r   assertIsNotClose  s    zIsCloseTests.assertIsNotClosec             O   s*   x$|D ]\}}| j ||f|| qW d S )N)r   )rD   examplesrj   r   r   r   r   r   r   assertAllClose  s    zIsCloseTests.assertAllClosec             O   s*   x$|D ]\}}| j ||f|| qW d S )N)r   )rD   r   rj   r   r   r   r   r   r   assertAllNotClose  s    zIsCloseTests.assertAllNotClosec          	   C   sR   |  t | jdddd W d Q R X |  t | jddddd W d Q R X d S )Nr   g0.+)rel_tolg0.++g    _)r  r&   )rT   rV   r   )rD   r   r   r   test_negative_tolerances  s    z%IsCloseTests.test_negative_tolerancesc             C   s$   ddddddg}| j |ddd d S )	N)g       @g       @)gN@igN@i)g;g;)i90  g    @)g        g       )iNF iNF g        )r  r&   )r  )rD   Zidentical_examplesr   r   r   test_identical  s    zIsCloseTests.test_identicalc             C   s*   dddg}| j |dd | j|dd d S )N)g    חAg   חA)g:0yEgnyE)g	ѭ?gfNӭ?g:0yE>)r  g&.>)r  r  )rD   Zeight_decimal_places_examplesr   r   r   test_eight_decimal_places  s
    z&IsCloseTests.test_eight_decimal_placesc             C   s*   dddg}| j |dd | j|dd d S )N)g&.>g        )g&.g        )gu?j/ʠg        g?)r  g:0yE>)r&   )r  r  )rD   Znear_zero_examplesr   r   r   test_near_zero  s
    zIsCloseTests.test_near_zeroc             C   s<   |  tt | j ttdd |  tt | j ttdd d S )Ng        )r&   )r   rW   rX   )rD   r   r   r   test_identical_infinite  s    z$IsCloseTests.test_identical_infinitec             C   sR   t t ft dfdt ftt ft tfttftdfdtftdfdtfg
}| j|dd d S )Ng0.++g      ?gg?)r&   )r[   rW   rX   r  )rD   Znot_close_examplesr   r   r   test_inf_ninf_nan  s    
zIsCloseTests.test_inf_ninf_nanc             C   s4   dddg}| j |dd dddg}| j|dd d S )	N)g      ?g      ?)g333333g333333)gYngYng        )r  )g      ?g     ?)g?g      ?)gZbtigTbti)r  r  )rD   Zzero_tolerance_close_examplesZ!zero_tolerance_not_close_examplesr   r   r   test_zero_tolerance  s    z IsCloseTests.test_zero_tolerancec             C   s   | j ddgdd d S )N)	   ry   )ry   r  g?)r  )r  )rD   r   r   r   test_asymmetry  s    zIsCloseTests.test_asymmetryc             C   s(   ddg}| j |dd | j|dd d S )N)ii )i[i[g:0yE>)r  g&.>)r  r  )rD   Zinteger_examplesr   r   r   test_integers  s    zIsCloseTests.test_integersc             C   sh   ddl m} |d|df|d|df|d|df|d	|d
fg}| j|dd | j|dd d S )Nr   )Decimalz
1.00000001z1.0z1.00000001e-20z1.0e-20z1.00000001e-100z1.0e-100z1.00000001e20z1.0e20g:0yE>)r  g&.>)decimalr  r  r  )rD   r  Zdecimal_examplesr   r   r   test_decimals  s    zIsCloseTests.test_decimalsc             C   sd   ddl m} |ddd |df|d|df|dd|ddfg}| j|dd	 | j|d
d	 d S )Nr   )r   r   i il       	(qe	 l      Fx:^V g:0yE>)r  g&.>)r   r   r  r  )rD   r   Zfraction_examplesr   r   r   test_fractions   s    zIsCloseTests.test_fractionsN)rG   rH   rI   r   r   r   r   r  r  r  r  r  r  r  r	  r
  r  r  r  r  r   r   r   r   r     s    

r   c              C   sN   ddl m}  t }|tt |tt || d t| d S )Nr   )DocFileSuitezieee754.txt)	Zdoctestr  r   Z	TestSuiteZaddTestZ	makeSuiterJ   r   r   )r  Zsuiter   r   r   	test_main  s    r  )r;   r<   )2Ztest.supportr   r   r   Ztestr   r   r   osr   r	   r   r   rY   r   r[   rW   rX   r   r   r   r   r   r   rG   argvfile__file__pathdirnamecurdirZtest_dirr   r   r   r   r   r   r   r!   r'   r7   r:   rA   r   rB   ZTestCaserJ   r   r  r   r   r   r   <module>   sZ   -
.         #{
