کامپایلر پایتون چیست
نویسنده : رضا قربانی | زمان انتشار : 09 اسفند 1399 ساعت 18:41
جهت انجام پروژه های دانشجویی و یا تمرینهای برنامه نویسی رشته کامپیوتر میتوانید به آی دی تلگرام زیر پیام دهید
@AlirezaSepand
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 | # Package initialisation frompypy.interpreter.mixedmodule importMixedModule classModule(MixedModule): appleveldefs={ 'factorial':'app_math.factorial' } interpleveldefs={ 'e' :'interp_math.get(space).w_e', 'pi' :'interp_math.get(space).w_pi', 'pow' :'interp_math.pow', 'cosh' :'interp_math.cosh', 'copysign' :'interp_math.copysign', 'ldexp' :'interp_math.ldexp', 'hypot' :'interp_math.hypot', 'tan' :'interp_math.tan', 'asin' :'interp_math.asin', 'fabs' :'interp_math.fabs', 'floor' :'interp_math.floor', 'sqrt' :'interp_math.sqrt', 'frexp' :'interp_math.frexp', 'degrees' :'interp_math.degrees', 'log' :'interp_math.log', 'log10' :'interp_math.log10', 'fmod' :'interp_math.fmod', 'atan' :'interp_math.atan', 'ceil' :'interp_math.ceil', 'sinh' :'interp_math.sinh', 'cos' :'interp_math.cos', 'tanh' :'interp_math.tanh', 'radians' :'interp_math.radians', 'sin' :'interp_math.sin', 'atan2' :'interp_math.atan2', 'modf' :'interp_math.modf', 'exp' :'interp_math.exp', 'expm1' :'interp_math.expm1', 'acos' :'interp_math.acos', 'isinf' :'interp_math.isinf', 'isnan' :'interp_math.isnan', 'trunc' :'interp_math.trunc', 'fsum' :'interp_math.fsum', 'asinh' :'interp_math.asinh', 'acosh' :'interp_math.acosh', 'atanh' :'interp_math.atanh', 'log1p' :'interp_math.log1p', 'expm1' :'interp_math.expm1', 'erf' :'interp_math.erf', 'erfc' :'interp_math.erfc', 'gamma' :'interp_math.gamma', 'lgamma' :'interp_math.lgamma', } importsys,math INFINITY=1e200*1e200 NAN=abs(INFINITY/INFINITY) defisinf(x): returnx==INFINITY orx==-INFINITY defisnan(v): returnv!=v defpositiveinf(x): returnisinf(x)andx>0.0 defnegativeinf(x): returnisinf(x)andx<0.0 deffinite(x): returnnotisinf(x)andnotisnan(x) unary_math_functions=['acos','asin','atan', 'ceil','cos','cosh','exp','fabs','floor', 'sin','sinh','sqrt','tan','tanh','log','log10', 'acosh','asinh','atanh','log1p','expm1'] binary_math_functions=['atan2','fmod','hypot','pow'] classMathTests: REGCASES=[] forname inunary_math_functions: try: input,output=(0.3,),getattr(math,name)(0.3) exceptAttributeError: # cannot test this function continue exceptValueError: input,output=(1.3,),getattr(math,name)(1.3) REGCASES.append((name,input,output)) IRREGCASES=[ ('atan2',(0.31,0.123),math.atan2(0.31,0.123)), ('fmod', (0.31,0.123),math.fmod(0.31,0.123)), ('hypot',(0.31,0.123),math.hypot(0.31,0.123)), ('pow', (0.31,0.123),math.pow(0.31,0.123)), ('pow', (-0.31,0.123),ValueError), ('pow', (-0.5,2.0),0.25), ('pow', (-0.5,1.0),-0.5), ('pow', (-0.5,0.0),1.0), ('pow', (-0.5,-1.0),-2.0), ('ldexp',(3.375,2),13.5), ('ldexp',(1.0,-10000),0.0), # underflow ('frexp',(-1.25,),lambdax:x==(-0.625,1)), ('modf', (4.25,),lambdax:x==(0.25,4.0)), ('modf', (-4.25,),lambdax:x==(-0.25,-4.0)), ('copysign',(1.5,0.0),1.5), ('copysign',(1.5,-0.0),-1.5), ('copysign',(1.5,INFINITY),1.5), ('copysign',(1.5,-INFINITY),-1.5), ('copysign',(1.5,NAN),1.5), ('copysign',(1.75,-NAN),-1.75), # special case for -NAN here ] OVFCASES=[ ('cosh',(9999.9,),OverflowError), ('sinh',(9999.9,),OverflowError), ('exp',(9999.9,),OverflowError), ('pow',(10.0,40000.0),OverflowError), ('ldexp',(10.0,40000),OverflowError), ('log',(0.0,),ValueError),#cpython does it this way ('log1p',(-1.0,),OverflowError), ('log',(-1.,),ValueError), ('log10',(0.0,),ValueError),#cpython does it this way ] INFCASES=[ ('acos',(INFINITY,),ValueError), ('acos',(-INFINITY,),ValueError), ('asin',(INFINITY,),ValueError), ('asin',(-INFINITY,),ValueError), ('atan',(INFINITY,),math.pi/2), ('atan',(-INFINITY,),-math.pi/2), ('atanh',(INFINITY,),ValueError), ('atanh',(-INFINITY,),ValueError), ('ceil',(INFINITY,),positiveinf), ('ceil',(-INFINITY,),negativeinf), ('cos',(INFINITY,),ValueError), ('cos',(-INFINITY,),ValueError), ('cosh',(INFINITY,),positiveinf), ('cosh',(-INFINITY,),positiveinf), ('exp',(INFINITY,),positiveinf), ('exp',(-INFINITY,),0.0), ('fabs',(INFINITY,),positiveinf), ('fabs',(-INFINITY,),positiveinf), ('floor',(INFINITY,),positiveinf), ('floor',(-INFINITY,),negativeinf), ('sin',(INFINITY,),ValueError), ('sin',(-INFINITY,),ValueError), ('sinh',(INFINITY,),positiveinf), ('sinh',(-INFINITY,),negativeinf), ('sqrt',(INFINITY,),positiveinf), ('sqrt',(-INFINITY,),ValueError), ('tan',(INFINITY,),ValueError), ('tan',(-INFINITY,),ValueError), ('tanh',(INFINITY,),1.0), ('tanh',(-INFINITY,),-1.0), ('log',(INFINITY,),positiveinf), ('log',(-INFINITY,),ValueError), ('log10',(INFINITY,),positiveinf), ('log10',(-INFINITY,),ValueError), ('frexp',(INFINITY,),lambdax:isinf(x[0])), ('ldexp',(INFINITY,3),positiveinf), ('ldexp',(-INFINITY,3),negativeinf), ('modf', (INFINITY,),lambdax:positiveinf(x[1])), ('modf', (-INFINITY,),lambdax:negativeinf(x[1])), ('pow',(INFINITY,0.0),1.0), ('pow',(INFINITY,0.001),positiveinf), ('pow',(INFINITY,-0.001),0.0), ('pow',(-INFINITY,0.0),1.0), ('pow',(-INFINITY,0.001),positiveinf), ('pow',(-INFINITY,-0.001),0.0), ('pow',(-INFINITY,3.0),negativeinf), ('pow',(-INFINITY,6.0),positiveinf), ('pow',(-INFINITY,-13.0),-0.0), ('pow',(-INFINITY,-128.0),0.0), ('pow',(1.001,INFINITY),positiveinf), ('pow',(1.0, INFINITY),1.0), ('pow',(0.999,INFINITY),0.0), ('pow',(-0.999,INFINITY),0.0), #('pow', (-1.0, INFINITY), 1.0, but strange, could also be -1.0), ('pow',(-1.001,INFINITY),positiveinf), ('pow',(1.001,-INFINITY),0.0), ('pow',(1.0, -INFINITY),1.0), #('pow', (0.999, -INFINITY), positiveinf, but get OverflowError), #('pow', (INFINITY, INFINITY), positiveinf, but get OverflowError), ('pow',(INFINITY,-INFINITY),0.0), ('pow',(-INFINITY,INFINITY),positiveinf), ] IRREGERRCASES=[ # ('atan2',(INFINITY,-2.3),math.pi/2), ('atan2',(INFINITY,0.0),math.pi/2), ('atan2',(INFINITY,3.0),math.pi/2), #('atan2', (INFINITY, INFINITY), ?strange), ('atan2',(2.1,INFINITY),0.0), ('atan2',(0.0,INFINITY),0.0), ('atan2',(-0.1,INFINITY),-0.0), ('atan2',(-INFINITY,0.4),-math.pi/2), ('atan2',(2.1,-INFINITY),math.pi), ('atan2',(0.0,-INFINITY),math.pi), ('atan2',(-0.1,-INFINITY),-math.pi), # ('fmod',(INFINITY,1.0),ValueError), ('fmod',(1.0,INFINITY),1.0), ('fmod',(1.0,-INFINITY),1.0), ('fmod',(INFINITY,INFINITY),ValueError), # ('hypot',(-INFINITY,1.0),positiveinf), ('hypot',(-2.3,-INFINITY),positiveinf), ] binary_math_functions=['atan2','fmod','hypot','pow'] NANCASES1=[ (name,(NAN,),isnan)forname inunary_math_functions] NANCASES2=[ (name,(NAN,0.1),isnan)forname inbinary_math_functions] NANCASES3=[ (name,(-0.2,NAN),isnan)forname inbinary_math_functions] NANCASES4=[ (name,(NAN,-INFINITY),isnan)forname inbinary_math_functions ifname!='hypot'] NANCASES5=[ (name,(INFINITY,NAN),isnan)forname inbinary_math_functions ifname!='hypot'] NANCASES6=[ ('frexp',(NAN,),lambdax:isnan(x[0])), ('ldexp',(NAN,2),isnan), ('hypot',(NAN,INFINITY),positiveinf), ('hypot',(NAN,-INFINITY),positiveinf), ('hypot',(INFINITY,NAN),positiveinf), ('hypot',(-INFINITY,NAN),positiveinf), ('modf',(NAN,),lambdax:(isnan(x[0])andisnan(x[1]))), ] # The list of test cases. Note that various tests import this, # notably in rpython/lltypesystem/module and in translator/c/test. TESTCASES=(REGCASES+IRREGCASES+OVFCASES+INFCASES+IRREGERRCASES +NANCASES1+NANCASES2+NANCASES3+NANCASES4+NANCASES5 +NANCASES6) defget_tester(expected): iftype(expected)istype(Exception): deftester(value): returnFalse elifcallable(expected): deftester(value): ok=expected(value) assertisinstance(ok,bool) returnok else: assertfinite(expected),"badly written test" deftester(got): gotsign=expectedsign=1 ifgot<0.0:gotsign=-gotsign ifexpected<0.0:expectedsign=-expectedsign returnfinite(got)and(got==expected and gotsign==expectedsign) returntester |
منبع: blog.faradars.org