math.modf
Python math.modf()方法 (Python math.modf() method)
math.modf() method is a library method of math module, it is used to get the fractional part and integer part of a number. It accepts a number (integer or float) and returns a tuple that contains fractional part and integer part of the number.
math.modf()方法是数学模块的库方法,用于获取数字的小数部分和整数部分。 它接受一个数字(整数或浮点数),并返回一个包含该数字的小数部分和整数部分的元组。
Note:
注意:
The integer part of the number also returns in float
数字的整数部分也以float形式返回
If the number is an integer, the fractional part will be 0.0
如果数字是整数,则小数部分将是0.0
If the number is a negative number, both of the parts will be negative
如果数字为负数,则两个部分均为负数
If anything is passed except the number, the method returns a type error "TypeError: a float is required"
如果传递了除数字以外的任何内容,则该方法返回类型错误“ TypeError:需要浮点数”
Syntax of math.modf() method:
math.modf()方法的语法:
math.modf(n)
Parameter(s): n – an integer or a float number.
参数: n-整数或浮点数。
Return value: tuple – it returns a tuple that contains fractional part and integer part of the number n.
返回值: tuple –返回一个元组,其中包含数字n的小数部分和整数部分。
Example:
例:
Input:
a = 10.23
# function call
print(math.modf(a))
Output:
(0.23000000000000043, 10.0)
Python代码演示math.modf()方法的示例 (Python code to demonstrate example of math.modf() method)
# Python code demonstrate example of
# math.modf() method
# importing math module
import math
# numbers
a = 10
b = 10.23
c = -10
d = -10.23
# printing the fractional and integer part
# of the numbers by using math.modf()
print("modf(a): ", math.modf(a))
print("modf(b): ", math.modf(b))
print("modf(c): ", math.modf(c))
print("modf(d): ", math.modf(d))
Output
输出量
modf(a): (0.0, 10.0)
modf(b): (0.23000000000000043, 10.0)
modf(c): (-0.0, -10.0)
modf(d): (-0.23000000000000043, -10.0)
翻译自: https://www.includehelp.com/python/math-modf-method-with-example.aspx
math.modf