2019년 9월 6일 금요일

Python[3 day]: Basic operators for mathematical calculations and mathematical functions

   Maybe, there is no need to explain the operators of + (addition), - (subtration), * (multiplication), / (division). In my tests, it was confirmed that the calculation of division returns the real number value even though you calculate only with integer numbers.


   In Python, modulus operator of % returns the remainder after the division calculation. If you type 100%3, Python interpreter divides 100 by 3 and returns the remainder of 1. Floor division operator of // returns the quotient after the division calculation. If you type 100//3, Python interpreter divides 100 by 3 and returns the quotient of 33.0. In other word, in the floor division, Python interpreter throws away the remainder after the division calculation.

    
   The operator of ** is used for the exponential (power) calculation in Python.


   Python provides many mathematical functions the same as other programming language. However, some mathematical functions available in Python are supported by the module called math, therefore, to use these functions, you should import math module within your program before they are called in your program.
   The method to import math module is very simple. All you have to do is that you type the command of import math before the function supported by math module is used in your program. The below table shows the mathematical functions required math module.


   However, if you use any function requiring math module, you should be careful with the notation of the function. For example, when you use sqrt function, its name is not sqrt, but math.sqrt. That is, module name(math)+period(.)+function name(sqrt). The way of this notation should be also kept whenever you import any other module provided in Python.


   From the above table, it is seen that the exponential calculation can be done not only by the operator of **, but also by the function of pow(x,y). The below is the calculation example to calculation of square root.


   Trigonometric functions such as sin, cos, tan etc. are also the important mathematical functions in science and engineering calculations.
   As the same as the previously explained mathematical functions, these trigonometric functions are also required math module for using them in a Python program. The below table shows the trigonometric functions supported by Python math module.


   When using the basic trigonometric functions (sin, cos, tan) in a Python program,  the angle for the trigonometric functions should be radian (not degree), which is the same with all other programming languages.
   Python is a very kind (user friendly) programming language because the last two functions are supported to convert radian to degree and vice versa. However, in my personal opinion, it is questionable whether the last two functions for converting angles are really necessary.
   Anyway, to use the trigonometric functions, never forget to input import math first, and then write math.sin( ). It is noticeable that Python provides the function of hypot(x,y), which is not seen in other programming languages. hypot(x,y) returns the hypotenuse length of a right angle triangle from Pythagorean theorem.



댓글 없음:

댓글 쓰기