简体   繁体   English

Python中的倍数与C ++的精度相同

[英]Multiply doubles in Python with same precision as C++

I am rewriting a C++ program into Python. 我正在将C ++程序重写为Python。 I need to multiply 2 doubles but C++ and Python don't give the same result. 我需要乘以2个双打,但C ++和Python不会给出相同的结果。 Here's an example with 'hard-coded' values: 以下是“硬编码”值的示例:

C++ C ++

printf("%f", ( 44474025505478620106407223274000875520.0 * 5454277033526873088.0 ) );
>>> 242573655903020442240866171189072992939998568974355791872.0

Python 蟒蛇

print("%f" % ( 44474025505478620106407223274000875520.0 * 5454277033526873088.0 ) )
>>> 242573655903020398684723205308949669628048817708024725504.0

My problem is that I don't need the most accurate result: I need to get (with Python) a result as close as C++'s result as possible. 我的问题是我不需要最准确的结果:我需要得到(使用Python)尽可能接近C ++结果的结果。

In my example, the 15 first digits are the same: 在我的例子中,15个第一个数字是相同的:

C++ > 242573655903020[442240866171189072992939998568974355791872.0
Py  > 242573655903020[398684723205308949669628048817708024725504.0

I need to have a result even more close (18 first digits would be nice) 我需要得到一个更接近的结果(18个第一位数字会很好)

I'm really stuck here... Anybody has an idea? 我真的被困在这里......有人有想法吗?

FYI : 仅供参考

Python version: 2.7.8 Python版本:2.7.8

C++ compiler: cl.exe (the one from visual studio) C ++编译器:cl.exe(来自visual studio的那个)

It seems to depend on the Python implementation. 它似乎依赖于Python实现。 For example, with ideone (cpython 2.7.13), I'll get the same result as your C result. 例如,使用ideone(cpython 2.7.13),我将获得与C结果相同的结果。

C version on Ideone - Result: Ideone上的C版 - 结果:

242573655903020442240866171189072992939998568974355791872.000000

Python version on Ideone - Result: Ideone上的Python版本 - 结果:

242573655903020442240866171189072992939998568974355791872.000000

Use library decimal , take your snippet as an example: 使用库decimal ,以您的代码段为例:

from decimal import Decimal

print("%f" % ( Decimal("44474025505478620106407223274000875520.0") * Decimal("5454277033526873088.0") ) )

It gives 242573655903020442240866171189072992939998568974355791872.000000 which is exactly the same as the result given in C . 它给出了242573655903020442240866171189072992939998568974355791872.000000 ,它与C给出的结果完全相同。

DBL_DIG or std::numeric_limits::digits10 will probably return 15. With IEE754 doubles you get 15-17 digits depending on the number. DBL_DIG或std :: numeric_limits :: digits10可能会返回15.随着IEE754的两倍,您将获得15-17位数,具体取决于数字。 Your result is within spec. 您的结果在规格范围内。 You can mitigate to achieve higher precision by using multiprecision numbers. 您可以使用多精度数字来缓解以获得更高的精度。 In C++ Boost Multiprecision is an option as is something like mpmath in python 在C ++中,Boost Multiprecision是一个选项,类似于python中的mpmath

http://www.boost.org/doc/libs/1_65_1/libs/multiprecision/doc/html/index.html http://www.boost.org/doc/libs/1_65_1/libs/multiprecision/doc/html/index.html

http://mpmath.org/ http://mpmath.org/

you could use python GMPY library, or you could just use python's "bignumber" . 你可以使用python GMPY库,或者你可以使用python的“bignumber”。 since there is no limit for int in python , you could just do 因为python中没有int的限制,你可以这样做

fl1*10**len(fl1)*fl2*10**len(fl2)

then divide with 然后除以

10**(len(fl2)+len(fl1))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM