KX Community

Find answers, ask questions, and connect with our KX Community around the world.
KX Community Guidelines

Home Forums kdb+ KDB Geom/Trig

  • KDB Geom/Trig

    Posted by Laura on October 17, 2021 at 12:00 am

    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!

     

     

    Laura replied 9 months ago 2 Members · 3 Replies
  • 3 Replies
  • davidcrossey

    Member
    October 18, 2021 at 12:00 am

    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

  • Laura

    Administrator
    October 18, 2021 at 12:00 am

    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.

  • davidcrossey

    Member
    October 19, 2021 at 12:00 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

Log in to reply.