# conversion error from double in c++ to long or float in kdb+

**URL:** <https://forum.kx.com/t/conversion-error-from-double-in-c-to-long-or-float-in-kdb/9835>\
**Category:** Community Support\
**Tags:** kdb-and-q\
**Created:** [May 19, 2015, 6:02am UTC](https://forum.kx.com/t/conversion-error-from-double-in-c-to-long-or-float-in-kdb/9835 "2015-05-19T06:02:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![guy1pitman](https://avatars.discourse-cdn.com/v4/letter/g/ee59a6/32.png) [@guy1pitman](https://forum.kx.com/u/guy1pitman)\
**Post date:** [May 19, 2015, 6:02am UTC](https://forum.kx.com/t/conversion-error-from-double-in-c-to-long-or-float-in-kdb/9835/1 "2015-05-19T06:02:00Z")

</div>

I am trying to convert some double types in c++ to longs or floats in kdb+

using kj and KJ to convert to kdb+ the following nos in c++

1068080561

-639597367

-1108929061

-1115727465

1072032170

get converted to&nbsp;

47524255114672j and 4 other nos differing only in the last few digits

using kf and KF they get converted to&nbsp;

2.328563e-310 &nbsp;and nos differing only in the last few digits. &nbsp; Can you give some help here?

Many thanks&nbsp;

---

<div class="post-metadata">

**Author:** ![charlie1](https://avatars.discourse-cdn.com/v4/letter/c/ec9cab/32.png) [@charlie1](https://forum.kx.com/u/charlie1)\
**Post date:** [May 29, 2015, 12:33pm UTC](https://forum.kx.com/t/conversion-error-from-double-in-c-to-long-or-float-in-kdb/9835/2 "2015-05-29T12:33:00Z")

</div>

please can you post code that exhibits the issue?

The following works for me. What are you doing differently?

$ more guy.cpp  
#include"k.h"

extern “C” K f(K x){  
&nbsp; K y;  
&nbsp; J i;  
&nbsp; if(KF==xt){  
&nbsp;&nbsp;&nbsp; y=ktn(KJ,xn);  
&nbsp;&nbsp;&nbsp; for(i=0;i\<xn;i++)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; kJ(y)[i]=kF(x)[i];  
&nbsp; }  
&nbsp; else if(-KF==xt)  
&nbsp;&nbsp;&nbsp; y=kj(xf);  
&nbsp; else  
&nbsp;&nbsp;&nbsp; y=krr((S)“type”);  
&nbsp; return y;  
}  
&nbsp;&nbsp;&nbsp;  
$ more guy.q  
f:(`$"./guy")2:(`f;1)  
a:1068080561 -639597367 -1108929061 -1115727465 1072032170f  
f a  
f each a  
$ g++ -bundle -undefined dynamic\_lookup guy.cpp -DKXVER=3 -fPIC -o2 -o guy.so -m32  
$ ./q guy.q  
KDB+ 3.3t 2015.05.27 Copyright (C) 1993-2015 Kx Systems  
m32/ 4()core ..

1068080561 -639597367 -1108929061 -1115727465 1072032170  
1068080561 -639597367 -1108929061 -1115727465 1072032170  
q)\

likewise the following works for me

#include"k.h"

extern “C” K f(K x){  
&nbsp; double d={1068080561,-639597367,-1108929061,-1115727465,1072032170};  
&nbsp; K y=ktn(KF,5);  
&nbsp; J i;  
&nbsp; for(i=0;i\<y-\>n;i++)  
&nbsp;&nbsp;&nbsp; kF(y)[i]=d[i];  
&nbsp; return y;  
}

$ ./q  
KDB+ 3.3t 2015.05.27 Copyright (C) 1993-2015 Kx Systems  
m32/ 4()core  
q)\P 0  
f:(`$"./guy")2:(`f;1)q)  
q)f  
1068080561 -639597367 -1108929061 -1115727465 1072032170f  
q)\
