-2

Good day. I'm testing some things with RSA in Python, and I've found an issue which I can solve... I've tried with Python 3.8.2 on Win10, and also with Python 3.7.7 on Linux.

Lets have a number, like:

c1= 36251028222184669113487409017454975826565106104917435244458509025325597595097437837292486054294655693705631371301937875354932194238774646471806718003961847732817524682493697446263462385101163784046225206169381882321268194820701612704489320707217757417267296259559235617734319007884212443563740615946094992859

Which is odd, and it has some divisors, like the number 3. However, using python, the next result is wrong:

>>> int(c1/3)
12083676074061555316246267430643464247176522791037114836775756754628361403915178709103046469098451026523530772272080775928995905537251905701994027100469698250900136798602107760548114248253333583378545153076020030263178871202026581924052160700631938491851060523788925385374271125821574344356121690982561349632   

Because is even, and because this other result:

>>> c1-(c1/3)*3
4.9896007738368e+291    

So I'm probably missing something about huge numbers in Python, but after searching in Google, all that appears says that Python can handle long Integers (i.e: Handling very large numbers in Python)

Thank you in advance

EDIT1: Using // operator doesnt work neither. With c1//3 it shows an odd number, but not with 1433 for example:

>>> int(c1/1433)
25297298131322167445037545214187294306719866275723199239586371433276402101706584876000795120234021688465172586752437074519879774327812782348905848779769082172156601811448934599891376653705513433451245958986783036140639646619734644642118968668454860764517223706466696549981028176876987462015607168839974912

>>> int(c1//1433)
25297298131322169653515288916577094086926103353047756625581653192830144867479021519394616925537093994211885116051596563401906625428314477649551094210720061223180408012905580911558592034264594406173220660271724970217214371821843414308785290095755587869691065079943639649500571533764279444217544044623932304   
NikNitro
  • 29
  • 1
  • 3
  • 1
    Large numbers such as those are stored as floating points. And floating point math is not the best at being accurate. – rdas Apr 12 '20 at 10:51
  • 1
    Does this answer your question? [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – rdas Apr 12 '20 at 10:51
  • Did you mean: `c // 3` ? I get `120 ...` (many digits elided) `... 330953` – quamrana Apr 12 '20 at 10:52
  • Regarding the https://stackoverflow.com/questions/588004/is-floating-point-math-broken question, it says why the error is (IEEE 754), but not if it's possible to solve it, or how. – NikNitro Apr 12 '20 at 11:10
  • 1
    So far you have provided no evidence for why you think c1//3 or c1//1433 are wrong. **WHY** do you think c1 divided by 1433 should be odd? – President James K. Polk Apr 13 '20 at 15:46
  • You ar right, I'm sorry. I took it for granted. Here you have a comment that proof that multiplication and division of any two odd numbers always result in an odd number. – NikNitro Apr 14 '20 at 17:59

2 Answers2

0

I tried this which seems to confirm that c1 is divisible by 3:

c1 = 36251028222184669113487409017454975826565106104917435244458509025325597595097437837292486054294655693705631371301937875354932194238774646471806718003961847732817524682493697446263462385101163784046225206169381882321268194820701612704489320707217757417267296259559235617734319007884212443563740615946094992859
d1 = c1 // 3
print(d1)

r = c1 - (d1 * 3)
print(r)

Output:

12083676074061556371162469672484991942188368701639145081486169675108532531699145945764162018098218564568543790433979291784977398079591548823935572667987282577605841560831232482087820795033721261348741735389793960773756064940233870901496440235739252472422432086519745205911439669294737481187913538648698330953
0
quamrana
  • 37,849
  • 12
  • 53
  • 71
  • Thank you for your answer. the '//' operator works in this example with 3, but not with 1433, so it could be casuality. Anyway, what I need is to be sure that a division result will match 100% the result. – NikNitro Apr 12 '20 at 11:08
0

You can use Python arbitrary precision library mpmath

from mpmath import mp, mpf
mp.dps = 310 # Used 310 since c1 has 308 digits

 # Float precise up to mp.dps digits
c1 = mpf(36251028222184669113487409017454975826565106104917435244458509025325597595097437837292486054294655693705631371301937875354932194238774646471806718003961847732817524682493697446263462385101163784046225206169381882321268194820701612704489320707217757417267296259559235617734319007884212443563740615946094992859)

Test 1

print(int(c1/3))
Out: 
12083676074061556371162469672484991942188368701639145081486169675108532531699145945764162018098218564568543790433979291784977398079591548823935572667987282577605841560831232482087820795033721261348741735389793960773756064940233870901496440235739252472422432086519745205911439669294737481187913538648698330953

print(c1 - (c1/3)*3)
Out: 0.0

Test 2

print(int(c1/1433))
Out: 25297298131322169653515288916577094086926103353047756625581653192830144867479021519394616925537093994211885116051596563401906625428314477649551094210720061223180408012905580911558592034264594406173220660271724970217214371821843414308785290095755587869691065079943639649500571533764279444217544044623932304

print(c1 - (c1/1433)*1433)
0.0
DarrylG
  • 16,732
  • 2
  • 17
  • 23
  • Thanks DarrylG, it looks helpfull. However, I can see that when you divide c1 with 1433 (both are odd numbers), the result is even (ends with "932304"), so the division is not working properly :( – NikNitro Apr 12 '20 at 11:27