1+ from highpymath import log as _log
2+ from highpymath import exp as _exp
3+ from highpymath import MathTypeError , MathValueError
4+
5+ def fast_mul (a : any , b : any , return_int : bool = False , return_string : bool = False ):
6+ """
7+ Fast multiplication of two numbers
8+ """
9+ return_float = True
10+ if return_int :
11+ return_float = False
12+ if not isinstance (a , (int , float )):
13+ raise MathTypeError ("a must be a number" )
14+ if not isinstance (b , (int , float )):
15+ raise MathTypeError ("b must be a number" )
16+ if isinstance (a , int ):
17+ a = float (a )
18+ if isinstance (b , int ):
19+ b = float (b )
20+ _result = _log (base = a , power = 10 ) + _log (base = b , power = 10 )
21+ _result = _exp (base = 10 , power = _result )
22+ if return_int :
23+ _result = int (_result )
24+ elif return_float :
25+ _result = float (_result )
26+ if return_string :
27+ _result = str (_result )
28+ return _result
29+
30+ def fast_div (a : any , b : any , return_int : bool = False , return_string : bool = False ):
31+ """
32+ Fast division of two numbers
33+ """
34+ return_float = True
35+ if return_int :
36+ return_float = False
37+ if not isinstance (a , (int , float )):
38+ raise MathTypeError ("a must be a number" )
39+ if not isinstance (b , (int , float )):
40+ raise MathTypeError ("b must be a number" )
41+ if isinstance (a , int ):
42+ a = float (a )
43+ if isinstance (b , int ):
44+ b = float (b )
45+ _result = _log (base = a , power = 10 ) - _log (base = b , power = 10 )
46+ _result = _exp (base = 10 , power = _result )
47+ if return_int :
48+ _result = int (_result )
49+ elif return_float :
50+ _result = float (_result )
51+ if return_string :
52+ _result = str (_result )
53+ return _result
54+
55+ def fast_exp (base : any , power : any = 2 , return_int : bool = False , return_string : bool = False ):
56+ """
57+ Fast exponentiation of a number
58+ """
59+ return_float = True
60+ if return_int :
61+ return_float = False
62+ if not isinstance (base , (int , float )):
63+ raise MathTypeError ("base must be a number" )
64+ if not isinstance (power , (int , float )):
65+ raise MathTypeError ("power must be a number" )
66+ if isinstance (base , int ):
67+ base = float (base )
68+ if isinstance (power , int ):
69+ power = float (power )
70+ _start = 1
71+ _result = 0
72+ while _start < power :
73+ _result += _log (base = base , power = 10 )
74+ _start += 1
75+ _result = _exp (base = 10 , power = _result )
76+ if return_int :
77+ _result = int (_result )
78+ elif return_float :
79+ _result = float (_result )
80+ if return_string :
81+ _result = str (_result )
82+ return _result
0 commit comments