python两种除法区别以及向上向下取整

python两种除法

在Python中,有两种除法,一种除法是/:

10 / 3
3.3333333333333335

/除法计算结果是浮点数,即使是两个整数恰好整除,结果也是浮点数:

9/3
3.0

还有一种除法是//,称为地板除(floor),两个整数的除法仍然是整数:

10 // 3
3

整数的地板除//永远是整数,即使除不尽。要做精确的除法,使用/就可以。

因为//除法只取结果的整数部分,所以Python还提供一个余数运算,可以得到两个整数相除的余数:

10 % 3
3

另外//除可以看成math库中的floor方法(向下取整)

import math
math.floor(2.0)
math.floor(2.6666666666666665)
math.floor(-2.0)
math.floor(-2.6666666666666665)

2
2
-3
-3

如果想使用向上取整,可以使用math库中的ceil方法:

import math
print(math.ceil(2.0))
print(math.ceil(2.6666666666666665))
print(math.ceil(-2.0))
print(math.ceil(-2.6666666666666665))

2
3
-2
-2

原文地址:https://www.cnblogs.com/hj-SAMA/p/12275625.html

时间: 2024-08-14 07:12:10

python两种除法区别以及向上向下取整的相关文章

向上/向下取整和四舍五入编程实现

在看Guava Cache的实现源码时,其中有个向上取整操作,它的源码如下: int segmentCapacity = initialCapacity / segmentCount; if (segmentCapacity * segmentCount < initialCapacity) {   ++segmentCapacity; } 关于向上取整.向下取整.四舍五入等操作,有不同的实现,效率上讲,也是基本可以忽略不计,毕竟用的比较少,这里对向上/向下取整和四舍五入的编程实现进行一下整理.

向上向下取整、舍入

向下取整 floor (地板) import math math.floor(-2.2) # -3.0 math.floor(2.2) # 2.0 向上取整 ceil (天花板) import math  math.ceil(-2.2) # -2.0 math.ceil(2.2) # 3.0 舍入 round round(2.24, 1) # 2.2 round(2.26, 1) # 2.3 向上向下取整.舍入,布布扣,bubuko.com

oracle小数格式化 向上 向下取整

取整(向下取整): select floor(5.534) from dual;select trunc(5.534) from dual;上面两种用法都可以对数字5.534向下取整,结果为5. 如果要向上取整 ,得到结果为6,则应该用ceilselect ceil(5.534) from dual; 四舍五入: SELECT round(5.534) FROM dual;SELECT round(5.534,0) FROM dual;SELECT round(5.534,1) FROM dua

jquery向上向下取整

在用ajax进行分页查询时,分页的数目要用到取整函数 <script language="javascript"> var uu=Math.floor(5.36) //向下取整 结果为5 var uu=Math.floor(5.88) //结果为5 Math.ceil(5.33) //向上取整,结果为6 Math.round(5.55) //四舍五入 结果为6 math.round(5.22) //结果为5 </script>

Python 引入包的两种方式区别

1.import XXX仅仅是告诉我们需要使用这个包,但是你真正使用的时候,需要完整的导入这个包的全路径 比如: import wechat.views 在使用其中的hello函数的时候,需要 wechat.views.hello - 这个路径不能简略 2.from ... import ...就不需要指定父的路径了 比如: from wechat.views import *    #你可以使用views内部的变量 你就可以直接使用其中的hello hello 但是如果: from wecha

Python 之 向上取整、向下取整以及四舍五入

python 向上取整ceil 向下取整floor 四舍五入round,相面给出源码示例. import math #向上取整 print "math.ceil---" print "math.ceil(2.3) => ", math.ceil(2.3) print "math.ceil(2.6) => ", math.ceil(2.6) #向下取整 print "\nmath.floor---" print &q

python 向上取整ceil 向下取整floor 四舍五入round

#encoding:utf-8import math #向上取整print "math.ceil---"print "math.ceil(2.3) => ", math.ceil(2.3)print "math.ceil(2.6) => ", math.ceil(2.6) #向下取整print "\nmath.floor---"print "math.floor(2.3) => ", ma

【向上取整/向下取整】C语言向上或向下取整 函数

C语言有以下几种取整方法: 1.直接赋值给整数变量.如: int i = 2.5; 或 i = (int) 2.5; 这种方法采用的是舍去小数部分 2.C/C++中的整数除法运算符"/"本身就有取整功能(int / int),但是整数除法对负数的取整结果和使用的C编译器有关. 3.使用floor函数.floor(x)返回的是小于或等于x的最大整数.如: floor(2.5) = 2 floor(-2.5) = -3 4.使用ceil函数.ceil(x)返回的是大于x的最小整数.如: c

Why Python&#39;s Integer Division Floors ---- python int(6/-132)时答案不一致,向下取整

leetcode150题中有一个步骤: int(6/-132) == 0 or ==-1? 在自己本地python3环境跑是int(6/-132) =0,但是提交的时候确实-1. 查找相关资料解惑: Why Python's Integer Division Floors为何Python整除运算采用向下取整的规则 今天(又)有人问我,为什么Python中的整除(integer division)返回值向下取整(floor)而不是像C语言中那样向0取整. 在正整数范围内,两者并无实质差别,例如: