Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- KX Community
- :
- Discussion Forums
- :
- kdb+ and q
- :
- KDB Geom/Trig

Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

Options

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

2021.10.17 01:32 PM

Hey All,

How do you use KDB sin/cos/tan to solve for the angles of a triangle?

Im not asking about SOHCAHTOA -- when I use the sin/cos/tan functions in KDB/q I get different results compared to other language's functions.

Is there a conversion im missing?

Thanks!

1 ACCEPTED SOLUTION

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

2021.10.19 03:33 AM - edited 2021.10.19 04:33 AM

Hi planefan,

Radians tend to be the more natural unit when working with angles than degrees, and you'll find they are typically the default unit in most languages.

KDB doesn't have built in support for degrees, however you can work in degrees by converting to radians and rounding your results.

For example:

```
q)deg:0,0+\12#30
q)deg
0 30 60 90 120 150 180 210 240 270 300 330 360
q)pi:3.141592653589793
q)round:{(10 xexp neg x)*`long$y*10 xexp x}[3;] /3 dp; change as necessary
q)deg!round sin deg*pi%180
0 | 0
30 | 0.5
60 | 0.866
90 | 1
120| 0.866
150| 0.5
180| 0
210| -0.5
240| -0.866
270| -1
300| -0.866
330| -0.5
360| 0
```

You can set the above as a function for easier re-use; and also swap sin, for cos or tan, although be careful with tan(90) and tan(270) which should be be set to null / undefined.

Kind regards,

David

3 REPLIES 3

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

2021.10.17 05:02 PM - edited 2021.10.17 06:03 PM

Hi planefan,

Thanks for posting your query on KX Community!

For the angles you are trying to solve, are you working in degrees or radians?

KDB assumes a radian input, see here using sin as an example. You can see how to convert between degrees and radians on Geometry and trigonometry

Kind regards,

David

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

2021.10.18 01:08 PM

Hey David, thanks for the help!

I'm dealing in degrees, or trying to.

Does kdb sin/cos only accept radians?

For example, if I go sin(30) on my comp calc i get .5. When I go sin(30) in kdb I get -0.9880316.

At it's simplest form, I'm trying to use the sin/cos/tan functions within KDB similar to how i'd use them on my graphing calculator.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

2021.10.19 03:33 AM - edited 2021.10.19 04:33 AM

Hi planefan,

Radians tend to be the more natural unit when working with angles than degrees, and you'll find they are typically the default unit in most languages.

KDB doesn't have built in support for degrees, however you can work in degrees by converting to radians and rounding your results.

For example:

```
q)deg:0,0+\12#30
q)deg
0 30 60 90 120 150 180 210 240 270 300 330 360
q)pi:3.141592653589793
q)round:{(10 xexp neg x)*`long$y*10 xexp x}[3;] /3 dp; change as necessary
q)deg!round sin deg*pi%180
0 | 0
30 | 0.5
60 | 0.866
90 | 1
120| 0.866
150| 0.5
180| 0
210| -0.5
240| -0.866
270| -1
300| -0.866
330| -0.5
360| 0
```

You can set the above as a function for easier re-use; and also swap sin, for cos or tan, although be careful with tan(90) and tan(270) which should be be set to null / undefined.

Kind regards,

David

Main Office Contacts

**EMEA**

Tel: +44 (0)28 3025 2242

**AMERICAS**

Tel: +1 (212) 447 6700

**APAC**

Tel: +61 (0)2 9236 5700

Useful Information

Resources

KX. All Rights Reserved.

KX and kdb+ are registered trademarks of KX Systems, Inc., a subsidiary of FD Technologies plc.