Raising to the power of large numbers is giving me the wrong answer?

13 次查看(过去 30 天)
If I try 11^16 in matlab, I get: 45949729863572160
If I try 11**16 in python, I get: 45949729863572161
Notice, the last digits are different.
As the numbers get larger, the problem gets worse: 99^95 in Matlab: 3848960788934848|488282452569509484590776195611314554049114673132510910096787679715604422673797115451807631980373077374162416714994207463122539142978709403811688831410945323915071533162168320
99**95 in python: 3848960788934848|611927795802824596789608451156087366034658627953530148126008534258032267383768627487094610968554286692697374726725853195657679460590239636893953692985541958490801973870359499
Notice, where I placed the line, that's they begin to differ
the way I printed the numbers in Matlab، to control the precision:
num2str(99^95,1000)
the answer I am looking for is the python answer

采纳的回答

the cyclist
the cyclist 2013-5-30
In general, MATLAB uses double-precision, which roughly means that you can expect 16 digits of precision.
If you need higher precision, then there is a very good user-contributed package for doing that, which you can download from the File Exchange: http://www.mathworks.com/matlabcentral/fileexchange/22725-variable-precision-integer-arithmetic.
v1 = vpi(99);
v2 = vpi(95);
v = v1^v2
v =
38489607889348486119277958028245967896084511560873660346586279535301
481260085342580322673837686274870946109685542866926973747267258531956576
79460590239636893953692985541958490801973870359499

更多回答(1 个)

Iain
Iain 2013-5-30
Matlab, by default, uses double precision numbers, which puts the useful number of significant figures at around 16. - More than enough for most use.
If you note that where matlab and python start to differ is at the 17th significant figure, then it should be clear to you that that is where the number representation within matlab is limited.
If you force matlab to do the calculations as uint64, you'll get the result accurate to whole numbers of up to 2^64 - 1 (i.e. 18 significant figures)
If you write the appropriate logic/method, you can get matlab to effectively use "uint128", or "uint256", or higher if your needs dictate.

类别

Help CenterFile Exchange 中查找有关 Call Python from MATLAB 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by