cancel
Showing results for 
Search instead for 
Did you mean: 

C++ convert large decimal to KDB

ekallivrousis
New Contributor III
I need help to find out if there is compatible function in c++ class used to connect to db that would convert the large(24) decimal place to datatype compatible to kdb
1 ACCEPTED SOLUTION

These are examples of using the C interface to extend the Kdb+ database.

 

Documentation is here:

https://code.kx.com/q/interfaces/using-c-functions/ 

It shows an example of writing a new 'add' function in C:

#include"k.h"
#ifdef __cplusplus
extern "C"{
#endif

K add(K x,K y)
{
  if(x->t!=-KJ||y->t!=-KJ)
    return krr("type");
  return kj(x->j+y->j);
}

#ifdef __cplusplus
}
#endif

Then this can be used from inside a Kdb+ process:

q)add:(`add 2:(`add;2))
q)add[3;4]
7

This is a possible method to add math functions for 128-bit floating point numbers inside Kdb+.

View solution in original post

8 REPLIES 8

davidcrossey
Moderator Moderator
Moderator

Hi @ekallivrousis,

You could use the Double / Float type as per the C API Reference

Thanks for posting your query on the forums!

Kind regards,

David

Hi David, 
    To elaborate on @ekallivrousis comment, we are trying to use 128-bit floating point data type (instead of 64-bit(8 byte double) ) to support 24 decimal places instead of 16 decimals supported by double. Currently, as per C API Reference KDB+ supports 4byte(float) and 8byte(double).  Is there were way to store 128-bit floating point values ?

You can see an example of mapping a decimal to either a list of bytes or scaling to a double in this interface.

Hello,

If we store data as list of bytes can we preform math operations? Could you please show an example of this?

The goal here is to store decimals up to 18 places without losing precision as its for Ethereum.

Hi ekallivrousis,

 

This thread may be useful to you:

https://community.kx.com/t5/KX-Technology/Big-Integers-larger-than-long/m-p/228

The comments link to c extension libraries which enable math operators on bigInt datatype. You could do similar for any datatype you need to operate on. This is needed as once the data is stored in bytes there are no native math operations .

 

Hey rocuinneagain,

Correct me if im wrong but this is to do math in C correct? I want to do math operations in the KDB database. The c program will be just putting the data into DB

These are examples of using the C interface to extend the Kdb+ database.

 

Documentation is here:

https://code.kx.com/q/interfaces/using-c-functions/ 

It shows an example of writing a new 'add' function in C:

#include"k.h"
#ifdef __cplusplus
extern "C"{
#endif

K add(K x,K y)
{
  if(x->t!=-KJ||y->t!=-KJ)
    return krr("type");
  return kj(x->j+y->j);
}

#ifdef __cplusplus
}
#endif

Then this can be used from inside a Kdb+ process:

q)add:(`add 2:(`add;2))
q)add[3;4]
7

This is a possible method to add math functions for 128-bit floating point numbers inside Kdb+.

Thank you for the above this is a big step in right direction for us.

if we store large decimal will precision be lost when storing in kdb and querying to display on ui via java