
Table of Contents(目次)
math functions
Python comes with a standard set of “mathematical functions.” These include various arithmetic operations on numbers and trigonometric functions such as sin and cos. In this lecture, we will focus on introducing the functions that are most commonly used.
Some of you may not be good at math, but here, you only need to understand (1) that Python has math functions and (2) the prominent examples of how to use them. Please feel free to take this course. (Many programmers never use math functions, so you don't need to memorize everything introduced here.)
Some functions cannot be covered here. To check all available functions, please refer to the official documentation.
https://docs.python.org/3.13/library/math.html
ceil, floor
First, let's learn the ceil and floor functions.
ceil is short for “Ceiling.” This always returns an integer by “rounding up” when converting a floating-point number to an integer. floor always returns an integer by “rounding down” when converting a floating-point number to an integer. ceil and floor cannot specify digits like round.
Now, let's try running the following code. ceil rounds 5.5 up, so “6” is returned, and floor rounds 5.5 down, so “5” is returned.
import math
result_ceil1 = math.ceil(5.5) # Ceiling
result_floor1 = math.floor(5.5) # Floor
print(result_ceil1) # 6
print(result_floor1) # 5
Regarding the code explanation, when using mathematical functions, you need to add the line “import math” to import the “math module.” This tells Python, “We will be using a set of mathematical functions, so please import them.”
(We will explain modules and import in detail in a separate lecture, so for now, just understand that you need to declare that you want to “import” the “set of functions (module)” of mathematical functions.)
When using each function, write the module name and function name connected by a dot (e.g., math.function_name()
). This is equivalent to declaring to Python that you are using the ceil function from the math module.
The rest of the code above is the same as what we have learned so far. Enter the numbers you want to process in the parentheses of the function, assign the results of the processing to variables, and finally print them out.
Next, pass negative floating-point numbers to ceil and floor.
import math
result_ceil2 = math.ceil(-5.5) # Ceiling
result_floor2 = math.floor(-5.5) # Floor
print(result_ceil2) # -5
print(result_floor2) # -6
ceil and floor work the same way, even with negative values. “math.ceil(-5.5)” rounds up and returns “-5,” while “math.floor(-5.5)” rounds down and returns “-6.”
modf
The modf function separates the integer part and the decimal part of a number. Let's try executing the following code.
import math
result = math.modf(5.5)
print(result) # (0.5, 5.0)
When you execute modf, the result is returned as a number enclosed in parentheses, such as “(decimal part, integer part).” In Python, a data type that holds multiple values in parentheses is called a “tuple.”If you specify “5.5” inside the parentheses of modf, it will be separated into the fractional part “0.5” and the integer part “5.0,” and the result will be returned as “(0.5, 5.0).”
(We will learn more about tuples in a separate lecture, so for now, just remember that they return multiple numerical values.)
If you want to divide the variable into a decimal part and an integer part and assign them to each variable, you can write it as follows.
import math
d, i = math.modf(5.5) # (0.5, 5.0)
print("decimal:", d) # decimal part
print("integer:", i) # integer part
The difference from the previous code is that the assignment to the result variable has been rewritten to assign values to two variables, as in d, i =
. This format is used to assign each value returned in the form of a tuple(“(0.5, 5.0)”) to the respective variables in order from left to right. In this case, the value “0.5” is assigned to the variable “d,” and the value “5.0” is assigned to the variable “i.”
fabs, copysign
fabs is a function that returns the absolute value. “f” is an abbreviation for float, and “abs” is an abbreviation for absolute. It is a function that returns the absolute value and is compatible with floats. Enter the number for which you want to find the absolute value in parentheses. (The value you enter can be an integer.)
copysign specifies the “value” and “sign” within parentheses. It behaves as follows: “Copy the sign of the absolute value of the ‘value’.”
Let's run the code and verify the behavior.
import math
result_fabs = math.fabs(-5.5) # absolute
result_copysign = math.copysign(-5.5, -0.0)
print(result_fabs) # 5.5
print(result_copysign) # -5.5
Executing “math.fabs(-5.5)” returns the absolute value “5.5”.
“math.copysign(-5.5, -0.0)” copies the absolute value (“5.5”) of “-5.5” and the sign (“-0.0”) of “-5.5”, resulting in “-5.5”.(To specify the negative sign, write it as “-0.0”.) If you write “math.copysign(-5.5, 0.0)”, it copies the absolute value (“5.5”) of “-5.5” and the sign (“0.0”, which is implicitly positive), so the result is “5.5”.
If you always want to return the positive value of the absolute value, use “fabs.” If the sign may change depending on the situation, use “copysign.”
pi, sin, cos
Mathematical functions have “constants” available. Constants are values that are predetermined and do not change. For example, in Python, mathematical functions include the circumference ratio pi (π, approximately 3.1415…).
Write the following code to confirm the value of the constant pi.
import math
print(math.pi) # 3.141592653589793
By writing “math.pi,” you can use the value of the constant. (Unlike functions, you do not need to include parentheses.) When you print this, you can confirm that the value is set to “3.141592653589793.” By using such constants, programmers no longer need to write code like “pi = 3.141592653589793.”
Let's look at an example of using “math.pi.” Since mathematical functions such as sin and cos are available, let's write and execute the following code.
import math
result_sin = math.sin(math.pi / 2) # Sine
result_cos = math.cos(math.pi) # Cosine
print(result_sin) # 1.0
print(result_cos) # -1.0
“math.sin(math.pi / 2)” is equivalent to calculating “math.sin(3.1415... / 2)” and the result is “1.0.” “math.cos(math.pi)” is equivalent to calculating “math.cos(3.1415...)” and the result is “-1.0.”