python - Getting the wrong answer when dividing two integers, one large and one small -


i attempting use prime sieve find prime factors of numbers. in code, end having divide large number small number find prime factor out of range of sieve i'm using. shouldn't problem, in fact, method works every number checked except one.

the problem have run divide large number 51 find prime factor , when multiply 51 check if equals original number... doesn't!!

it not work regardless of if convert int or if leave float in scientific notation.

i understand floats not perfect, don't know how around weird error. plugged numbers calculator , got correct answer, code gives me wrong answer.

what going on here? if run code see checks return false when should true both.

as said before, method works 1 of numbers testing has factor outside range of sieve using, specific large number having issues with.

the correct answer should 1,176,462,117,668,023,508,828,242,241 , answer im getting 1,176,462,117,668,023,481,334,235,136

l_number = 59999568001069198950240354291 answer = 59999568001069198950240354291 / 51 int_answer = int(answer) check = int_answer*51 check2 = answer * 51  print("the large number is: {:,d}".format(l_number)) print("large number divided 51: ", answer) print("if check == original number: ", check2 == 59999568001069198950240354291) print("large number divided 51 integer: {:,d}".format(int_answer)) print("that answer * 51 (should original number): {:,d}".format(check)) print("if integer check == original number: ", check == 59999568001069198950240354291) 

as point out float division not exact due limited precision available 64-bit double.

it works if use integer division (in python 3)

answer = 59999568001069198950240354291 // 51 

this works because python uses arbitrary precision integer arithmetic.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -