How can I use the Sage modulo operator % in a function definition?

1,080 views
Skip to first unread message

Privasie Invazhian

unread,
Apr 10, 2014, 1:28:57 AM4/10/14
to sage-s...@googlegroups.com
Running Sage 5.3 on a MacBook Pro with OS X version 10.7.5, I want to use the modulo operator % in defining a function of a variable, as in the following example:

t = var('t')
L(t) = t*725.5 % 360.0
plot(L, 0,5)

This produces the following error message:
...
TypeError: unsupported operand type(s) for %:
'sage.symbolic.expression.Expression' and
'sage.symbolic.expression.Expression'

I can use other arithmetic operators such as + and / in this function expression, but not %, and I don't understand why. Is there an alternative or workaround so that I can compute the quantity L dependent on this variable t and then graph the remainder of L mod 360 for a given range of values of t?

Thanks for any advice,
Kim

Jeroen Demeyer

unread,
Apr 10, 2014, 9:24:37 AM4/10/14
to sage-s...@googlegroups.com
On 2014-04-10 07:28, Privasie Invazhian wrote:
> I can use other arithmetic operators such as + and / in this function expression, but not %, and I don't understand why.
Modulo in *symbolic* expressions is simply not implemented.

> Is there an alternative or workaround
Yes, use a normal Python function:

def L(t):
return t*725.5 % 360.0

Privasie Invazhian

unread,
Apr 10, 2014, 10:59:45 PM4/10/14
to sage-s...@googlegroups.com
Works great, thanks!  But now I'm having problems with the modulo operator returning negative results on floating-point operands.  E.g.,

4.9 % 10
 
returns 4.9000000000000, as I would expect.  But

5.1 % 10

returns not 5.100000000000 but -4.900000000000.  This is independent of whether I'm invoking it in a function or in a standalone Sage expression.

I'll try to figure it out and make a new thread if I'm stumped.

Thanks again!
Reply all
Reply to author
Forward
0 new messages