Plotting the integer-and-fraction remainder of a function value modulo 360

Kim Plofker kim_plofker at yahoo.com
Thu Apr 10 01:03:49 EDT 2014


Thanks, the % operator is indeed what I want, but I want to use it with a function expression rather than with numbers alone.  And that seems to create a type error.

Here's an example of what goes wrong:

t = var('t')

L(t) = t*725.5%360.0


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 it indeed a Sage-specific problem rather than something I could work around in Python?

Many thanks again for any help.

Kim


________________________________
 From: Ben Finney <ben+python at benfinney.id.au>
To: python-list at python.org 
Sent: Thursday, April 10, 2014 12:54 AM
Subject: Re: Plotting the integer-and-fraction remainder of a function value modulo 360
 

Kim Plofker <kim_plofker at yahoo.com> writes:

> How can I get Python to represent a value of a function in degrees,
> i.e., with values between 0 and 360, by taking the (non-integer)
> function expression mod 360?

In Python, you simply use the modulo (‘%’) operator::

    >>> 45.0 % 360
    45.0
    >>> 700.0 % 360
    340.0
    >>> 

> That is, I have a function with non-integer values, called Longitude

If they're not integers, and you're not saying what they *are*, then I
can't know anything beyond “they will behave the way the Longitude class
defines them”.

> which is defined in terms of the variable t.

I don't understand what it means for a longitude value to be “defined in
terms of the variable t”.

Can you say more about how these values are defined? Since (as you say)
they're not integers, what *are* they?

> I just want to plot Longitude modulo 360 for a range of values of t:
> that is, for every value of t, plot the integer-AND-fraction remainder
> after dividing Longitude by 360.

What does it mean to “plot the integer-AND-fraction remainder”? It
sounds like you want to plot two numbers separately, the integer and the
fraction remainder. But that doesn't make much sense to me.

Do you mean simply that you want to plot numbers like ‘3.7’, ‘270.0’,
and ‘48.75’? In which case, this is supported by the native ‘float’
type, and (for better accuracy) by the ‘decimal.Decimal’ type from the
standard library::

    >>> lon = decimal.Decimal("758.45")
    >>> lon % 360
    Decimal('38.45')

> But Python (in Sage) apparently won't let me use the int function or
> the // operator on functions defined in terms of a variable: I get a
> "cannot evaluate symbolic expression numerically" TypeError.

It sounds like this Sage is not behaving as the standard Python types
do, with regard to the modulo ‘%’ operator.

For help with Sage (I don't know what that is), you probably will get
better answers on a Sage-specific discussion forum.

From the perspective of Python, the standard types handle the
requirements you describe without any TypeError.

-- 
\      “If you were going to shoot a mime, would you use a silencer?” |
  `\                                                    —Steven Wright |
_o__)                                                                  |
Ben Finney

-- 
https://mail.python.org/mailman/listinfo/python-list
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20140409/551c0442/attachment.html>


More information about the Python-list mailing list