python中地板除是什么意思_python 地板除法(floor)和截斷除法(trunc)

math.floor() & math.trunc()

math.floor 和 math.trunc的官方不同版本的介紹如下:math.floor:

python2.7: Return the floor of x as a float, the largest integer value less than or equal to x.

python3.5: Return the floor of x, the largest integer less than or equal to x. If x is not a float, delegates to x.__floor__(), which should return an Integral value.

math.trunc:

python2.7: Return the Real value x truncated to an Integral (usually a long integer). Uses the __trunc__ method.

python3.5: Return the Real value x truncated to an Integral (usually an integer). Delegates to x.__trunc__().

math.ceil:

python2.7: Return the ceiling of x as a float, the smallest integer value greater than or equal to x.

python3.5: Return the ceiling of x, the smallest integer greater than or equal to x. If x is not a float, delegates to x.__ceil__(), which should return an Integral value.

先上具體的例子:

腳本中附加了math.ceil函數(和math.floor對立)root@pts/4 $ cat python_math_floor_and_trunc.py

#!/usr/bin/env pyton

#-*- codingL utf-8 -*-

import math

a = 3

b = 3.12

c = 3.67

print('-'*20+'a = 3; b = 3.12; c = 3.67'+'-'*20)

print('-'*20+'math.ceil a b c'+'-'*20)

print(math.ceil(a))

print(math.ceil(b))

print(math.ceil(c))

print('-'*20+'math.floor a b c'+'-'*20)

print(math.floor(a))

print(math.floor(b))

print(math.floor(c))

print('-'*20+'math.trunc a b c'+'-'*20)

print(math.trunc(a))

print(math.trunc(b))

print(math.trunc(c))

在Python2.7下的運行結果是:root@pts/4 $ python python_math_floor_and_trunc.py

--------------------a = 3; b = 3.12; c = 3.67--------------------

--------------------math.ceil a b c--------------------

3.0

4.0

4.0

--------------------math.floor a b c--------------------

3.0

3.0

3.0

--------------------math.trunc a b c--------------------

3

3

3

在Python3.5下的運行結果是:root@pts/5 $ python python_math_floor_and_trunc.py

--------------------a = 3; b = 3.12; c = 3.67--------------------

--------------------math.ceil a b c--------------------

3

4

4

--------------------math.floor a b c--------------------

3

3

3

--------------------math.trunc a b c--------------------

3

3

3總結來說:

math.trunc 不管是在Python2.7或者是Python3.5版本中最終的結果都是`截斷`之后的`整數`

math.ceil/math.floor 在Python2.7版本返回值是`浮點數`;在python3.5版本是`整數`

math.ceil 是返回 大於或者等於當前值的`最小整數`

math.floor 是返回 小於或者等於當前值的`最大整數`

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值