简体   繁体   English

在Python中使用带浮点值的指数运算符(**)时出现意外输出

[英]Unexpected output when using exponent operator(**) with float value in Python

I am currently using Python 3.6 in Windows 7. 我目前在Windows 7中使用Python 3.6。

When I use exponent operator(**) with float that gives a large number, I get an unexpected output. 当我使用带大量浮点数的指数运算符(**)时,会得到意外的输出。

For example, the output of "10^32" differs whether the exponent is in a form of an integer(32) or a float(32.0). 例如,无论指数是整数(32)还是浮点数(32.0),输出“ 10 ^ 32”都不同。

   >>>print (int(10**(32.0)))
   100000000000000005366162204393472

   >>>print (int(10**(32)))
   100000000000000000000000000000000

Using "**"operator in Python(example) 在Python中使用“ **”运算符(示例)

Is there a reason for the different output? 是否有不同输出的原因?

If so, how can i fix the problem? 如果是这样,我该如何解决该问题?

This is due to the inherent inaccuracy of floating point representation. 这是由于浮点表示形式固有的不准确性。 See Floating point inaccuracy examples and What Every Computer Scientist Should Know About Floating-Point Arithmetic . 请参阅浮点错误示例每位计算机科学家应了解的有关浮点算法的知识

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

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