|
| 1 | +#Mathematical Functions in Python | Set 1 (Numeric Functions) |
| 2 | + |
| 3 | +# 1)ceil() and floor() |
| 4 | +# importing "math" for mathematical operations |
| 5 | + |
| 6 | +'''import math |
| 7 | +a = 2.3 |
| 8 | + |
| 9 | +# returning the ceil of 2.3 |
| 10 | +print ("The ceil of 2.3 is : ", end="") |
| 11 | +print (math.ceil(a)) |
| 12 | + |
| 13 | +# returning the floor of 2.3 |
| 14 | +print ("The floor of 2.3 is : ", end="") |
| 15 | +print (math.floor(a))''' |
| 16 | + |
| 17 | +# 2) Python code to demonstrate the working of |
| 18 | +# fabs() and factorial() |
| 19 | + |
| 20 | +# importing "math" for mathematical operations |
| 21 | +'''import math |
| 22 | + |
| 23 | +a = -10 |
| 24 | +b= 5 |
| 25 | + |
| 26 | +# returning the absolute value. |
| 27 | +print ("The absolute value of -10 is : ", end="") |
| 28 | +print (math.fabs(a)) |
| 29 | + |
| 30 | +# returning the factorial of 5 |
| 31 | +print ("The factorial of 5 is : ", end="") |
| 32 | +print (math.factorial(b))''' |
| 33 | + |
| 34 | +# Python code to demonstrate the working of |
| 35 | +# copysign() and gcd() |
| 36 | + |
| 37 | +# importing "math" for mathematical operations |
| 38 | +'''import math |
| 39 | + |
| 40 | +a = -10 |
| 41 | +b = 5.5 |
| 42 | +c = 15 |
| 43 | +d = 5 |
| 44 | + |
| 45 | +# returning the copysigned value. |
| 46 | +print ("The copysigned value of -10 and 5.5 is : ", end="") |
| 47 | +print (math.copysign(5.5, -10)) |
| 48 | + |
| 49 | +# returning the gcd of 15 and 5 |
| 50 | +print ("The gcd of 5 and 15 is : ", end="") |
| 51 | +print (math.gcd(5,15))''' |
| 52 | + |
| 53 | +# Python code to demonstrate the working of |
| 54 | +# exp() and log() |
| 55 | + |
| 56 | +# importing "math" for mathematical operations |
| 57 | +'''import math |
| 58 | + |
| 59 | +# returning the exp of 4 |
| 60 | +print ("The e**4 value is : ", end="") |
| 61 | +print (math.exp(4)) |
| 62 | + |
| 63 | +# returning the log of 2,3 |
| 64 | +print ("The value of log 2 with base 3 is : ", end="") |
| 65 | +print (math.log(2,3))''' |
| 66 | + |
| 67 | +# Python code to demonstrate the working of |
| 68 | +# log2() and log10() |
| 69 | + |
| 70 | +# importing "math" for mathematical operations |
| 71 | +'''import math |
| 72 | + |
| 73 | +# returning the log2 of 16 |
| 74 | +print ("The value of log2 of 16 is : ", end="") |
| 75 | +print (math.log2(16)) |
| 76 | + |
| 77 | +# returning the log10 of 10000 |
| 78 | +print ("The value of log10 of 10000 is : ", end="") |
| 79 | +print (math.log10(10000))''' |
| 80 | + |
| 81 | +# Python code to demonstrate the working of |
| 82 | +# pow() and sqrt() |
| 83 | + |
| 84 | +# importing "math" for mathematical operations |
| 85 | +'''import math |
| 86 | + |
| 87 | +# returning the value of 3**2 |
| 88 | +print ("The value of 3 to the power 2 is : ", end="") |
| 89 | +print (math.pow(3,2)) |
| 90 | + |
| 91 | +# returning the square root of 25 |
| 92 | +print ("The value of square root of 25 : ", end="") |
| 93 | +print (math.sqrt(25))''' |
| 94 | + |
| 95 | +# Python code to demonstrate the working of |
| 96 | +# sin() and cos() |
| 97 | + |
| 98 | +# importing "math" for mathematical operations |
| 99 | +'''import math |
| 100 | + |
| 101 | +a = math.pi/6 |
| 102 | + |
| 103 | +# returning the value of sine of pi/6 |
| 104 | +print ("The value of sine of pi/6 is : ", end="") |
| 105 | +print (math.sin(a)) |
| 106 | + |
| 107 | +# returning the value of cosine of pi/6 |
| 108 | +print ("The value of cosine of pi/6 is : ", end="") |
| 109 | +print (math.cos(a))''' |
| 110 | + |
| 111 | +# Python code to demonstrate the working of |
| 112 | +# tan() and hypot() |
| 113 | + |
| 114 | +# importing "math" for mathematical operations |
| 115 | +'''import math |
| 116 | + |
| 117 | +a = math.pi/6 |
| 118 | +b = 3 |
| 119 | +c = 4 |
| 120 | + |
| 121 | +# returning the value of tangent of pi/6 |
| 122 | +print ("The value of tangent of pi/6 is : ", end="") |
| 123 | +print (math.tan(a)) |
| 124 | + |
| 125 | +# returning the value of hypotenuse of 3 and 4 |
| 126 | +print ("The value of hypotenuse of 3 and 4 is : ", end="") |
| 127 | +print (math.hypot(b,c))''' |
| 128 | + |
| 129 | +# Python code to demonstrate the working of |
| 130 | +# degrees() and radians() |
| 131 | + |
| 132 | +# importing "math" for mathematical operations |
| 133 | +'''import math |
| 134 | + |
| 135 | +a = math.pi/6 |
| 136 | +b = 30 |
| 137 | + |
| 138 | +# returning the converted value from radians to degrees |
| 139 | +print ("The converted value from radians to degrees is : ", end="") |
| 140 | +print (math.degrees(a)) |
| 141 | + |
| 142 | +# returning the converted value from degrees to radians |
| 143 | +print ("The converted value from degrees to radians is : ", end="") |
| 144 | +print (math.radians(b))''' |
| 145 | + |
| 146 | +# Python code to demonstrate the working of |
| 147 | +# gamma() |
| 148 | + |
| 149 | +# importing "math" for mathematical operations |
| 150 | +'''import math |
| 151 | + |
| 152 | +a = 4 |
| 153 | + |
| 154 | +# returning the gamma() of 4 |
| 155 | +print ("The gamma() of 4 is : ", end="") |
| 156 | +print (math.gamma(a))''' |
| 157 | + |
| 158 | +# Python code to demonstrate the working of |
| 159 | +# const. pi and e |
| 160 | + |
| 161 | +# importing "math" for mathematical operations |
| 162 | +'''import math |
| 163 | + |
| 164 | +# returning the value of const. pi |
| 165 | +print ("The value of const. pi is : ", end="") |
| 166 | +print (math.pi) |
| 167 | + |
| 168 | +# returning the value of const. e |
| 169 | +print ("The value of const. e is : ", end="") |
| 170 | +print (math.e)''' |
| 171 | + |
| 172 | +# Python code to demonstrate the working of |
| 173 | +# inf, nan, isinf(), isnan() |
| 174 | + |
| 175 | +# importing "math" for mathematical operations |
| 176 | +'''import math |
| 177 | + |
| 178 | +# checking if number is nan |
| 179 | +if (math.isnan(math.nan)): |
| 180 | + print ("The number is nan") |
| 181 | +else : print ("The number is not nan") |
| 182 | + |
| 183 | +# checking if number is positive infinity |
| 184 | +if (math.isinf(math.inf)): |
| 185 | + print ("The number is positive infinity") |
| 186 | +else : print ("The number is not positive infinity")''' |
| 187 | + |
| 188 | + |
| 189 | + |
0 commit comments