C++ vs Python precision

心已入冬 提交于 2019-12-05 06:07:19

Decimal is a built in python class that handles floating points correctly (as base 10, not as IEEE 7somethingsomething standard). I don't know if it supports logarithms and all that though.

Edit: It does indeed support logarithms "and all that".

You can set the precision of it as well. Default is 28 places, but it can be as large as you want. Think of it as a BigInt for decimals.

Python floats are doubles under the hood, as you discovered. You will have to resort to C code, or an external library, to get better floating-point precision.

The GMP library is a good one, and it has a python wrapper called 'GMPY', available on PyPI

In general, I would do it this way. However, it doesn't seem to perform anywhere near fast enough for your example numbers.

num = 453
k = 9
result = num ** num

print str(result)[:k]
# Prints: '163111849'
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!