在Python中使用更多小数 [英] Use more decimals in Python

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

问题描述

我已经有一个使用二分法确定某物值的代码,问题是我需要一个如此精确的值,超过15个小数,并且在某些时候python停止获取较小的数字

I already have a code which uses the bisection method to determine the value of something, the problem is that I need a value so precise, more than 15 decimals, and at some point python stops getting smaller digits

我知道Decimals库,但是我真的必须将代码中的每个参数都重写为Decimals(parameter)吗?因为我有很多参数

I am aware of the Decimals library but do I really have to rewrite every parameter in the code as Decimals(parameter) ? because I have lots of parameters

有没有一种方法可以将代码中的每个浮点数普遍转换为Decimals?还是有一种方法可以使用另一种方法一起解决这个问题?

is there a way to convert every float in the code to Decimals universally? or is there a way to solve this problem all together using a different way ?

62.5
31.25
15.625
23.4375
27.34375
25.390625
26.3671875
25.87890625
25.634765625
25.7568359375
25.81787109375
25.787353515625
25.7720947265625
25.76446533203125
25.760650634765625
25.758743286132812
25.75969696044922
25.759220123291016
25.758981704711914
25.758862495422363
25.758802890777588
25.758832693099976
25.75881779193878
25.75882524251938
25.75882151722908
25.75882337987423
25.758824311196804
25.75882477685809
25.758825009688735
25.758825126104057
25.758825184311718
25.758825155207887
25.758825140655972
25.758825133380014
25.758825137017993
25.758825138836983
25.758825139746477
25.758825140201225
25.75882513997385
25.758825139860164
25.75882513980332
25.7588251397749
25.75882513978911
25.758825139796215
25.758825139792663
25.758825139790886
25.758825139791774
25.75882513979222
25.758825139792442
25.75882513979233
25.758825139792386
25.758825139792414
25.7588251397924
25.758825139792407
25.758825139792403
25.758825139792407
25.758825139792407
25.758825139792407
25.758825139792407
25.758825139792407
25.758825139792407
25.758825139792407

您有时会看到Python停止更改值,因为它不计算较小的数字

You see at some point Python stops changing the value because it doesnt compute smaller digits

谢谢

推荐答案

对于 float 实例, Python使用机器级别的双精度浮点数(用C表示 double )。究竟是什么取决于构建Python的平台和编译器。
这些天来,它很可能是 64位双精度二进制浮点型-点号。 53位有效数字给出15到17个有效十进制数字。

For float instances, Python uses machine-level double precision floating point numbers (double in C terms). What exactly this is depends on the platform and compiler that built Python. These days it is most probably a 64-bit double-precision binary floating-point number. The 53-bit significand gives between 15 to 17 significant decimal digits.

如果使用 numpy ,则可以使用长双倍浮点数。它有一个64位的数字,产生大约18个十进制数字

If you use numpy, you can use the long double floating point number. This has a 64-bit sigificand, yielding approximately 18 decimal digits.

十进制值具有可调精度。默认情况下,它是28位有效数字,但是您可以更改它。因此,如果可以使用标准的数学运算符以及 Decimal 支持的有限数量的数学函数,则更改为Decimal可能是一个不错的选择。但这确实意味着更改所有代码。

Decimal values have adjustable precision. By default it is 28 significant digits, but you can change that. So changing to Decimal is probably a good choice if you can get by with the standard mathematical operators and the limited amount of mathematical functions that Decimal supports. But this would indeed mean changing all your code. It is part of the standard library, that might be a plus.

或者,您可以查看 mpmath 库。这还允许您设置任意精度(默认为15个有效数字)。它支持比 Decimal 更高级的数学。它实现了数学 cmath 等所有功能。但是在这里,您还必须将整个程序转换为将 float 更改为 mpf 类型。默认情况下,mpmath内部使用整数。如果可用,mpmath会使用Python绑定到 gmp 库,这使其速度更快,尤其是在精度(> 100位数字)。

Alternatively, you could look at the mpmath library. This also allows you to set an arbitrary precision (it defaults to 15 significant digits). It supports much more advanced mathematics than Decimal. It implements all the functions from math and cmath and more. But here you would also have to convert your whole program to change float to the mpf type. By default, mpmath works with integers internally. When available, mpmath uses the Python bindings to the gmp library which makes it much faster, especially at high precision (>100 digits).

如果您想要更高的精度,则必须转换程序以使用任意精度数学模块。根据所使用的个数学函数的数量, mpmath 看起来更合适;它支持从数学 cmath 然后是某些功能的所有功能。

If you want more precision, you will have to convert your program to use a arbitrary precision math module. Depending on how many math functions you use, mpmath looks like a better fit; it supports all functions from math and cmath and then some.

float mpf的总自动转换(或 Decimal )类型是不可能的。但是您可以找到大部分方法。

A total automatic conversion from float to mpf (or Decimal) types is not possible. But you can get most of the way there.

十进制 mpf 支持标准数学运算符。因此,您不必更改这些。您确实需要查看创建输入浮点数的位置以及使用数学。

Both Decimal and mpf support the standard math operators. So you should not have to change those. You do need to look at places where you create or input floating point numbers and at places where you use functions from math.

首先,将所有 float(实例更改为 Decimal( mpf(。这需要显式创建的数字。
其次,搜索所有浮点文字并以相同的方式替换它们。您可以

First, change all instances of float( to Decimal( or mpf(. That takes care of explicitly created numbers. Second, search for all floating point literals and replace them the same way. You can use your editor's search-and-replace function for this, or you can write a small Python script for it. So this can largely be automated.

然后使用所有编辑器中的搜索和替换功能,也可以编写一个小的Python脚本。从源(例如文件或数据库)读取数据并进行更新的函数,除非您明确使用 float(),否则可能必须手动完成操作,在这种情况下,可以通过上述方法处理它们。

Then look at all the functions that read data from a source (e.g. a file or database), and update them. This will probably have to be done manually, unless there these explicitly use float(), in which case they are handled by the abovementioned method.

如果您当前正在使用数学模块,则必须在选定的模中搜索并用等效功能替换这些功能乐在这种情况下,搜索部分可以轻松实现自动化。
如果您已使用导入数学,则可以轻松地搜索 数学。以找到函数需要更换。在这种情况下,使用 mpmath 时,只需将 math。替换为 mpmath 。,因为 mpmath 实现了 math 的所有功能。
如果您已使用数学导入x,y,z中的 ,则需要分别搜索x,y和z。您甚至可以编写脚本以将数学函数转换为 Decimal mpf 。是否值得这样做取决于代码库的大小和复杂性。

If you are currently using the math module, you will have to search and replace those functions by their equivalents in the chosen module. In this case the search part can be easily automated. If you have used import math, you can easily search for "math." to find the functions that need replacing. In this case when using mpmath, you could simply replace "math." with "mpmath.", since mpmath implements all functions from math. If you have used from math import x, y, z, you need to search for x, y and z separately. You could even write a script to translate math functions to their equivalents in Decimal or mpf. It depends on the size and complexity of your code base if this is worth doing.

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

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