# precision with c++ interfaces

**URL:** <https://forum.kx.com/t/precision-with-c-interfaces/8531>\
**Category:** Community Support\
**Tags:** kdb-and-q\
**Created:** [December 4, 2013, 8:38am UTC](https://forum.kx.com/t/precision-with-c-interfaces/8531 "2013-12-04T08:38:00Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![naveen\_sharma](https://avatars.discourse-cdn.com/v4/letter/n/eb9ed0/32.png) [@naveen\_sharma](https://forum.kx.com/u/naveen_sharma)\
**Post date:** [December 4, 2013, 8:38am UTC](https://forum.kx.com/t/precision-with-c-interfaces/8531/1 "2013-12-04T08:38:00Z")

</div>

When I download data to c++ code, I see that floating point data is rounded to only 2 decimal places.  
I have tried declaring other doubles with more decimal digits and they can be printed fine.

Is there a way to change this in the interface?

Regards,

---

<div class="post-metadata">

**Author:** ![naveen\_sharma](https://avatars.discourse-cdn.com/v4/letter/n/eb9ed0/32.png) [@naveen\_sharma](https://forum.kx.com/u/naveen_sharma)\
**Post date:** [December 4, 2013, 9:00am UTC](https://forum.kx.com/t/precision-with-c-interfaces/8531/2 "2013-12-04T09:00:00Z")

</div>

On further checking it seems that c++ can only download 7 digits(including the decimal) from kdb server.  
So it can download 45.1799 or 5432.15.

Any way I can fix this?

On Wednesday, December 4, 2013 2:08:41 PM UTC+5:30, naveen sharma wrote:

> When I download data to c++ code, I see that floating point data is rounded to only 2 decimal places.  
> I have tried declaring other doubles with more decimal digits and they can be printed fine.
> 
> Is there a way to change this in the interface?
> 
> Regards,

---

<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:** [December 4, 2013, 9:22am UTC](https://forum.kx.com/t/precision-with-c-interfaces/8531/3 "2013-12-04T09:22:00Z")

</div>

If you are retrieving the binary representation of these floats, the only restriction is that of IEEE754, which is the same for c/c++. If you are retrieving the stringified representation from the server, this is controlled by \P.

e.g.

q)22%7

3.142857

q)\P 0

q)22%7

3.1428571428571428

see&nbsp;http://code.kx.com/wiki/Reference/DisplayPrecision

maybe you can post some c code to show what you are doing?

---

<div class="post-metadata">

**Author:** ![naveen\_sharma](https://avatars.discourse-cdn.com/v4/letter/n/eb9ed0/32.png) [@naveen\_sharma](https://forum.kx.com/u/naveen_sharma)\
**Post date:** [December 4, 2013, 9:28am UTC](https://forum.kx.com/t/precision-with-c-interfaces/8531/4 "2013-12-04T09:28:00Z")

</div>

K getTwoDat(servParams& spar, corrParams& cpar, string& dt)

{

&nbsp; &nbsp; &nbsp; &nbsp; string query = “corrTwoDat[” + cpar.symIn + “;” + to\_string(cpar.interval) + “;” + dt + “]”;

&nbsp; &nbsp; &nbsp; &nbsp; K flip, result, columnNames, data, dtlist, procDict;

&nbsp; &nbsp; &nbsp; &nbsp; int row, col, nCols, nRows, handle=khp((S)(spar.host).c\_str(), spar.port);

&nbsp; &nbsp; &nbsp; &nbsp; if(handle\<0)printf(“Cannot connect\n”),exit(1);

&nbsp; &nbsp; &nbsp; &nbsp; else if(!handle)printf(“Wrong credentials\n”),exit(1);

&nbsp; &nbsp; &nbsp; &nbsp; result=k(handle,(S)query.c\_str(),(K)0);

&nbsp; &nbsp; &nbsp; &nbsp; if(!result)

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf(“Network Error\n”),perror(“Network”),exit(1);

&nbsp; &nbsp; &nbsp; &nbsp; if(result-\>t==-128)

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf(“Server Error %s\n”,result-\>s),kclose(handle),exit(1);

&nbsp; &nbsp; &nbsp; &nbsp; kclose(handle);

&nbsp; &nbsp; &nbsp; &nbsp; if(result-\>t!=99&&result-\>t!=98) // accept table or dict

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf(“type %d\n”,result-\>t),r0(result),exit(1);

&nbsp; &nbsp; &nbsp; &nbsp; flip = kK(result-\>k)[1];

&nbsp; &nbsp; &nbsp; &nbsp; //flip = (result-\>k)[1];

&nbsp; &nbsp; &nbsp; &nbsp; //procDict = kK(

&nbsp; &nbsp; &nbsp; &nbsp; cout \<\< kE(kK(flip)[35])[0] \<\< “ss \n”;

&nbsp; &nbsp; &nbsp; &nbsp; return(flip);

}

This gets me the data. And as you can see the last line of the function prints just the two digits after decimal.

---

<div class="post-metadata">

**Author:** ![naveen\_sharma](https://avatars.discourse-cdn.com/v4/letter/n/eb9ed0/32.png) [@naveen\_sharma](https://forum.kx.com/u/naveen_sharma)\
**Post date:** [December 4, 2013, 9:31am UTC](https://forum.kx.com/t/precision-with-c-interfaces/8531/5 "2013-12-04T09:31:00Z")

</div>

Another weird thing, Running \P 0 doesn’t change the display of 22%7. I still get only 7 digits after decimal.

---

<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:** [December 4, 2013, 9:31am UTC](https://forum.kx.com/t/precision-with-c-interfaces/8531/6 "2013-12-04T09:31:00Z")

</div>

does this help?

http://www.cplusplus.com/reference/iomanip/setprecision/

---

<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:** [December 4, 2013, 9:47am UTC](https://forum.kx.com/t/precision-with-c-interfaces/8531/7 "2013-12-04T09:47:00Z")

</div>

yes, type real will stringify to max[8;system"P"].  
But I believe your issue here is with stringify of a single precision float in c++, not kdb+.

---

<div class="post-metadata">

**Author:** ![naveen\_sharma](https://avatars.discourse-cdn.com/v4/letter/n/eb9ed0/32.png) [@naveen\_sharma](https://forum.kx.com/u/naveen_sharma)\
**Post date:** [December 4, 2013, 10:19am UTC](https://forum.kx.com/t/precision-with-c-interfaces/8531/8 "2013-12-04T10:19:00Z")

</div>

Thanks Charles!  
It was just a display issue
