February 1st, 2016 by depesz | Tags: 9.6, degrees, postgresql, radians, trigonometry | No comments »

Did it help? If yes - maybe you can help me?

Did it help? If yes - maybe you can help me?

On 22nd of January, Tom Lane committed patch:

Add trigonometric functions that work in degrees. The implementations go to some lengths to deliver exact results for values where an exact result can be expected, such as sind(30) = 0.5 exactly. Dean Rasheed, reviewed by Michael Paquier |

The description seems to explain everything, but just for completeness sake:

So far we had a bunch of trigonometric functions, but they all worked on radians ( 0 .. 6.28 ), more or less:

$ select r::numeric(5,3), sin(r)::numeric(5,3), cos(r)::numeric(5,3) from generate_series(0.0, 2.1 * pi()::numeric, (pi()/4)::numeric) r; r | sin | cos -------+--------+-------- 0.000 | 0.000 | 1.000 0.785 | 0.707 | 0.707 1.571 | 1.000 | 0.000 2.356 | 0.707 | -0.707 3.142 | 0.000 | -1.000 3.927 | -0.707 | -0.707 4.712 | -1.000 | 0.000 5.498 | -0.707 | 0.707 6.283 | 0.000 | 1.000 (9 rows) |

Now, we got similar functions, with suffix “d" that handle trigonometry using degrees ( 0 .. 360 ) :

$ select d, sind(d)::numeric(5,3), cosd(d)::numeric(5,3) from generate_series(0, 360, 30) d; d | sind | cosd -----+--------+-------- 0 | 0.000 | 1.000 30 | 0.500 | 0.866 60 | 0.866 | 0.500 90 | 1.000 | 0.000 120 | 0.866 | -0.500 150 | 0.500 | -0.866 180 | 0.000 | -1.000 210 | -0.500 | -0.866 240 | -0.866 | -0.500 270 | -1.000 | 0.000 300 | -0.866 | 0.500 330 | -0.500 | 0.866 360 | 0.000 | 1.000 (13 rows) |

Of course not only sin and cos have been added, for full list, check the docs.

It's probably not a very common usecase, to use trigonometry in PostgreSQL, but it's a nice addition anyway. Thanks.