Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

degrees and radians.

94 views
Skip to first unread message

Jim Richardson

unread,
May 4, 2002, 3:37:07 AM5/4/02
to
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


I am trying to get the math module to deal with degrees rather than
radians. (that it deals with radians for the angular functions like
sin() isn't mentioned in the docs, which was sort of an eyeopener :) I
can't find any info on doing this. I can convert from-to degrees in the
code calling the function, but that's a bit clunky. Any pointers to an
FM to R? :)

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE804+jd90bcYOAWPYRAt9KAKCuqeC4ozuXSaKZ5xY27Wv+k04QuQCcCrCZ
WyichPnKgXo+GaDdAebsaeU=
=h+vc
-----END PGP SIGNATURE-----

--
Jim Richardson
Anarchist, pagan and proud of it
http://www.eskimo.com/~warlock
Linux, from watches to supercomputers, for grandmas and geeks.

Tim Hammerquist

unread,
May 4, 2002, 4:27:51 AM5/4/02
to
Jim Richardson graced us by uttering:

> I am trying to get the math module to deal with degrees rather than
> radians. (that it deals with radians for the angular functions like
> sin() isn't mentioned in the docs, which was sort of an eyeopener :)
> I can't find any info on doing this. I can convert from-to degrees in
> the code calling the function, but that's a bit clunky. Any pointers
> to an FM to R? :)

Radians are what trig is based on. Otherwise the formula for the area of
a circle would be 'A = 360r'; since when does a unit circle have an area
of 360 square units?

OTOH, `man 3 sin` on my system documents the sin() function of the C
math library _is_ documented as taking radians. On a POSIX system, this
is usually what is called by Ruby's Math.sin() method.

Can you just write an additional method like

def sin_d(deg)
Math.sin(deg * Math::PI / 180)
end

...and maybe even make it a method of the Math module itself?

Tim Hammerquist
--
For every problem, there is one solution which is simple, neat and wrong.
-- H. L. Mencken

Tim Hammerquist

unread,
May 4, 2002, 4:30:58 AM5/4/02
to
Tim Hammerquist graced us by uttering:

> Jim Richardson graced us by uttering:
>> I am trying to get the math module to deal with degrees rather than
>> radians. (that it deals with radians for the angular functions like
>> sin() isn't mentioned in the docs, which was sort of an eyeopener :)
>> I can't find any info on doing this. I can convert from-to degrees in
>> the code calling the function, but that's a bit clunky. Any pointers
>> to an FM to R? :)
>
> Radians are what trig is based on. Otherwise the formula for the area
> of a circle would be 'A = 360r'; since when does a unit circle have an
> area of 360 square units?
>
> OTOH, `man 3 sin` on my system documents the sin() function of the C
> math library _is_ documented as taking radians. On a POSIX system,
> this is usually what is called by Ruby's Math.sin() method.
>
> Can you just write an additional method like
>
> def sin_d(deg)
> Math.sin(deg * Math::PI / 180)
> end
>
> ...and maybe even make it a method of the Math module itself?

Sorry for the confusion between languages here. The rest is still valid,
though.

Tim Hammerquist
--
Playing strip poker with an exhibitionist
somehow takes the challenge out of it.
-- Nick Smith, Metropolitan

Robert George Mayer

unread,
May 4, 2002, 1:24:50 PM5/4/02
to

Jim -

All the math routines I have ever dealt with use radians. In
30 years of dealing with this, in Fortran, C, C++ and Python, I
have found what works best for me is to use exclusively radians
internally and convert only where the user will see it - at the
GUI and with any text (not binary) files. (I think that this
works well, but may not be as natural with a command line program,
where the interface between the user and the program is so simple
to begin with.) In other words, I just consider the degrees/radians
conversion to be part of the interface between the user and the
program internals.

I find that it help to curse the Babylonian astronomers while
putting all the conversions in place.

- BOB

Simon Foster

unread,
May 4, 2002, 7:00:34 PM5/4/02
to

"Tim Hammerquist" <t...@vegeta.ath.cx> wrote in message
news:slrnad77g...@vegeta.ath.cx...

> > Radians are what trig is based on. Otherwise the formula for the area
> > of a circle would be 'A = 360r'; since when does a unit circle have an
> > area of 360 square units?
> >

Where do you get this from? A = pi * r^2?

> > OTOH, `man 3 sin` on my system documents the sin() function of the C
> > math library _is_ documented as taking radians. On a POSIX system,
> > this is usually what is called by Ruby's Math.sin() method.
> >

Maybe he's not on Unix?


Tim Hammerquist

unread,
May 4, 2002, 8:25:55 PM5/4/02
to
Simon Foster graced us by uttering:

> "Tim Hammerquist" <t...@vegeta.ath.cx> wrote:
> > > Radians are what trig is based on. Otherwise the formula for the
> > > area of a circle would be 'A = 360r'; since when does a unit
> > > circle have an area of 360 square units?
>
> Where do you get this from? A = pi * r^2?

You're right. Not enough coffee and too much time passed since college
trig. =) Correction:

: If trig were based on degrees, the following would be true for a
: unit circle:
:
: [ A = (180 * r^2) = 180 sq. units ]
: [ C = (360 * r) = 360 units ]

Thus:

"since when does a unit circle have an area of 180 sq. units?"
^^^^^^^^^^^^^

> > > OTOH, `man 3 sin` on my system documents the sin() function of
> > > the C math library _is_ documented as taking radians. On a POSIX
> > > system, this is usually what is called by Ruby's Math.sin()
> > > method.
>
> Maybe he's not on Unix?

My handy copy of the Turbo C++ 3.0 Library Reference manual from my
MS-DOS version of the software also documents sin() as taking a value in
radians. (C)1990. A.D. So this is far from new. One might say it's a
standard... A trig calculator's default setting doesn't dictate the
default of the world at large.

Furthermore, not all *nix systems are POSIX-compliant. And very few
POSIX-compliant systems are _completely_ compliant with the POSIX
standard.

My only intent in mentioning POSIX was that on most *nix systems (esp.
POSIX ones), you can #include<math.h> and compile and expect to call
a sin() function (that, btw, takes its argument in radians). Other
PC-oriented operating systems lack a standard math lib "out-of-the-box".

Tim Hammerquist
--
guru, n: a computer owner who can read the manual.

Simon Foster

unread,
May 4, 2002, 8:37:18 PM5/4/02
to
On Sun, 05 May 2002 00:25:55 GMT, Tim Hammerquist <t...@vegeta.ath.cx>
wrote:

>Simon Foster graced us by uttering:
>> "Tim Hammerquist" <t...@vegeta.ath.cx> wrote:
>> > > Radians are what trig is based on. Otherwise the formula for the
>> > > area of a circle would be 'A = 360r'; since when does a unit
>> > > circle have an area of 360 square units?
>>
>> Where do you get this from? A = pi * r^2?
>
>You're right. Not enough coffee and too much time passed since college
>trig. =) Correction:
>
>: If trig were based on degrees, the following would be true for a
>: unit circle:
>:
>: [ A = (180 * r^2) = 180 sq. units ]
>: [ C = (360 * r) = 360 units ]
>
>Thus:
>
> "since when does a unit circle have an area of 180 sq. units?"
> ^^^^^^^^^^^^^
>


I think you need more coffee! Surely PI is "3 and a bit"! The
area of a unit circle is pi. How big do you make it?


>> > > OTOH, `man 3 sin` on my system documents the sin() function of
>> > > the C math library _is_ documented as taking radians. On a POSIX
>> > > system, this is usually what is called by Ruby's Math.sin()
>> > > method.
>>
>> Maybe he's not on Unix?
>
>My handy copy of the Turbo C++ 3.0 Library Reference manual from my
>MS-DOS version of the software also documents sin() as taking a value in
>radians. (C)1990. A.D. So this is far from new. One might say it's a
>standard... A trig calculator's default setting doesn't dictate the
>default of the world at large.
>
>Furthermore, not all *nix systems are POSIX-compliant. And very few
>POSIX-compliant systems are _completely_ compliant with the POSIX
>standard.
>
>My only intent in mentioning POSIX was that on most *nix systems (esp.
>POSIX ones), you can #include<math.h> and compile and expect to call
>a sin() function (that, btw, takes its argument in radians). Other
>PC-oriented operating systems lack a standard math lib "out-of-the-box".
>
>Tim Hammerquist

You're right here, I've never seen a trig call that didn't
expect the argument in rads.

--
Simon Foster
Somewhere in the West of England

Tim Peters

unread,
May 4, 2002, 8:56:57 PM5/4/02
to
> Maybe he's not on Unix?

The docstrings are the same on Windows and Macs <wink>:

>>> print math.sin.__doc__
sin(x)

Return the sine of x (measured in radians).
>>>

although-if-you-stick-with-1.5.2-you-won't-see-that-ly y'rs - tim


Tim Hammerquist

unread,
May 4, 2002, 9:23:30 PM5/4/02
to
Simon Foster graced us by uttering:
> Tim Hammerquist <t...@vegeta.ath.cx> wrote:
>>You're right. Not enough coffee and too much time passed since college
>>trig. =) Correction:
>>
>>: If trig were based on degrees, the following would be true for a
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^

>>: unit circle:
>>:
>>: [ A = (180 * r^2) = 180 sq. units ]
>>: [ C = (360 * r) = 360 units ]
>>
>>Thus:
>>
>> "since when does a unit circle have an area of 180 sq. units?"
>> ^^^^^^^^^^^^^
>
> I think you need more coffee! Surely PI is "3 and a bit"! The
> area of a unit circle is pi. How big do you make it?

I think you missed my point. The OP was expressing his shock at Math.sin
accepting its argument in radians. I was painting a picture of what the
world would be like if trig were based on degrees instead.

Operating under the fairly certain assumption that (2*PI) radians = 360
degrees, I simply replace pi with 180 degrees in the classic geometric
functions. Thus:

A = PI * r^2

becomes:

A = 180 * r^2

So, for a unit circle whose radius is 1:

A = 180 * (1)^2
A = 180 * (1)
A = 180
--> 180 sq. units ?!?!?!

This is quite obviously ridiculous, and was intended so.

To satisfy your question, however, my value of pi, stored permanently in
my brain to six significant digits since Mr. Chatfield's calc class in
high school: 3.14159 ;)

> Simon Foster
> Somewhere in the West of England

Tim Hammerquist
Somewhere in the West of the United States
--
It's astonishing how much trouble one can
get oneself into, if one works at it.
-- Destruction, The Sandman

Fernando Pérez

unread,
May 4, 2002, 9:30:58 PM5/4/02
to
Tim Hammerquist wrote:

> My only intent in mentioning POSIX was that on most *nix systems (esp.
> POSIX ones), you can #include<math.h> and compile and expect to call
> a sin() function (that, btw, takes its argument in radians). Other
> PC-oriented operating systems lack a standard math lib "out-of-the-box".
>

BTW, this isn't really a unix-or-not issue, it's simply that mathematically,
the 'natural' form of all trig functions (their power series expansion)
requires their arguments to be expressed in radians: the period of sin(x) is
simply 2*pi, 6.1428..., whatever you want to call it. It's _not_ 360:

sin(0) == 0 ; sin(360) ~ 0.96.

Scientific calculators simply have a 'deg' mode which on the fly converts back
and forth between degree input and radians for internal work, but that's
simply a user convenience (because degrees are more conveninent for some
'everyday' use as angular measurement units).

Just a little detail.

Cheers,

f.

John Baxter

unread,
May 4, 2002, 10:27:16 PM5/4/02
to
In article <3CD41962...@acm.org>,

Robert George Mayer <ma...@acm.org> wrote:

> I find that it help to curse the Babylonian astronomers while
> putting all the conversions in place.

Points...everything should be in points. There being 32 of them in a
circle, they are even binarily attractive. ;-)

--John (steer north by east 1/4 east, if you please) Baxter

Jim Richardson

unread,
May 4, 2002, 1:45:30 PM5/4/02
to
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Sat, 04 May 2002 11:24:50 -0600,


Robert George Mayer <ma...@acm.org> wrote:

> Jim Richardson wrote:
>>
>>
>> I am trying to get the math module to deal with degrees rather than
>> radians. (that it deals with radians for the angular functions like
>> sin() isn't mentioned in the docs, which was sort of an eyeopener :) I
>> can't find any info on doing this. I can convert from-to degrees in the
>> code calling the function, but that's a bit clunky. Any pointers to an
>> FM to R? :)
>>

> Jim -
>
> All the math routines I have ever dealt with use radians. In
> 30 years of dealing with this, in Fortran, C, C++ and Python, I
> have found what works best for me is to use exclusively radians
> internally and convert only where the user will see it - at the
> GUI and with any text (not binary) files. (I think that this
> works well, but may not be as natural with a command line program,
> where the interface between the user and the program is so simple
> to begin with.) In other words, I just consider the degrees/radians
> conversion to be part of the interface between the user and the
> program internals.
>
> I find that it help to curse the Babylonian astronomers while
> putting all the conversions in place.
>

*chuckle* ok, sounds like the route to take. I don't have a lot of
programming experience, and python is my first "real" language in the
last 20 years. Assembly doesn't count in this context.


do you have any good curses in Babylonian to share? :P

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE81B46d90bcYOAWPYRAmXfAJ9z4tItLXq/zVIq3Ef+uRIGgwJInACgxAkE
mZnnk1NR8xJrrfN8dWsQX+k=
=tXKl

Jim Richardson

unread,
May 4, 2002, 1:47:55 PM5/4/02
to
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Sat, 04 May 2002 08:30:58 GMT,
Tim Hammerquist <t...@vegeta.ath.cx> wrote:
> Tim Hammerquist graced us by uttering:
>> Jim Richardson graced us by uttering:
>>> I am trying to get the math module to deal with degrees rather than
>>> radians. (that it deals with radians for the angular functions like
>>> sin() isn't mentioned in the docs, which was sort of an eyeopener :)
>>> I can't find any info on doing this. I can convert from-to degrees in
>>> the code calling the function, but that's a bit clunky. Any pointers
>>> to an FM to R? :)
>>
>> Radians are what trig is based on. Otherwise the formula for the area
>> of a circle would be 'A = 360r'; since when does a unit circle have an
>> area of 360 square units?
>>
>> OTOH, `man 3 sin` on my system documents the sin() function of the C
>> math library _is_ documented as taking radians. On a POSIX system,
>> this is usually what is called by Ruby's Math.sin() method.
>>
>> Can you just write an additional method like
>>
>> def sin_d(deg)
>> Math.sin(deg * Math::PI / 180)
>> end
>>
>> ...and maybe even make it a method of the Math module itself?
>


This is basically what I am now doing, (although yours is more elegant.)
I didn't realize that python basically called the C math function,
although that makes sense. I didn't think to check that. Thanks.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE81B7Ld90bcYOAWPYRAizKAKC4m5k2syn+c9xAA2M+mKmIQmZPpQCgrOzD
Lp6BYYh7ZLRLDPRmOe2pxDs=
=A8lE

Jim Richardson

unread,
May 5, 2002, 3:09:55 AM5/5/02
to
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I guess this whole furor shows how long it's been for me since I took
math at high school level :)

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE81NrDd90bcYOAWPYRArmoAKCJK3bX5Q8wJKVf27Ivd0ggIZp9AQCdFJx9
UPU3O/FzMUekwp25yvTjTwU=
=tujK

Jim Richardson

unread,
May 5, 2002, 3:11:57 AM5/5/02
to
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Well, now that I have conquered elementry trig, I am now going to try
for simple python and figure out how to add the contents of a tuple to
the contents of another tuple...

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE81Ns9d90bcYOAWPYRAq+FAJ9tIc6yg7RsgsCOnPwZiEe07xj6+ACdGqgK
EAWLted0R1tEBE/CtpUEN/c=
=YagS

Simon Foster

unread,
May 5, 2002, 4:30:40 AM5/5/02
to

"Tim Hammerquist" <t...@vegeta.ath.cx> wrote in message
news:slrnad92o...@vegeta.ath.cx...

> Operating under the fairly certain assumption that (2*PI) radians = 360
> degrees, I simply replace pi with 180 degrees in the classic geometric
> functions. Thus:
>

I see what you're driving at, but the fact that 2*pi radians = 360 degrees
still
does not change the value of pi (thankfully)!

> To satisfy your question, however, my value of pi, stored permanently in
> my brain to six significant digits since Mr. Chatfield's calc class in
> high school: 3.14159 ;)
>

I have 3.141592653

C:\>python
Python 2.2 (#28, Dec 21 2001, 12:21:22) [MSC 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from math import pi
>>> pi
3.1415926535897931
>>>

Cheers.


Simon Foster

unread,
May 5, 2002, 10:43:14 AM5/5/02
to

"Jim Richardson" <war...@eskimo.com> wrote in message
news:tvl2ba...@127.0.0.1...

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>

...

>
> Well, now that I have conquered elementry trig, I am now going to try
> for simple python and figure out how to add the contents of a tuple to
> the contents of another tuple...
>

Try '+'

>>> a
(1, 2)
>>> b
(3, 4)
>>> a+b
(1, 2, 3, 4)
>>>


Jim Richardson

unread,
May 5, 2002, 3:19:20 PM5/5/02
to
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


sorry, I wasn't very clear, in the above example, the result I am
looking for is

>>>a=(1,2)
>>>b=(2,3)
>>>a+b
(3,5)


if that makes sense, I don't want to append, I want to add.

>
>
>
>


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE81YW4d90bcYOAWPYRAoTvAKCwwIqQts+K+Bi3JWZWZ1UIz87pYQCgxuwd
wicTymWLn2xqKFnoEZkRXSg=
=NVVK

Fernando Pérez

unread,
May 5, 2002, 3:51:42 PM5/5/02
to
Jim Richardson wrote:

> sorry, I wasn't very clear, in the above example, the result I am
> looking for is
>
>>>>a=(1,2)
>>>>b=(2,3)
>>>>a+b
> (3,5)
>
>
> if that makes sense, I don't want to append, I want to add.
>

Use Numeric for that (unless you want to write your own code, of course). Its
arrays work in this way, which is different to that of python's normal
sequence types.

cheers,

f.

Simon Foster

unread,
May 5, 2002, 4:36:07 PM5/5/02
to

"Jim Richardson" <war...@eskimo.com> wrote in message
news:oj04ba...@127.0.0.1...

>>> [ x + y for x, y in zip( a, b ) ]
[4, 6]
>>>


Jim Richardson

unread,
May 5, 2002, 4:34:10 PM5/5/02
to
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

this would be the numeric python module?


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE81ZdCd90bcYOAWPYRAsghAJ9Mj6amq90GuRI0SKMECCZIpoC2PwCdED3F
iCnr6xUE4/NuBU6AkpmFL1o=
=NUYB

Fernando Pérez

unread,
May 5, 2002, 6:19:18 PM5/5/02
to
Jim Richardson wrote:

>> Use Numeric for that (unless you want to write your own code, of course).
>> Its arrays work in this way, which is different to that of python's normal
>> sequence types.
>>
>> cheers,
>>
>
> this would be the numeric python module?
>

Yes: http://www.pfdubois.com/numpy/

Cheers,

f.

Raymond Hettinger

unread,
May 5, 2002, 10:30:32 PM5/5/02
to
>, I wasn't very clear, in the above example, the result I am
> looking for is
>
> >>>a=(1,2)
> >>>b=(2,3)
> >>>a+b
> (3,5)
>
>
> if that makes sense, I don't want to append, I want to add.
>

Then, you want elementwise operations. Take a look at
matfunc.py at http://users.rcn.com/python/download/python.htm

>>> from matfunc import Vec
>>> Vec([1,2]) + Vec([2,3])
[3, 5]


Raymond Hettinger


William Park

unread,
May 6, 2002, 1:05:38 AM5/6/02
to
Jim Richardson <war...@eskimo.com> wrote:
> sorry, I wasn't very clear, in the above example, the result I am
> looking for is
>
>>>>a=(1,2)
>>>>b=(2,3)
>>>>a+b
> (3,5)

Well, you can learn about Python's OO nature by writing class to handle
this:

class vector:
def __init__(self, x=[]): # vector([...])
self.array = x[:]
self.n = len(x)

def __repr__(self):
return `self.array`

def __add__(self, other):
for a, b in map(None, self.array, other.array):
out.append(a + b)
return vector(out)

>>> a = vector([1,2])
>>> b = vector([2,3])
>>> a+b
[3, 5]

--
William Park, Open Geometry Consulting, <openge...@yahoo.ca>
8-CPU Cluster, Hosting, NAS, Linux, LaTeX, python, vim, mutt, tin

Greg Ewing

unread,
May 6, 2002, 1:36:46 AM5/6/02
to
Robert George Mayer wrote:
>
> I find that it help to curse the Babylonian astronomers while
> putting all the conversions in place.

You can make both these tasks a litte easier
using the following handy function:

def curse_babylonian_astronomers(d):
import math
return d * math.pi / 180.0

--
Greg Ewing, Computer Science Dept, University of Canterbury,
Christchurch, New Zealand
To get my email address, please visit my web page:
http://www.cosc.canterbury.ac.nz/~greg

Tim Hammerquist

unread,
May 6, 2002, 1:52:33 AM5/6/02
to
Simon Foster graced us by uttering:
> "Tim Hammerquist" <t...@vegeta.ath.cx> wrote:
>> Operating under the fairly certain assumption that (2*PI) radians
>> = 360 degrees, I simply replace pi with 180 degrees in the classic
>> geometric functions. Thus:
>
> I see what you're driving at, but the fact that 2*pi radians = 360
> degrees still does not change the value of pi (thankfully)!

No, pi is thankfully constant. I was just taking a misunderstanding to
its illogical conclusion.

>> To satisfy your question, however, my value of pi, stored permanently
>> in my brain to six significant digits since Mr. Chatfield's calc
>> class in high school: 3.14159 ;)
>
> I have 3.141592653

Nice. Should be more than enough, considering many professors accept
(22/3) as a reasonable value (~3.142857...)

> C:\>python
> Python 2.2 (#28, Dec 21 2001, 12:21:22) [MSC 32 bit (Intel)] on win32
> Type "help", "copyright", "credits" or "license" for more information.
>>>> from math import pi
>>>> pi
> 3.1415926535897931
>>>>

Anyway, that's what we need! A Python (or, maybe, Ruby) interpreter
embedded in our brain. Gods, wouldn't that make bill-paying easier!

Tim Hammerquist
--
Randal can write one-liners again. Everyone is happy, and peace spreads
over the whole Earth.
-- Larry Wall in <1997051019...@wall.org>

Fernando Pérez

unread,
May 6, 2002, 1:53:55 AM5/6/02
to
Tim Hammerquist wrote:

> Nice. Should be more than enough, considering many professors accept
> (22/3) as a reasonable value (~3.142857...)
>

you mean 22/7:

In [1]: 22/7.
Out[1]: 3.1428571428571428

In [2]: pi
Out[2]: 3.1415926535897931

In [3]: 100*(__-_)/_
Out[3]: 0.040249943477070081

It's actually pretty good, .04% error is enough for most day to day things.

Cheers,

f.

Tim Hammerquist

unread,
May 6, 2002, 2:32:33 AM5/6/02
to
Fernando Pérez graced us by uttering:

> Tim Hammerquist wrote:
>
>> Nice. Should be more than enough, considering many professors accept
>> (22/3) as a reasonable value (~3.142857...)
>
> you mean 22/7:

Of course. Dammit.

"Coffee..... Coffee NOW!"
-- Droz, "PCU"

Tim Hammerquist
--
In 1968 it took the computing power of 2 C-64's to fly a rocket to the moon.
Now, in 1998 it takes the Power of a Pentium 200 to run Microsoft Windows 98.
Something must have gone wrong.

Jim Richardson

unread,
May 6, 2002, 2:41:45 PM5/6/02
to
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Mon, 06 May 2002 17:36:46 +1200,
Greg Ewing <gr...@cosc.canterbury.ac.nz> wrote:
> Robert George Mayer wrote:
>>
>> I find that it help to curse the Babylonian astronomers while
>> putting all the conversions in place.
>
> You can make both these tasks a litte easier
> using the following handy function:
>
> def curse_babylonian_astronomers(d):
> import math
> return d * math.pi / 180.0
>

argh!
I tried that, and Marduk smashed my computer with an Axe.... bad thing,
very bad thing....


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE81s5pd90bcYOAWPYRAnMuAKDWK9VdZxBm8YXqmUWohx2h5FSCswCfdwjI
BIDbbHQyp1E8Chg5JmdYHhw=
=06/B

Jim Richardson

unread,
May 6, 2002, 2:40:50 PM5/6/02
to
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 6 May 2002 05:05:38 GMT,


William Park <openge...@NOSPAM.yahoo.ca> wrote:
> Jim Richardson <war...@eskimo.com> wrote:
>> sorry, I wasn't very clear, in the above example, the result I am
>> looking for is
>>
>>>>>a=(1,2)
>>>>>b=(2,3)
>>>>>a+b
>> (3,5)
>
> Well, you can learn about Python's OO nature by writing class to handle
> this:
>
> class vector:
> def __init__(self, x=[]): # vector([...])
> self.array = x[:]
> self.n = len(x)
>
> def __repr__(self):
> return `self.array`
>
> def __add__(self, other):
> for a, b in map(None, self.array, other.array):
> out.append(a + b)
> return vector(out)
>
>>>> a = vector([1,2])
>>>> b = vector([2,3])
>>>> a+b
> [3, 5]
>


Thanks. Actually, I am a little intimidated by the whole OO and class
thing. I still don't "grok" the whole OO thing.

I don't understand what the (self,...) part of the init is. I *think* I
know what __init__ is for, to set up the things you need to use the
functions (methods?) within the class, but what's with self all over the
place?


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE81s4yd90bcYOAWPYRAkBWAJ41AnR4O7V84xaT+J6U0p1TyNtowACeOeMo
Zs0TJHmg894Qa353mtLwSbw=
=un2O

William Park

unread,
May 6, 2002, 4:19:32 PM5/6/02
to
Jim Richardson <war...@eskimo.com> wrote:
>> class vector:
>> def __init__(self, x=[]): # vector([...])
>> self.array = x[:]
>> self.n = len(x)
>>
>> def __repr__(self):
>> return `self.array`
>>
>> def __add__(self, other):

out = [] <-- missed when I cut/paste... sorry.

>> for a, b in map(None, self.array, other.array):
>> out.append(a + b)
>> return vector(out)
>>
>>>>> a = vector([1,2])
>>>>> b = vector([2,3])
>>>>> a+b
>> [3, 5]
>>
>
>
> Thanks. Actually, I am a little intimidated by the whole OO and class
> thing. I still don't "grok" the whole OO thing.
>
> I don't understand what the (self,...) part of the init is. I *think* I
> know what __init__ is for, to set up the things you need to use the
> functions (methods?) within the class, but what's with self all over the
> place?

The function is called with parameter 'self' replaced by the object itself.
You can use any function parameter names, but 'self' is used by convention.

Raymond Hettinger

unread,
May 6, 2002, 4:50:34 PM5/6/02
to
> I am trying to get the math module to deal with degrees rather than
> radians. (that it deals with radians for the angular functions like
> sin() isn't mentioned in the docs, which was sort of an eyeopener :) I
> can't find any info on doing this. I can convert from-to degrees in the
> code calling the function, but that's a bit clunky. Any pointers to an
> FM to R? :)

There is a patch for adding degree/radian conversions.
See http://www/python.org/sf/552452


Raymond Hettinger

Fernando Pérez

unread,
May 6, 2002, 5:15:50 PM5/6/02
to
Raymond Hettinger wrote:

Sorry to sound critical, but I doubt this will make it in (and I hope it
doesn't). Reason: bloat. Yes, it's only two functions, but still, bloat is
bloat. Plus, if you really need those conversions, it's less typing and more
efficient (no function call) to simply use a multiplicative constant:

In [7]: to_rad = pi/180

In [8]: to_deg = 1/to_rad

In [9]: sin pi/4
------> sin (pi/4)
Out[9]: 0.70710678118654746

In [10]: sin 45*to_rad
-------> sin (45*to_rad)
Out[10]: 0.70710678118654746

In [11]: (pi/4)*to_deg
Out[11]: 45.0

I realize you have good intentions, but I truly don't think that in this case
the benefits outweigh the cost. OTOH, one could add a comment in the
docstrings to the effect of 'trig functions expect arguments in radians',
since it does seem to cause confusion among newbies.

Cheers,

f.

Steve Holden

unread,
May 6, 2002, 5:41:29 PM5/6/02
to
"Fernando Pérez" <fper...@yahoo.com> wrote in message
news:ab6rvq$ka4$1...@peabody.colorado.edu...

> Raymond Hettinger wrote:
>
> >> I am trying to get the math module to deal with degrees rather than
> >> radians. (that it deals with radians for the angular functions like
> >> sin() isn't mentioned in the docs, which was sort of an eyeopener :) I
> >> can't find any info on doing this. I can convert from-to degrees in the
> >> code calling the function, but that's a bit clunky. Any pointers to an
> >> FM to R? :)
> >
> > There is a patch for adding degree/radian conversions.
> > See http://www/python.org/sf/552452
>
> Sorry to sound critical, but I doubt this will make it in (and I hope it
> doesn't). Reason: bloat. Yes, it's only two functions, but still, bloat is
> bloat. Plus, if you really need those conversions, it's less typing and
more
> efficient (no function call) to simply use a multiplicative constant:
>
> In [7]: to_rad = pi/180

Let's make that pi/180.0 so it works before the division unification takes
place, shall we?


>
> In [8]: to_deg = 1/to_rad
>
> In [9]: sin pi/4
> ------> sin (pi/4)

Similarly this should be pi/4.0

> Out[9]: 0.70710678118654746
>
> In [10]: sin 45*to_rad
> -------> sin (45*to_rad)
> Out[10]: 0.70710678118654746
>
> In [11]: (pi/4)*to_deg
> Out[11]: 45.0
>
> I realize you have good intentions, but I truly don't think that in this
case
> the benefits outweigh the cost. OTOH, one could add a comment in the
> docstrings to the effect of 'trig functions expect arguments in radians',
> since it does seem to cause confusion among newbies.
>

But these would be maths newbies as well as Python newbies. The comment is a
good idea.

regards
Steve
--

Steve Holden: http://www.holdenweb.com/ ; Python Web Programming:
http://pydish.holdenweb.com/pwp/

Fernando Pérez

unread,
May 6, 2002, 5:56:52 PM5/6/02
to
Steve Holden wrote:

> Let's make that pi/180.0 so it works before the division unification takes
> place, shall we?
>>
>> In [8]: to_deg = 1/to_rad
>>
>> In [9]: sin pi/4
>> ------> sin (pi/4)
>
> Similarly this should be pi/4.0
>

It doesn't matter, math.pi is a float (unless Guido decides to use the
Congress version of pi==3 ;)

Cheers,

f.

Fernando Pérez

unread,
May 6, 2002, 5:58:13 PM5/6/02
to
Steve Holden wrote:

>> I realize you have good intentions, but I truly don't think that in this
> case
>> the benefits outweigh the cost. OTOH, one could add a comment in the
>> docstrings to the effect of 'trig functions expect arguments in radians',
>> since it does seem to cause confusion among newbies.
>>
> But these would be maths newbies as well as Python newbies. The comment is a
> good idea.

Indeed I meant math newbies rather than Python newbies. Thanks for the
clarification, that's what I had in mind but wasn't explicit enough. This can
actually bite a very experienced programmer who simply isn't too familiar
with math details.

Cheers,

f.

Jim Richardson

unread,
May 7, 2002, 1:00:22 AM5/7/02
to
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 6 May 2002 20:19:32 GMT,

OK, I think... I'll have to cogitate on this. Thanks for the help.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE8119md90bcYOAWPYRAlOAAJ98AcmzTzzgLajOzjjAbIa5KCXMRQCfaRPn
s7e6Ne1P0u1UhbGD29nSEe8=
=mKel

Jim Richardson

unread,
May 7, 2002, 1:02:28 AM5/7/02
to
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Well, it was a lightbulb going off in my head when I finally figured out
*why* the numbers my code was giving me were so wrong. Doh! after I
thought about it, it made perfect sense, but before I had thought about
it :)....

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE811/kd90bcYOAWPYRAmF6AKCya2mJ44k0UCixEg5SCl7KCjPevACeJJ5C
0DF1Qa/PoRvFn2L1wa9SihI=
=dCLr

Delaney, Timothy

unread,
May 7, 2002, 1:18:22 AM5/7/02
to
> From: Jim Richardson [mailto:war...@eskimo.com]

>
> Thanks. Actually, I am a little intimidated by the whole OO and class
> thing. I still don't "grok" the whole OO thing.
>
> I don't understand what the (self,...) part of the init is. I
> *think* I
> know what __init__ is for, to set up the things you need to use the
> functions (methods?) within the class, but what's with self
> all over the
> place?

This may help ...

http://www.ibiblio.org/obp/thinkCSpy/

Tim Delaney


Jim Richardson

unread,
May 7, 2002, 2:29:04 AM5/7/02
to
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


Cool link, thank you!

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE813Qwd90bcYOAWPYRAtFMAJwIYnIFF69nibghoi7v+8TVPVjddQCgiy/q
3L3zA8dSH5/K33BPNOowlwo=
=wKeG

Andrae Muys

unread,
May 7, 2002, 8:46:39 PM5/7/02
to
Jim Richardson <war...@eskimo.com> wrote in message news:<oj04ba...@127.0.0.1>...

> sorry, I wasn't very clear, in the above example, the result I am
> looking for is
>
> >>>a=(1,2)
> >>>b=(2,3)
> >>>a+b
> (3,5)
>

Well if it's just a one-off you can use a simple list comprehension

[a+b for a,b in zip(a,b)]
[3,5]

if you specifically need a tuple then wrapping the above in tuple()
will do the conversion.

However if this is likely to be a common task (as I suspect), then you
are better off using Numeric as suggested, or (less likely) rolling
your own class.

Andrae Muys

Huaiyu Zhu

unread,
May 7, 2002, 9:10:46 PM5/7/02
to
Jim Richardson <war...@eskimo.com> wrote:
>Thanks. Actually, I am a little intimidated by the whole OO and class
>thing. I still don't "grok" the whole OO thing.
>
>I don't understand what the (self,...) part of the init is. I *think* I
>know what __init__ is for, to set up the things you need to use the
>functions (methods?) within the class, but what's with self all over the
>place?

Try the "Instant Python" page. Find it on Google or follow links from
python.org.

Huaiyu

Jim Richardson

unread,
May 8, 2002, 1:53:24 AM5/8/02
to
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 7 May 2002 17:46:39 -0700,


I am going to look into numeric thanks, and also zip, I don't recal this
from 1.5? is it new with 2.0?

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE82L1Ud90bcYOAWPYRAuLwAJ44s0WfeNqPeio5z/vjwj+u1NoHrwCgijpn
Kip+FQUnQoHEli8vERJ6zjw=
=YhR/

Jim Richardson

unread,
May 8, 2002, 1:53:39 AM5/8/02
to
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Thanks

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE82L1jd90bcYOAWPYRAs8nAJ9DAGJCpeYDSoK9l97o/e52jdpdVwCfdpGv
ImEBAtbixBb7N6JyVCpMccI=
=wpdc

murdock...@gmail.com

unread,
Aug 24, 2016, 12:09:26 AM8/24/16
to
On Saturday, May 4, 2002 at 3:37:07 AM UTC-4, Jim Richardson wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
>
> I am trying to get the math module to deal with degrees rather than
> radians. (that it deals with radians for the angular functions like
> sin() isn't mentioned in the docs, which was sort of an eyeopener :) I
> can't find any info on doing this. I can convert from-to degrees in the
> code calling the function, but that's a bit clunky. Any pointers to an
> FM to R? :)
>
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.0.6 (GNU/Linux)
> Comment: For info see http://www.gnupg.org
>
> iD8DBQE804+jd90bcYOAWPYRAt9KAKCuqeC4ozuXSaKZ5xY27Wv+k04QuQCcCrCZ
> WyichPnKgXo+GaDdAebsaeU=
> =h+vc
> -----END PGP SIGNATURE-----
>
> --
> Jim Richardson
> Anarchist, pagan and proud of it
> http://www.eskimo.com/~warlock
> Linux, from watches to supercomputers, for grandmas and geeks.

For what is is worth.. Electrical Engineers for the most part work in degrees NOT Radians for example try doing polar to rectangular or vice versa in polar.
I have never seen it done.

Also Borland C and C++ used Degrees and NOT Radians.. go look at the libraries

Just for what its worth.

Gary Herron

unread,
Aug 24, 2016, 12:35:26 AM8/24/16
to
Do you really need anything more complex than this?

>>> toRadians = math.pi/180.0

>>> math.sin(90*toRadians)
1.0

Perhaps I'm not understanding what you mean by "clunky", but this seems
pretty clean and simple to me.


Gary Herron


--
Dr. Gary Herron
Professor of Computer Science
DigiPen Institute of Technology
(425) 895-4418

Random832

unread,
Aug 24, 2016, 1:04:33 AM8/24/16
to
On Wed, Aug 24, 2016, at 00:26, Gary Herron wrote:
> Perhaps I'm not understanding what you mean by "clunky", but this seems
> pretty clean and simple to me.

The original post is from 2002, I don't know why it got a reply just
now.

Steven D'Aprano

unread,
Aug 24, 2016, 1:27:13 AM8/24/16
to
On Wednesday 24 August 2016 14:26, Gary Herron wrote:

> Do you really need anything more complex than this?
>
> >>> toRadians = math.pi/180.0
>
> >>> math.sin(90*toRadians)
> 1.0
>
> Perhaps I'm not understanding what you mean by "clunky", but this seems
> pretty clean and simple to me.

The math module has two conversion functions, math.radians() and
math.degrees().


Some other languages (Julia, by memory, and perhaps others) have dedicated
sind(), cosd(), tand() or possibly dsin(), dcos(), dtan() functions which take
their argument in degrees and are more accurate than doing a conversion to
radians first. I'd like to see that.

I've also seen languages with sinp() etc to calculate the sine of x*pi without
the intermediate calculation.

But if I were designing Python from scratch, I'd make sin(), cos() and tan()
call dunder methods __sin__ etc:


def sin(obj):
if hasattr(type(obj), '__sin__'):
y = type(obj).__sin__()
if y is not NotImplemented:
return y
elif isinstance(obj, numbers.Number):
return float.__sin__(float(obj))
raise TypeError

Likewise for asin() etc.

Then you could define your own numeric types, such as a Degrees type, a
PiRadians type, etc, with their own dedicated trig function implementations,
without the caller needing to care about which sin* function they call.




--
Steve

Stephen Tucker

unread,
Aug 24, 2016, 4:30:37 AM8/24/16
to
For what it's worth, mathematicians naturally work with angles in radians.

The mathematics of the trignonmetric functions works naturally when the
angle is expressed in radians.

For the older among us, logarithms also have a "natural" base, and that is
the number e. Back in those days, however, even the mathematicians would
use logarithm tables where the values were shown to base 10.
> --
> https://mail.python.org/mailman/listinfo/python-list
>

Rob Gaddi

unread,
Aug 24, 2016, 1:04:01 PM8/24/16
to
murdock...@gmail.com wrote:

> On Saturday, May 4, 2002 at 3:37:07 AM UTC-4, Jim Richardson wrote:
>>
>> I am trying to get the math module to deal with degrees rather than
>> radians. (that it deals with radians for the angular functions like
>> sin() isn't mentioned in the docs, which was sort of an eyeopener :) I
>> can't find any info on doing this. I can convert from-to degrees in the
>> code calling the function, but that's a bit clunky. Any pointers to an
>> FM to R? :
>>
>> --
>> Jim Richardson
>> Anarchist, pagan and proud of it
>> http://www.eskimo.com/~warlock
>> Linux, from watches to supercomputers, for grandmas and geeks.
>
> For what is is worth.. Electrical Engineers for the most part work in degrees NOT Radians for example try doing polar to rectangular or vice versa in polar.
> I have never seen it done.
>

While I fully admit to thinking in degrees, any time I'm actually doing
any mathematical work my units are either radians or cycles. The
representation of angle in fixed point cycles actually comes out really
nicely; each bit takes you from hemispheres to quadrants to octants,
etc.

--
Rob Gaddi, Highland Technology -- www.highlandtechnology.com

Email address domain is currently out of order. See above to fix.

Roel Schroeven

unread,
Aug 24, 2016, 5:41:27 PM8/24/16
to
murdock...@gmail.com schreef op 2016-08-24 06:08:
> Also Borland C and C++ used Degrees and NOT Radians.. go look at the libraries

You have a reference for that? Borland C++ Builder Help from C++ Builder
5 disagrees with you:

"sin, sinl

Header File
math.h

Category
Math Routines

Syntax
#include <math.h>
double sin(double x);
long double sinl(long double x);

Description
Calculates sine.

sin computes the sine of the input value. Angles are specified in radians.

sinl is the long double version; it takes a long double argument and
returns a long double result. Error handling for these functions can be
modified through the functions _matherr and _matherrl.

Return Value
sin and sinl return the sine of the input value."

The VCL trigonometry functions also use radians. From the same help system:

"SinCos

Returns sine and cosine of an angle.

Unit
Math

Category
Trigonometry routines

extern PACKAGE void __fastcall SinCos(Extended Theta, Extended &Sin,
Extended &Cos);

Description
Call SinCos to obtain the sine and cosine of an angle expressed in
radians. Theta specifies the angle. The sine and cosine are returned by
the Sin and Cos parameters, respectively.

SinCos is twice as fast as calling Sin and Cos separately for the same
angle."

I could also copy some entries from the C++Builder 2010 documentation;
they all use radians too.


Best regards,
Roel

--
The saddest aspect of life right now is that science gathers knowledge
faster than society gathers wisdom.
-- Isaac Asimov

Roel Schroeven

Lawrence D’Oliveiro

unread,
Sep 8, 2016, 4:15:03 AM9/8/16
to
On Tuesday, May 7, 2002 at 9:15:50 AM UTC+12, Fernando Pérez wrote:
> Plus, if you really need those conversions, it's less typing and more
> efficient (no function call) to simply use a multiplicative constant...

This I have found to be the best idea. In Qahirah <https://github.com/ldo/qahirah> I provided “deg” and “circle” constants, used like this:

ldo@hypatia:~> python3 -ic "import math, qahirah as qah"
>>> math.sin(45 * qah.deg)
0.7071067811865475
>>> math.atan(1) / qah.deg
45.0
>>> math.cos(qah.circle / 4)
6.123233995736766e-17

I think these provide greater flexibility than having to have explicit conversion functions.
0 new messages