python实数集_Python中的assertAlmostEqual浮点数集合的单元测试

下面是我如何实现一个泛型函数is_almost_equal(first, second):

首先,复制需要比较的对象(first和second),但不要精确复制:剪切在对象中遇到的任何浮点的不重要的小数位数。

既然已经有了first和second的副本,其中不重要的十进制数字已经消失,那么只需使用==运算符比较first和second。

假设我们有一个cut_insignificant_digits_recursively(obj, places)函数,它与obj重复,但只保留原始obj中每个浮点数的places最有意义的小数位数。以下是is_almost_equals(first, second, places)的工作实现:from insignificant_digit_cutter import cut_insignificant_digits_recursively

def is_almost_equal(first, second, places):

'''returns True if first and second equal.

returns true if first and second aren't equal but have exactly the same

structure and values except for a bunch of floats which are just almost

equal (floats are almost equal if they're equal when we consider only the

[places] most significant digits of each).'''

if first == second:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值