如何将浮点数与Python中的其他两个浮点数进行比较?

发布于 2025-02-01 04:22:54 字数 442 浏览 3 评论 0原文

当我进行以下操作以比较小数值是否在两个值之间时,我会得到不一致的结果。

x = 1.87

if 1.0 >= x < 2.0:
    print("x: ", x, "; cat: high")
    return 'high'
elif 2.0 >= x < 3.0: 
    print("x: ", x, "; cat: medium")
    return 'medium'
elif 3.0 >= x <= 5.0:
    print("x: ", x, "; cat: low")
    return 'low'
else:
    return None

结果,我获得了结果。我检查了其他值,而它们都不匹配。我在Math.isclose()上找到了文章,但这对我没有帮助。我该如何解决?

When I do the following to compare if the decimal value is between the two values, I get inconsistent results.

x = 1.87

if 1.0 >= x < 2.0:
    print("x: ", x, "; cat: high")
    return 'high'
elif 2.0 >= x < 3.0: 
    print("x: ", x, "; cat: medium")
    return 'medium'
elif 3.0 >= x <= 5.0:
    print("x: ", x, "; cat: low")
    return 'low'
else:
    return None

I get the medium returned as a result. I checked with other values, and none of them matched. I found articles on math.isclose(), but it doesn't help me. How do I solve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文