<s> from __future__ import division <EOL> import sys <EOL> import random <EOL> import math <EOL> import unittest <EOL> from test . test_support import run_unittest , cpython_only <EOL> requires_IEEE_754 = unittest . skipUnless ( <EOL> float . __getformat__ ( "<STR_LIT>" ) . startswith ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" ) <EOL> DBL_MAX = sys . float_info . max <EOL> DBL_MAX_EXP = sys . float_info . max_exp <EOL> DBL_MIN_EXP = sys . float_info . min_exp <EOL> DBL_MANT_DIG = sys . float_info . mant_dig <EOL> DBL_MIN_OVERFLOW = <NUM_LIT:2> ** DBL_MAX_EXP - <NUM_LIT:2> ** ( DBL_MAX_EXP - DBL_MANT_DIG - <NUM_LIT:1> ) <EOL> def truediv ( a , b ) : <EOL> """<STR_LIT>""" <EOL> negative = a ^ b < <NUM_LIT:0> <EOL> a , b = abs ( a ) , abs ( b ) <EOL> if not b : <EOL> raise ZeroDivisionError ( "<STR_LIT>" ) <EOL> if a >= DBL_MIN_OVERFLOW * b : <EOL> raise OverflowError ( "<STR_LIT>" ) <EOL> d = a . bit_length ( ) - b . bit_length ( ) <EOL> if d >= <NUM_LIT:0> and a >= <NUM_LIT:2> ** d * b or d < <NUM_LIT:0> and a * <NUM_LIT:2> ** - d >= b : <EOL> d += <NUM_LIT:1> <EOL> exp = max ( d , DBL_MIN_EXP ) - DBL_MANT_DIG <EOL> a , b = a << max ( - exp , <NUM_LIT:0> ) , b << max ( exp , <NUM_LIT:0> ) <EOL> q , r = divmod ( a , b ) <EOL> if <NUM_LIT:2> * r > b or <NUM_LIT:2> * r == b and q % <NUM_LIT:2> == <NUM_LIT:1> : <EOL> q += <NUM_LIT:1> <EOL> result = math . ldexp ( float ( q ) , exp ) <EOL> return - result if negative else result <EOL> class TrueDivisionTests ( unittest . TestCase ) : <EOL> def test ( self ) : <EOL> huge = <NUM_LIT:1> L << <NUM_LIT> <EOL> mhuge = - huge <EOL> self . assertEqual ( huge / huge , <NUM_LIT:1.0> ) <EOL> self . assertEqual ( mhuge / mhuge , <NUM_LIT:1.0> ) <EOL> self . assertEqual ( huge / mhuge , - <NUM_LIT:1.0> ) <EOL> self . assertEqual ( mhuge / huge , - <NUM_LIT:1.0> ) <EOL> self . assertEqual ( <NUM_LIT:1> / huge , <NUM_LIT:0.0> ) <EOL> self . assertEqual ( <NUM_LIT:1> L / huge , <NUM_LIT:0.0> ) <EOL> self . assertEqual ( <NUM_LIT:1> / mhuge , <NUM_LIT:0.0> ) <EOL> self . assertEqual ( <NUM_LIT:1> L / mhuge , <NUM_LIT:0.0> ) <EOL> self . assertEqual ( ( <NUM_LIT> * huge + ( huge >> <NUM_LIT:1> ) ) / huge , <NUM_LIT> ) <EOL> self . assertEqual ( ( <NUM_LIT> * mhuge + ( mhuge >> <NUM_LIT:1> ) ) / mhuge , <NUM_LIT> ) <EOL> self . assertEqual ( ( <NUM_LIT> * huge + ( huge >> <NUM_LIT:1> ) ) / mhuge , - <NUM_LIT> ) <EOL> self . assertEqual ( ( <NUM_LIT> * mhuge + ( mhuge >> <NUM_LIT:1> ) ) / huge , - <NUM_LIT> ) <EOL> self . assertEqual ( huge / ( huge << <NUM_LIT:1> ) , <NUM_LIT:0.5> ) <EOL> self . assertEqual ( ( <NUM_LIT> * huge ) / huge , <NUM_LIT> ) <EOL> namespace = { '<STR_LIT>' : huge , '<STR_LIT>' : mhuge } <EOL> for overflow in [ "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" ] : <EOL> with self . assertRaises ( OverflowError ) : <EOL> eval ( overflow , namespace ) <EOL> for underflow in [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" ] : <EOL> result = eval ( underflow , namespace ) <EOL> self . assertEqual ( result , <NUM_LIT:0.0> , '<STR_LIT>' <EOL> '<STR_LIT>' . format ( underflow ) ) <EOL> for zero in [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] : <EOL> with self . assertRaises ( ZeroDivisionError ) : <EOL> eval ( zero , namespace ) <EOL> def check_truediv ( self , a , b , skip_small = True ) : <EOL> """<STR_LIT>""" <EOL> a , b = long ( a ) , long ( b ) <EOL> if skip_small and max ( abs ( a ) , abs ( b ) ) < <NUM_LIT:2> ** DBL_MANT_DIG : <EOL> return <EOL> try : <EOL> expected = repr ( truediv ( a , b ) ) <EOL> except OverflowError : <EOL> expected = '<STR_LIT>' <EOL> except ZeroDivisionError : <EOL> expected = '<STR_LIT>' <EOL> try : <EOL> got = repr ( a / b ) <EOL> except OverflowError : <EOL> got = '<STR_LIT>' <EOL> except ZeroDivisionError : <EOL> got = '<STR_LIT>' <EOL> self . assertEqual ( expected , got , "<STR_LIT>" <EOL> "<STR_LIT>" . format ( a , b , expected , got ) ) <EOL> @ cpython_only <EOL> @ requires_IEEE_754 <EOL> def test_correctly_rounded_true_division ( self ) : <EOL> self . check_truediv ( <NUM_LIT> , <NUM_LIT:0> ) <EOL> self . check_truediv ( - <NUM_LIT> , <NUM_LIT:0> ) <EOL> self . check_truediv ( <NUM_LIT:0> , <NUM_LIT:3> ) <EOL> self . check_truediv ( <NUM_LIT:0> , - <NUM_LIT:3> ) <EOL> self . check_truediv ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> self . check_truediv ( <NUM_LIT> * <NUM_LIT> * <NUM_LIT:2> ** DBL_MAX_EXP , <NUM_LIT> ) <EOL> self . check_truediv ( <NUM_LIT> , <NUM_LIT> * <NUM_LIT:2> ** ( DBL_MANT_DIG - DBL_MIN_EXP ) ) <EOL> self . check_truediv ( <NUM_LIT> * <NUM_LIT:2> ** <NUM_LIT:100> , <NUM_LIT> ) <EOL> self . check_truediv ( <NUM_LIT> * <NUM_LIT:2> ** <NUM_LIT:30> , <NUM_LIT> * <NUM_LIT:7> ** <NUM_LIT> ) <EOL> bases = ( <NUM_LIT:0> , DBL_MANT_DIG , DBL_MIN_EXP , <EOL> DBL_MAX_EXP , DBL_MIN_EXP - DBL_MANT_DIG ) <EOL> for base in bases : <EOL> for exp in range ( base - <NUM_LIT:15> , base + <NUM_LIT:15> ) : <EOL> self . check_truediv ( <NUM_LIT> * <NUM_LIT:2> ** max ( exp , <NUM_LIT:0> ) , <NUM_LIT> * <NUM_LIT:2> ** max ( - exp , <NUM_LIT:0> ) ) <EOL> self . check_truediv ( <NUM_LIT> * <NUM_LIT:2> ** max ( exp , <NUM_LIT:0> ) , <NUM_LIT> * <NUM_LIT:2> ** max ( - exp , <NUM_LIT:0> ) ) <EOL> for m in [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:7> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:7> ** <NUM_LIT:100> , <EOL> - <NUM_LIT:1> , - <NUM_LIT:2> , - <NUM_LIT:5> , - <NUM_LIT> , - <NUM_LIT> , - <NUM_LIT> ** <NUM_LIT:50> ] : <EOL> for n in range ( - <NUM_LIT:10> , <NUM_LIT:10> ) : <EOL> self . check_truediv ( m * DBL_MIN_OVERFLOW + n , m ) <EOL> self . check_truediv ( m * DBL_MIN_OVERFLOW + n , - m ) <EOL> for n in range ( <NUM_LIT> ) : <EOL> self . check_truediv ( ( <NUM_LIT:2> ** DBL_MANT_DIG + <NUM_LIT:1> ) * <NUM_LIT> * <NUM_LIT:2> ** <NUM_LIT:200> + <NUM_LIT:2> ** n , <EOL> <NUM_LIT:2> ** DBL_MANT_DIG * <NUM_LIT> ) <EOL> self . check_truediv ( <NUM_LIT:1> , <NUM_LIT> ) <EOL> self . check_truediv ( <NUM_LIT> , <NUM_LIT> ) <EOL> for i in range ( <NUM_LIT:1000> ) : <EOL> self . check_truediv ( <NUM_LIT:10> ** ( i + <NUM_LIT:1> ) , <NUM_LIT:10> ** i ) <EOL> self . check_truediv ( <NUM_LIT:10> ** i , <NUM_LIT:10> ** ( i + <NUM_LIT:1> ) ) <EOL> for m in [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:4> , <NUM_LIT:7> , <NUM_LIT:8> , <NUM_LIT:16> , <NUM_LIT> , <NUM_LIT:32> , <NUM_LIT> , <NUM_LIT:7> ** <NUM_LIT:100> , <EOL> - <NUM_LIT:1> , - <NUM_LIT:2> , - <NUM_LIT:5> , - <NUM_LIT> , - <NUM_LIT> , - <NUM_LIT> ** <NUM_LIT:50> ] : <EOL> for n in range ( - <NUM_LIT:10> , <NUM_LIT:10> ) : <EOL> self . check_truediv ( <NUM_LIT:2> ** DBL_MANT_DIG * m + n , m ) <EOL> for n in range ( - <NUM_LIT:20> , <NUM_LIT:20> ) : <EOL> self . check_truediv ( n , <NUM_LIT:2> ** <NUM_LIT> ) <EOL> for M in [ <NUM_LIT:10> ** <NUM_LIT:10> , <NUM_LIT:10> ** <NUM_LIT:100> , <NUM_LIT:10> ** <NUM_LIT:1000> ] : <EOL> for i in range ( <NUM_LIT:1000> ) : <EOL> a = random . randrange ( <NUM_LIT:1> , M ) <EOL> b = random . randrange ( a , <NUM_LIT:2> * a + <NUM_LIT:1> ) <EOL> self . check_truediv ( a , b ) <EOL> self . check_truediv ( - a , b ) <EOL> self . check_truediv ( a , - b ) <EOL> self . check_truediv ( - a , - b ) <EOL> for _ in range ( <NUM_LIT> ) : <EOL> a_bits = random . randrange ( <NUM_LIT:1000> ) <EOL> b_bits = random . randrange ( <NUM_LIT:1> , <NUM_LIT:1000> ) <EOL> x = random . randrange ( <NUM_LIT:2> ** a_bits ) <EOL> y = random . randrange ( <NUM_LIT:1> , <NUM_LIT:2> ** b_bits ) <EOL> self . check_truediv ( x , y ) <EOL> self . check_truediv ( x , - y ) <EOL> self . check_truediv ( - x , y ) <EOL> self . check_truediv ( - x , - y ) <EOL> def test_main ( ) : <EOL> run_unittest ( TrueDivisionTests ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> test_main ( ) </s>
