python2 与 3:一般除法、除法取整、除法取余、精确减法与精确除法

python2

python2除法

  python2的除法,与c语言除法类似,两个整数相除结果一定是整数,除数与被除数有一个是浮点数,结果就是浮点数。因此当两个整数相除,结果又有小数时,由于结果是整数,所以小数部分会被省略,针对这种情况,要实现真正的除法,除数与被除数至少有一个应改为浮点数。(貌似python2.7开始,除法和python3除法一样了,没有仔细考究)

a = 3
b = 2
print a/b
print float(a)/b
print a/float(b)
print float(a)/float(b)

# 结果
1
1.5
1.5
1.5

python2除法取整

  加一个int(),进行强制类型转换即可。

a = 4.5
b = 3
print a/b
print int(a/b)

# 结果
1.5
1

python2除法取余

  利用%操作即可。

a = 5.5
b = 3
print a % b

# 结果
2.5

python3

python3除法

  python3的除法就直接使用"/",就可以了,不存在python2的情况

a = 3
b = 2
print(a/b)

# 结果
1.5

python3除法取整

  利用"//"操作

a = 5.5
b = 2
print(a//b)

# 结果
2.0

python3除法取余

  利用"%"操作

a = 5.5
b = 2
print(a%b)

# 结果
1.5

python3精确减法与精确除法

  我们先看看下面的输出

a = 0.9
b = 1

print(b-a)
print((b-a)/100)
print((0.1)/100)

# 结果
0.09999999999999998
0.0009999999999999998
0.001

  python计算要把数字先转为二进制,因为浮点数以二进制形式表示时的有穷性(利用乘K取余法,0.9的二进制无限延展),会导致计算误差。下面摘自python文档原话。

On most machines today, floats are approximated using a binary fraction with the numerator using the first 53 bits starting with the most significant bit and with the denominator as a power of two.

这不是只有python才有的bug,也不是你代码的bug,所有的编程语言都会有这个问题,当然可能很多编程语言会使用某些机制来规避这种显示。回到该blog的例子,0.9的二进制表示如下: 0.1110011001100110011001100110011001100110011001100110011 0.1110011001100110011001100110011001100110011001100110011 0.11100110011001100110011001100110011001100110011001100111-0.9的二进制表示为: 0.0001100110011001100110011001100110011001100110011001100 0.0001100110011001100110011001100110011001100110011001100 0.0001100110011001100110011001100110011001100110011001100转化过来就是0.09999999999999998
  这时候为了实现精确运算,可以使用类decimal。

import decimal
decimal.getcontext().prec = 10   # 取小数点后精度到第10位
a = 0.9
b = 1

print(decimal.Decimal(b)-decimal.Decimal(a))
print((decimal.Decimal(b)-decimal.Decimal(a))/100)
print((0.1)/100)

# 结果
0.1000000000
0.0010000000
0.001

  当然,直接使用近似函数round()也是可以的。

a = 0.9
b = 1

print(round(b-a, 10))
print(round((b-a)/100, 10))
print(round((0.1)/100, 10))

# 结果
0.1
0.001
0.001
  • 13
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个简单的实现: ```python import math class Vector3D: def __init__(self, x, y, z): self.x = x self.y = y self.z = z def __add__(self, other): return Vector3D(self.x + other.x, self.y + other.y, self.z + other.z) def __sub__(self, other): return Vector3D(self.x - other.x, self.y - other.y, self.z - other.z) def __mul__(self, value): if isinstance(value, Vector3D): return Vector3D(self.x * value.x, self.y * value.y, self.z * value.z) else: return Vector3D(self.x * value, self.y * value, self.z * value) def __truediv__(self, value): return Vector3D(self.x / value, self.y / value, self.z / value) def __floordiv__(self, value): return Vector3D(self.x // value, self.y // value, self.z // value) def __repr__(self): return f"Vector3D({self.x}, {self.y}, {self.z})" def length(self): return math.sqrt(self.x ** 2 + self.y ** 2 + self.z ** 2) def normalize(self): length = self.length() if length != 0: self.x /= length self.y /= length self.z /= length v1 = Vector3D(1, 2, 3) v2 = Vector3D(4, 5, 6) print(v1 + v2) # Vector3D(5, 7, 9) print(v1 - v2) # Vector3D(-3, -3, -3) print(v1 * 2) # Vector3D(2, 4, 6) print(v1 / 2) # Vector3D(0.5, 1.0, 1.5) print(v1 // 2) # Vector3D(0, 1, 1) ``` 在这个实现中,我们定义了一个 `Vector3D` 类,该类具有三个实例变量 `x`,`y` 和 `z`,代表向量的三个分量。 我们还定义了几个运算符重载方法,包括向量加法、向量减法、向量标量乘法、向量浮点除法和向量向下取整除法。这些方法都返回一个新的 `Vector3D` 对象。 我们还定义了 `length()` 方法来计算向量的长度,并定义了 `normalize()` 方法来将向量归一化为单位向量。 最后,我们在示例中展示了 `Vector3D` 类的一些用法。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值