python 地板除法(floor)和截断除法(trunc)
2017-04-04 19:47
344 查看
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 是返回 小于或者等于当前值的`最大整数`
相关文章推荐
- 《Modern Python Cookbook》(Python编程范例)笔记1.4真除法和向下截断除法(floor除法)
- python基础--除法、地板除、取余
- python3之数字(在实际应用中的数字--除法:传统除法、Floor除法和真除法)
- 【python】 int整型数的除法将返回地板除的整形,例如 99/100 = 0
- Python3中的真除和Floor除法用法分析
- Python3中的真除和Floor除法用法分析
- Python除法:传统除法、Floor除法及真除法
- python(50):python 向上取整 ceil 向下取整 floor 四舍五入 round
- R里面数字取整相关的操作有一组函数: ceiling ;floor;trunc; round; signif
- Python中的除法
- php中除法取整的方法(round,ceil,floor)
- python整数除法保留两位小数
- Python基础:Python2与Python3除法的区别
- delphi的取整函数round、trunc、ceil和floor
- python中的除法和取模 分类: python基础学习 2013-12-27 12:49 572人阅读 评论(0) 收藏
- python之除法获取真实的结果
- 低版本中Python除法运算小技巧
- Python中比较特别的除法运算和幂运算介绍
- Oracle ->> TRUNC, ROUND, CEIL, FLOOR
- 2、Python基础--除法、常用数学函数