简体   繁体   English

如何在python大数计算中抑制科学记数法

[英]How to suppress scientific notation in python large number calculation

In python, when a large number(integer or float) is calculated, it seems that the number will be store in scientific notation and therefore become inaccurate.在python中,当计算一个大数(整数或浮点数)时,这个数字似乎会以科学记数法存储,因此变得不准确。 eg例如

>>> rst = 15
>>> dst = [10, 14, 10, 14, 10, 14, 10, 14, 10, 14, 10, 14, 10, 14]
>>> [float(dst[i])*float(rst)**float(i) for i in range(len(dst))]

[10.0, 210.0, 2250.0, 47250.0, 506250.0, 10631250.0, 113906250.0, 2392031250.0, 25628906250.0, 538207031250.0, 5766503906250.0, 121096582031250.0, 1297463378906250.0, 2.724673095703125e+16]
>>> # please note the last element is stored in scientific notation
>>> # but if calculate 14*15**13 (same as how the last element is calculated)
>>> 14*15**13
>>> 27246730957031250
>>> # result is fine
>>> 14*15**13 == dst[-1]
>>> False

how should I suppress scientific notation in python large number calculation我应该如何在python大数计算中抑制科学记数法

please note the last element is stored in scientific notation
but if calculate 14*15**13 (same as how the last element is calculated)

Last element is float , whilst result of 14**15**13 is int .最后一个元素是float ,而14**15**13int If you are working solely with int s (and do not divide) you will get int in which case no e -notation is used.如果您仅使用int s(并且不进行除法),您将得到int在这种情况下不使用e符号。

Please note the difference between how a number is stored in memory and being used in calculations (as a float or int) and how it is converted to string and displayed on the screen (rounded as integer, fixed point or scientific notation).请注意数字在内存中的存储方式和在计算中的使用方式(作为浮点数或整数)以及如何将其转换为字符串并显示在屏幕上(四舍五入为整数、定点或科学记数法)之间的区别。 These are two completely independent concepts, so seeing something displayed in a certain way does not automatically say something about accuracy.这是两个完全独立的概念,因此看到以某种方式显示的内容并不能自动说明准确性。

In [8]: i = 123

In [9]: print('integer displayed as int: %i, as fixed point: %.1f or %.3f, and in scientific notation: %e' % (i, i, i, i))
integer displayed as int: 123, as fixed point: 123.0 or 123.000, and in scientific notation: 1.230000e+02

In [10]: f = 1.23

In [11]: print('floating point displayed as int: %i, as fixed point: %.1f or %.3f, and in scientific notation: %e' % (f, f, f, f))
floating point displayed as int: 1, as fixed point: 1.2 or 1.230, and in scientific notation: 1.230000e+00

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

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