-1

I was trying to divide a big number by another number. Specifically, 52075187820116425 by 5. However when I performed the operation I was getting an incorrect answer. I tested this out by using putting the following program on the jupyter notebook. However this yielded an answer that was exactly 5 off the correct one. I think it is not due to floating point number errors either due to the resulting quotient being an integer. Why is this happening? And can someone explain how to deal with this error as well?

big = 52075187820116425
int(big/5)*5

When I just do

big/5 

I get 1.0415037564023284e+16 which is also incorrect as the last digit should be 5 not 4

BOSSrobot
  • 27
  • 4
  • 1
    For int division you can use the `//` operator. The `/` operator returns a float, which is imprecise. – khelwood Apr 28 '20 at 19:47
  • even though it returns a float, the decimal point should be 0. This would mean that the int would cast it to the correct integer. I have no idea why this is happening. – BOSSrobot Apr 28 '20 at 22:23
  • Also, the floating point math does not answer my question as the division should result in an integer. big clearly divides 5. For this reason there should be no error when representing the resulting quantity. – BOSSrobot Apr 28 '20 at 23:19

1 Answers1

1

Is it truly an Integer?

Oddly, my Python gives the correct answer, and not an e+16 number. Maybe my Python version is different.

This is not a mistake, this is how Python (and most programming languages) store numbers (or floats).

Your 'big' value is not an Integer. It's an 'Any'. It could be anything and store anything. You can reassign it to a string if you wish. If you force it to become an Int, it will check if it can be an Int. But it doesn't really turn it into an Int.


What really are Floats?

Floating Point Numbers are not precise. Division becomes very weird. Sometimes, they can even be infinity or nan. They use a certain amount of bits for the numeric value, and another certain amount of bits for its magnitude: e+16.

1.0415037564023284e+16 == 10.0415037564023284e+15 (Integers don't ever do this. They don't have magnitude. They are numeric only.)

Division in Python is tricky. If you have an Int value, the "/" method will always return a Float (That is, it will always return a numeric trait and a magnitude trait).


Final Answer

• "//" Operator

The // Operator will always only return a perfect numeric trait (and never a magnitude e+ trait).

Community
  • 1
  • 1
0-1
  • 702
  • 1
  • 10
  • 30