如何在python中使用八倍精度

2024-05-13 19:16:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我遇到这个问题是为了检查一个数是否可以被5整除。你知道吗

我所做的许多解决方案之一是

def f(x):
     return x/5==x//5

运行正常,最多2^53-1或10^17位数值。更重要的是它给了我错误。你知道吗

我发现是因为浮点数错误。 IEEE_754#Basic_and_interchange_formats

在链接中,给定了二进制256(八倍精度)。你知道吗

在python中没有找到任何有用的实现方法。你知道吗

另外,我不需要一个方法来检查一个数是否可以被5整除。你知道吗


Tags: and方法returnbasic链接def错误二进制
1条回答
网友
1楼 · 发布于 2024-05-13 19:16:47

“默认精度”意味着您可以选择不同的精度。你不能。你知道吗

https://docs.python.org/3/reference/datamodel.html

numbers.Real (float)

These represent machine-level double precision floating point numbers. You are at the mercy of the underlying machine architecture (and C or Java implementation) for the accepted range and handling of overflow. Python does not support single-precision floating point numbers; the savings in processor and memory usage that are usually the reason for using these are dwarfed by the overhead of using objects in Python, so there is no reason to complicate the language with two kinds of floating point numbers.

不能保证您甚至拥有ieee754浮点值。你知道吗

相关问题 更多 >