剩下的在Python中使用Float [英] Remainder on Float in Python

查看:98
本文介绍了剩下的在Python中使用Float的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想向您展示python中的操作结果.我无法解释.

I just want to show you the results of the operations in python. I cannot explain.

>>> 1.0%1.0
0.0  (OK)
>>> 1.0%0.1
0.09999.... 
>>> 1.0%0.001
0.00999....
>>> 1.0 %0.0001
0.000999...

...等等.我需要让我理解'x%y'的余数是否为0.0的东西,即'y'将'x'精确除以N次,其中N是整数. 由于以前的行为,我不知道如何设置可能的容差来确定余数是否接近于0. 有帮助吗?

... and so on. I need something that allows me to understand whether the remainder of 'x%y' is 0.0, namely 'y' divides 'x' exactly N times, where N is an integer. Due to the previous behavior I don't know how to set a possible tolerance to determine if the remainder is next to 0. Any help?

推荐答案

此(长)响应说,使用decimal模块:

>>> from decimal import Decimal
>>> Decimal('3.5') % Decimal('0.1')
Decimal('0.0')
>>> print(Decimal('3.5') % Decimal('0.1'))
0.0
>>> (Decimal(7)/2) % (Decimal(1)/10)
Decimal('0.0')

问题本质上是由于系统中浮点数的表示,您可以在Internet上的任何位置以及链接的响应中阅读有关浮点数的信息.

The problem is essentially due to the representation of floats in the system, you can read stuff about that everywhere on the Internet, and in the response linked.

这篇关于剩下的在Python中使用Float的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆