KX Community

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

Home Forums kdb+ KDB Geom/Trig Re: KDB Geom/Trig

  • 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