1

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. e.g.

>>> 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

  • 1
    Check the statement `14*15**13 == dst[-1]`.You are checking if `14*15**13` is equal to `14`, if you want to print the values in non-scientific see [here](https://stackoverflow.com/questions/658763/how-to-suppress-scientific-notation-when-printing-float-values). Scientific notation does not reduce the accuracy of the calculation but is used to save space. `1e-5==0.00001` would return true. – Countour-Integral Oct 15 '20 at 13:19
  • sorry that was a typo, I checked `14*15**13 ==[float(dst[i])*float(rst)**float(i) for i in range(len(dst))][-1]` also `False` – user1116678 Oct 15 '20 at 13:39

2 Answers2

1
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. If you are working solely with ints (and do not divide) you will get int in which case no e-notation is used.

Daweo
  • 31,313
  • 3
  • 12
  • 25
  • Thanks pal. My ultimate purpose in my program is to get the sum of that list, and in the context I mis-assign a variable. Using `int()` to change dtype makes the program function properly. You're right, thanks pal. – user1116678 Oct 15 '20 at 13:42
0

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
Bas Swinckels
  • 18,095
  • 3
  • 45
  • 62
  • I see. But during the calculation, using float and int get complete different outcomes, and i am quite confident that it is not a printing issue since the `==` comparison between float result and int result is always `False` when it comes to large number calculation(`+ - * **`) – user1116678 Oct 15 '20 at 16:02