2 Replies - 488 Views - Last Post: 18 December 2012 - 06:56 AM Rate Topic: -----

#1 gravity226  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 01-January 09

Overflow Error 'Result to Large'

Posted 17 December 2012 - 08:35 PM

Hey everyone, thanks for stopping by.

I wanted to write a simple RSA encryption program using Python but I'm having an issue decrypting the data that I have already encrypted. Below is the line where it gets hung up and says Overflow error 'Result to Large':
for item in Encryptor:
    Decryptor.insert(len(Decryptor), long(math.fmod(pow(item, 2011), 3127)))



The item value is usually greater than 1000. Is there anything besides long that I can use to store this gigantic value?
Thanks,
Tommy

Is This A Good Question/Topic? 0
  • +

Replies To: Overflow Error 'Result to Large'

#2 Nallo  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 163
  • View blog
  • Posts: 255
  • Joined: 19-July 09

Re: Overflow Error 'Result to Large'

Posted 18 December 2012 - 05:30 AM

The problem is not long, the problem is that math.fmod converts its arguments to floats. So regardless which number type you pass to math.fmod you will get the same error when the numbers are to big to be floats.

Do you need fmod or is the normal integer % operator good enough for you?
I mean is item always an integer? If so you can use the % operator and adjust for the sign rule of fmod:
n = pow(item, 2011)
x = n % m
#x has now same sign as m. But using fmod it would have same sign as n.
#so adjust for that (if n and m always have the same sign you don't need that)
if m >= 0 and n < 0:
    x -= m
elif m < 0 and n >= 0:
    x += m

#instead of
n = pow(item, 2011)
x = math.fmod(n, m)


Was This Post Helpful? 1
  • +
  • -

#3 gravity226  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 01-January 09

Re: Overflow Error 'Result to Large'

Posted 18 December 2012 - 06:56 AM

That solves it, thanks Nallo!

Tommy
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1