<s> from __future__ import absolute_import <EOL> import math <EOL> from rpython . rlib import rfloat <EOL> from topaz . module import ModuleDef , ClassDef <EOL> from topaz . objects . exceptionobject import W_StandardError , new_exception_allocate <EOL> class Math ( object ) : <EOL> moduledef = ModuleDef ( "<STR_LIT>" ) <EOL> @ moduledef . setup_module <EOL> def setup_module ( space , w_mod ) : <EOL> space . set_const ( w_mod , "<STR_LIT>" , space . newfloat ( math . pi ) ) <EOL> space . set_const ( w_mod , "<STR_LIT:E>" , space . newfloat ( math . e ) ) <EOL> space . set_const ( w_mod , "<STR_LIT>" , space . getclassfor ( W_DomainError ) ) <EOL> @ moduledef . function ( "<STR_LIT>" , value = "<STR_LIT:float>" ) <EOL> def method_acos ( self , space , value ) : <EOL> return space . newfloat ( math . acos ( value ) ) <EOL> @ moduledef . function ( "<STR_LIT>" , value = "<STR_LIT:float>" ) <EOL> def method_acosh ( self , space , value ) : <EOL> try : <EOL> res = math . acosh ( value ) <EOL> except ValueError : <EOL> raise space . error ( space . getclassfor ( W_DomainError ) , '<STR_LIT>' ) <EOL> return space . newfloat ( res ) <EOL> @ moduledef . function ( "<STR_LIT>" , value = "<STR_LIT:float>" ) <EOL> def method_asin ( self , space , value ) : <EOL> return space . newfloat ( math . asin ( value ) ) <EOL> @ moduledef . function ( "<STR_LIT>" , value = "<STR_LIT:float>" ) <EOL> def method_asinh ( self , space , value ) : <EOL> return space . newfloat ( math . asinh ( value ) ) <EOL> @ moduledef . function ( "<STR_LIT>" , value = "<STR_LIT:float>" ) <EOL> def method_atan ( self , space , value ) : <EOL> return space . newfloat ( math . atan ( value ) ) <EOL> @ moduledef . function ( "<STR_LIT>" , value1 = "<STR_LIT:float>" , value2 = "<STR_LIT:float>" ) <EOL> def method_atan2 ( self , space , value1 , value2 ) : <EOL> return space . newfloat ( math . atan2 ( value1 , value2 ) ) <EOL> @ moduledef . function ( "<STR_LIT>" , value = "<STR_LIT:float>" ) <EOL> def method_atanh ( self , space , value ) : <EOL> try : <EOL> res = math . atanh ( value ) <EOL> except ValueError : <EOL> if value == <NUM_LIT:1.0> or value == - <NUM_LIT:1.0> : <EOL> res = rfloat . copysign ( rfloat . INFINITY , value ) <EOL> else : <EOL> raise space . error ( space . getclassfor ( W_DomainError ) , '<STR_LIT>' ) <EOL> return space . newfloat ( res ) <EOL> @ moduledef . function ( "<STR_LIT>" , value = "<STR_LIT:float>" ) <EOL> def method_cbrt ( self , space , value ) : <EOL> if value < <NUM_LIT:0> : <EOL> return space . newfloat ( - math . pow ( - value , <NUM_LIT:1.0> / <NUM_LIT> ) ) <EOL> else : <EOL> return space . newfloat ( math . pow ( value , <NUM_LIT:1.0> / <NUM_LIT> ) ) <EOL> @ moduledef . function ( "<STR_LIT>" , value = "<STR_LIT:float>" ) <EOL> def method_cos ( self , space , value ) : <EOL> return space . newfloat ( math . cos ( value ) ) <EOL> @ moduledef . function ( "<STR_LIT>" , value = "<STR_LIT:float>" ) <EOL> def method_cosh ( self , space , value ) : <EOL> try : <EOL> res = math . cosh ( value ) <EOL> except OverflowError : <EOL> res = rfloat . copysign ( rfloat . INFINITY , value ) <EOL> return space . newfloat ( res ) <EOL> @ moduledef . function ( "<STR_LIT>" , value = "<STR_LIT:float>" ) <EOL> def method_exp ( self , space , value ) : <EOL> return space . newfloat ( math . exp ( value ) ) <EOL> @ moduledef . function ( "<STR_LIT>" , value = "<STR_LIT:float>" ) <EOL> def method_frexp ( self , space , value ) : <EOL> mant , exp = math . frexp ( value ) <EOL> w_mant = space . newfloat ( mant ) <EOL> w_exp = space . newint ( exp ) <EOL> return space . newarray ( [ w_mant , w_exp ] ) <EOL> @ moduledef . function ( "<STR_LIT>" , value = "<STR_LIT:float>" ) <EOL> def method_gamma ( self , space , value ) : <EOL> try : <EOL> res = rfloat . gamma ( value ) <EOL> except ValueError : <EOL> if value == <NUM_LIT:0.0> : <EOL> res = rfloat . copysign ( rfloat . INFINITY , value ) <EOL> else : <EOL> raise space . error ( space . getclassfor ( W_DomainError ) , '<STR_LIT>' ) <EOL> except OverflowError : <EOL> res = rfloat . INFINITY <EOL> return space . newfloat ( res ) <EOL> @ moduledef . function ( "<STR_LIT>" , value = "<STR_LIT:float>" ) <EOL> def method_lgamma ( self , space , value ) : <EOL> try : <EOL> res = rfloat . lgamma ( value ) <EOL> except ( ValueError , OverflowError ) : <EOL> res = rfloat . INFINITY <EOL> gamma = ( <NUM_LIT:1> if value == - <NUM_LIT:1> or math . isnan ( value ) else <EOL> space . float_w ( space . send ( self , "<STR_LIT>" , [ space . newfloat ( value ) ] ) ) ) <EOL> sign = <NUM_LIT:1> if gamma > <NUM_LIT:0> else - <NUM_LIT:1> if gamma < <NUM_LIT:0> else <NUM_LIT:0> <EOL> return space . newarray ( [ space . newfloat ( res ) , space . newint ( sign ) ] ) <EOL> @ moduledef . function ( "<STR_LIT>" , value1 = "<STR_LIT:float>" , value2 = "<STR_LIT:float>" ) <EOL> def method_hypot ( self , space , value1 , value2 ) : <EOL> return space . newfloat ( math . hypot ( value1 , value2 ) ) <EOL> @ moduledef . function ( "<STR_LIT>" , value1 = "<STR_LIT:float>" , value2 = "<STR_LIT:int>" ) <EOL> def method_ldexp ( self , space , value1 , value2 ) : <EOL> return space . newfloat ( math . ldexp ( value1 , value2 ) ) <EOL> @ moduledef . function ( "<STR_LIT>" , value = "<STR_LIT:float>" , base = "<STR_LIT:float>" ) <EOL> def method_log ( self , space , value , base = math . e ) : <EOL> try : <EOL> res = <NUM_LIT:0.0> <EOL> if base == math . e : <EOL> res = math . log ( value ) <EOL> else : <EOL> res = math . log ( value ) / math . log ( base ) <EOL> except ValueError : <EOL> if value == <NUM_LIT:0.0> : <EOL> res = float ( - rfloat . INFINITY ) <EOL> else : <EOL> raise space . error ( space . getclassfor ( W_DomainError ) , '<STR_LIT>' ) <EOL> return space . newfloat ( res ) <EOL> @ moduledef . function ( "<STR_LIT>" , value = "<STR_LIT:float>" ) <EOL> def method_log10 ( self , space , value ) : <EOL> try : <EOL> res = math . log10 ( value ) <EOL> except ValueError : <EOL> if value == <NUM_LIT:0.0> : <EOL> res = float ( - rfloat . INFINITY ) <EOL> else : <EOL> raise space . error ( space . getclassfor ( W_DomainError ) , '<STR_LIT>' ) <EOL> return space . newfloat ( res ) <EOL> @ moduledef . function ( "<STR_LIT>" , value = "<STR_LIT:float>" ) <EOL> def method_log2 ( self , space , value ) : <EOL> try : <EOL> res = math . log ( value ) / math . log ( <NUM_LIT:2> ) <EOL> except ValueError : <EOL> if value == <NUM_LIT:0.0> : <EOL> res = float ( - rfloat . INFINITY ) <EOL> else : <EOL> raise space . error ( space . getclassfor ( W_DomainError ) , '<STR_LIT>' ) <EOL> return space . newfloat ( res ) <EOL> @ moduledef . function ( "<STR_LIT>" , value = "<STR_LIT:float>" ) <EOL> def method_sin ( self , space , value ) : <EOL> return space . newfloat ( math . sin ( value ) ) <EOL> @ moduledef . function ( "<STR_LIT>" , value = "<STR_LIT:float>" ) <EOL> def method_sinh ( self , space , value ) : <EOL> try : <EOL> res = math . sinh ( value ) <EOL> except OverflowError : <EOL> res = rfloat . copysign ( rfloat . INFINITY , value ) <EOL> return space . newfloat ( res ) <EOL> @ moduledef . function ( "<STR_LIT>" , value = "<STR_LIT:float>" ) <EOL> def method_sqrt ( self , space , value ) : <EOL> return space . newfloat ( math . sqrt ( value ) ) <EOL> @ moduledef . function ( "<STR_LIT>" , value = "<STR_LIT:float>" ) <EOL> def method_tan ( self , space , value ) : <EOL> try : <EOL> res = math . tan ( value ) <EOL> except ValueError : <EOL> res = rfloat . NAN <EOL> return space . newfloat ( res ) <EOL> @ moduledef . function ( "<STR_LIT>" , value = "<STR_LIT:float>" ) <EOL> def method_tanh ( self , space , value ) : <EOL> return space . newfloat ( math . tanh ( value ) ) <EOL> @ moduledef . function ( "<STR_LIT>" , value = "<STR_LIT:float>" ) <EOL> def method_erf ( self , space , value ) : <EOL> return space . newfloat ( rfloat . erf ( value ) ) <EOL> @ moduledef . function ( "<STR_LIT>" , value = "<STR_LIT:float>" ) <EOL> def method_erfc ( self , space , value ) : <EOL> return space . newfloat ( rfloat . erfc ( value ) ) <EOL> class W_DomainError ( W_StandardError ) : <EOL> classdef = ClassDef ( "<STR_LIT>" , W_StandardError . classdef ) <EOL> method_allocate = new_exception_allocate ( classdef ) </s>
