6

I'm trying to perform the modulus of a value in python, but I'm getting errors as it's interpretting the modulus as a string formatting constant, from my knowledge. My initial guess would be to type cast this, but then it hangs.

    val = pow(a,n,p)
    val = y1*val
    val = val % p

Are the two lines of code corresponding to this question. Right now, when I run this, I get: TypeError: not all arguments converted during string formatting At the second line.

If I wrap val into an integer and type cast it...it takes extremely long to calculate.

I'm not too skilled with python, my guess is I'm missing something simple, but what?

r36363
  • 589
  • 4
  • 15
  • 3
    what's `y1`? Assuming it's a string, `val = y1*val` also produces a string. It's probably helpful to print `repr(val)` after each step for debugging. – Wooble Mar 20 '12 at 18:02
  • 1
    Sure the error occurs on the second line? It would make perfect sense in the third line, given that `y1` is a string (which would result in `val` also being a string in the last line). – Niklas B. Mar 20 '12 at 18:06
  • What pow function is that with 3 arguments? Also, can you give more information on where these arguments come from? val should already be a number, not a string, so I'm guessing there is something fishy going on upstream. – Collin Green Mar 20 '12 at 18:08
  • @Keeyai: the default pow function has three arguments. Type "help(pow)" at the interpreter. – DSM Mar 20 '12 at 18:09
  • @DSM: I see! So, pow(x,y,z) is equivalent to (x**y) % z -- so you are effectively doing (((a**n) % p) * y1) % p. I'm still banking on weird arguments upstream. Check the types of everything and see what is coming out. By the time it throws your error, val is a string containing multiple % signs. – Collin Green Mar 20 '12 at 18:16
  • it is ``y1`` that is a string, multiplying the string by an integer repeats the string, so after ``y1*val``, ``val`` is a string. – mitch Mar 20 '12 at 18:21

2 Answers2

5

If yu are getting this error, y1 itself is a string. You can't perform numeric calculations with strings - when you do "int(y1) " - it is not casting, it is converting the number represented by characters inside the string o an actual numeric value - and that is the only way you can perform numeric operations on it.

If it is takin thta log, it is probable because you are trying convert "y1 * val" to int - which is wrong already - if y1 is a string, "y1 * val" gives you y1 concatenated to itself "val" times - so it would be a really huge number. You need to have the value i n "y1" as a number before multiplying - as in:

val  = int(y1) * val
jsbueno
  • 99,910
  • 10
  • 151
  • 209
  • This was the issue, I somehow stripped the int(y1) i had prior to the lines of code listed after reading it in from a file. – r36363 Mar 20 '12 at 22:49
2

As you can see from this code, the % operator has different meanings with strings than with numbers.

>>> 1 % 2
1
>>> '1' % 2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: not all arguments converted during string formatting

My guess is that y1 is actually a string. Here's the difference the type of y1 makes:

>>> val = 10
>>> y1 = '2'
>>> val * y1
'2222222222'
>>> y1 = 2
>>> val * y1
20
Steven Rumbalski
  • 44,786
  • 9
  • 89
  • 119