Re: Proposal: Trigonometric functions in degrees - Mailing list pgsql-hackers

From Tom Lane
Subject Re: Proposal: Trigonometric functions in degrees
Date
Msg-id 1671.1445890789@sss.pgh.pa.us
Whole thread Raw
In response to Re: Proposal: Trigonometric functions in degrees  (Dean Rasheed <dean.a.rasheed@gmail.com>)
Responses Re: Proposal: Trigonometric functions in degrees  (Dean Rasheed <dean.a.rasheed@gmail.com>)
List pgsql-hackers
Dean Rasheed <dean.a.rasheed@gmail.com> writes:
> On 26 October 2015 at 19:45, Peter Eisentraut <peter_e@gmx.net> wrote:
>> But how you are going to implement that?  I don't see a sind() in the C
>> library.

> I'm thinking something along the lines of:

> 1. Reduce the range of the input (say to 0..90 degrees).
> 2. Handle special cases (0, 30 and 90 for sind()).
> 3. Otherwise convert to radians for the general case.

I'd be okay with #1 and #3, but #2 is just a crock; it would risk creating
problems that did not exist before, such as non-monotonicity of the
function result (over ranges where that does not hold).

I looked into my dusty old copy of Cody & Waite's "Software Manual for the
Elementary Functions", which is what I used as a reference the last time
I had to do code like this (which admittedly was quite a long time ago;
the state of the art might've advanced).  C&W say that the key accuracy
limit for sin and cos is reduction of the argument modulo pi (or whichever
multiple of pi you choose to work with).  Now that problem just goes away
for degrees, of course, so it might be that reduction mod 360 and then
conversion to radians would be Good Enough(TM).

If it's not good enough, a possible idea is reduction mod 45 degrees or
even mod 30 degrees and then using trig identities to reconstruct the
correct output.

Anyway, I think the core idea of trying to build a reasonably thin wrapper
around sin(3m) and friends is probably an appropriate amount of effort,
depending on how many cases you are hoping to make exact.  I doubt it's
worth coding sind() from scratch.
        regards, tom lane



pgsql-hackers by date:

Previous
From: Dean Rasheed
Date:
Subject: Re: Proposal: Trigonometric functions in degrees
Next
From: Peter Eisentraut
Date:
Subject: Re: Patch: Implement failover on libpq connect level.