C ++与Python的精确度 [英] C++ vs Python precision

查看:376
本文介绍了C ++与Python的精确度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试着找出一个num ^ num的前k个数字的问题,我用C ++和Python编写了相同的程序



C ++

  long double intpart,num,f_digit,k; 
cin>> num>> k;
f_digit = pow(10.0,modf(num * log10(num),& intpart)+ k-1);
cout<< f_digit;

Python

  





$ b $输入

  19423474 9 




输出

  C ++> 163074912 
Python> 163074908

我检查了C ++解决方案的结果是正确的。
检查它在http://www.wolframalpha.com/input/?i=19423474^19423474



任何想法如何能得到相同的精度Python ???



编辑:我知道外部库包获取此精度,但任何民族解决方案???

是一个内置的python类,可以正确处理浮点数(以10为底,而不是IEEE 7标准中的某些东西) 。我不知道它是否支持对数以及所有这些。



编辑:确实 support logarithmsand all that。



您可以将它的精度设置为好。默认是28个地方,但它可以像你想要的那样大。把它看作是小数点的BigInt。


Trying out a problem of finding the first k digits of a num^num I wrote the same program in C++ and Python

C++

long double intpart,num,f_digit,k;
cin>>num>>k;
f_digit= pow(10.0,modf(num*log10(num),&intpart)+k-1);
cout<<f_digit;

Python

(a,b) = modf(num*log10(num))
f_digits = pow(10,b+k-1)
print f_digits

Input

19423474 9

Output

C++    > 163074912
Python > 163074908

I checked the results the C++ solution is the accurate one. Checked it at http://www.wolframalpha.com/input/?i=19423474^19423474

Any idea how can I get the same precision in Python ???

EDIT : I know about the external library packages to obtain this precision but any NATIVE solution ???

解决方案

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.

这篇关于C ++与Python的精确度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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