math.modf_带有Python示例的math.modf()方法

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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值