0

I will explain my problem by example:

>>> #In this case, I get unwanted result
>>> k = 20685671025767659927959422028 / 2580360422
>>> k
8.016582043889239e+18
>>> math.floor(k)
8016582043889239040
>>> #I dont want this to happen ^^, let it remain 8.016582043889239e+18
>>> #The following case though, is fine
>>> k2 = 5/6
>>> k2
0.8333333333333334
>>> math.floor(k2)
0

How do I make math.floor not flooring the scientific notated numbers? Is there a rule for which numbers are represented in a scientific notation (I guess it would be a certain boundry).


EDIT:

I first thought that the math.floor function was causing an accuracy loss, but it turns out that the first calculation itself lost the calculation's accuracy, which had me really confused, it can be easily seen here:

>>> 20685671025767659927959422028 / 2580360422
8016582043889239040
>>> 8016582043889239040 * 2580360422
20685671025767659370513274880
>>> 20685671025767659927959422028 - 20685671025767659370513274880
557446147148
>>> 557446147148 / 2580360422
216.0342184739958
>>> ##this is >1, meaning I lost quite a bit of information, and it was not due to the flooring

So now my problem is how to get the actual result of the division. I looked at the following thread: How to print all digits of a large number in python? But for some reason I didn't get the same result.


EDIT: I found a simple solution for the division accuracy problem in here: How to manage division of huge numbers in Python? Apparently the // operator returns an int rather then float, which has no size limit apart to the machine's memory.

Eliran Abdoo
  • 611
  • 6
  • 17
  • _"I guess it would be a certain boundary"_. I don't think so. I just entered `99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999` into the interactive prompt, and it shows the whole thing, despite being much larger than 8e18. – Kevin Jun 13 '17 at 18:13
  • @Kevin try to divide it with something – Eliran Abdoo Jun 13 '17 at 18:15
  • The notation is simply a visual representation of a number. What a function gets as the parameter is the binary representation, which is the same for all floating point numbers. – DYZ Jun 13 '17 at 18:15
  • 1
    @Kevin Because it's an int, not a float – cs95 Jun 13 '17 at 18:16
  • google: [python format numbers with scientific notation](https://www.google.ca/search?client=safari&rls=en&q=python+format+numbers+with+scientific+notation&ie=UTF-8&oe=UTF-8&gfe_rd=cr&ei=PyxAWZ_jMKqC8QfxyL-oBw) should point you toward the `{:e}` format type. – Tadhg McDonald-Jensen Jun 13 '17 at 18:18
  • 1
    BTW, the true value of that floor division is 8016582043889239256, but the closest that floats can get to representing that value looks like 8016582043889239040.0 in decimal. – PM 2Ring Jun 13 '17 at 18:21
  • @PM2Ring Yea, I also noticed that, I just edited my question. – Eliran Abdoo Jun 13 '17 at 18:36

1 Answers1

3

In Python 3, math.floor returns an integer. Integers are not displayed using scientific notation. Some floats are represented using scientific notation. If you want scientific notation, try converting back to float.

>>> float(math.floor(20685671025767659927959422028 / 2580360422))
8.016582043889239e+18

As Tadhg McDonald-Jensen indicates, you can also use str.format to get a string representation of your integer in scientific notation:

>>> k = 20685671025767659927959422028 / 2580360422
>>> "{:e}".format(k)
'8.016582e+18'

This may, in fact, be more practical than converting to float. As a general rule of thumb, you should choose a numeric data type based on the precision and range you require, without worrying about what it looks like when printed.

Kevin
  • 74,910
  • 12
  • 133
  • 166