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

Ben Finney ben+python at benfinney.id.au
Thu Apr 10 00:54:26 EDT 2014


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




More information about the Python-list mailing list