# Does floating point arithmetic bother you?

**URL:** <https://forum.kx.com/t/does-floating-point-arithmetic-bother-you/11879>\
**Category:** Community Support\
**Tags:** kdb-and-q\
**Created:** [March 9, 2019, 1:10am UTC](https://forum.kx.com/t/does-floating-point-arithmetic-bother-you/11879 "2019-03-09T01:10:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![yi\_codeplayer](https://avatars.discourse-cdn.com/v4/letter/y/f4b2a3/32.png) [@yi\_codeplayer](https://forum.kx.com/u/yi_codeplayer)\
**Post date:** [March 9, 2019, 1:10am UTC](https://forum.kx.com/t/does-floating-point-arithmetic-bother-you/11879/1 "2019-03-09T01:10:00Z")

</div>

I=E2=80=99m new to kdb+, since it=E2=80=99s used mainly by financial people= , and it use IEEE floating point number. Doesn=E2=80=99t the inaccuracy bot= her you, how do you deal with that?

---

<div class="post-metadata">

**Author:** ![jwbuitenhuis](https://avatars.discourse-cdn.com/v4/letter/j/45deac/32.png) [@jwbuitenhuis](https://forum.kx.com/u/jwbuitenhuis)\
**Post date:** [March 10, 2019, 10:02am UTC](https://forum.kx.com/t/does-floating-point-arithmetic-bother-you/11879/2 "2019-03-10T10:02:00Z")

</div>

Hi Yi,

Have you reviewed this?

https://code.kx.com/q/cookbook/precision/#float-precision

Can you be more specific about what problems you encounter?

---

<div class="post-metadata">

**Author:** ![yi\_codeplayer](https://avatars.discourse-cdn.com/v4/letter/y/f4b2a3/32.png) [@yi\_codeplayer](https://forum.kx.com/u/yi_codeplayer)\
**Post date:** [March 12, 2019, 1:34am UTC](https://forum.kx.com/t/does-floating-point-arithmetic-bother-you/11879/3 "2019-03-12T01:34:00Z")

</div>

Sometimes, the imprecision can be magnified into something problematic:q)0.1\*100000000000000 - (0.3-0.2)\*10000000000000000.0015625000000000001On Sun, Mar 10, 2019 at 6:03 PM JW Buitenhuis wrote:\>\> Hi Yi,\>\> Have you reviewed this?\> [https://code.kx.com/q/cookbook/precision/#float-precision&gt;&gt](https://code.kx.com/q/cookbook/precision/#float-precision&gt;&gt); Can you be more specific about what problems you encounter?\>\>\> On Sat, 9 Mar 2019 at 16:59, yi huang \<yi.codeplayer\> wrote:\>\>\>\> Im new to kdb+, since its used mainly by financial people, and it use IEEE floating point number. Doesnt the inaccuracy bother you, how do you deal with that?\>\>\>\> –\>\>

Submitted via Google Groups\</yi.codeplayer\>

---

<div class="post-metadata">

**Author:** ![Flying1](https://avatars.discourse-cdn.com/v4/letter/f/f4b2a3/32.png) [@Flying1](https://forum.kx.com/u/Flying1)\
**Post date:** [March 12, 2019, 9:10am UTC](https://forum.kx.com/t/does-floating-point-arithmetic-bother-you/11879/4 "2019-03-12T09:10:00Z")

</div>

This is nothing new in kdb+. All systems based on the IEEE standard will have similar problems. That’s why this is a must-read for everybody:&nbsp;[https://docs.oracle.com/cd/E19957-01/806-3568/ncg\_goldberg.html](https://docs.oracle.com/cd/E19957-01/806-3568/ncg%5C_goldberg.html)

---

<div class="post-metadata">

**Author:** ![Oldrich\_smejkal](https://avatars.discourse-cdn.com/v4/letter/o/bbe5ce/32.png) [@Oldrich\_smejkal](https://forum.kx.com/u/Oldrich_smejkal)\
**Post date:** [March 12, 2019, 12:23pm UTC](https://forum.kx.com/t/does-floating-point-arithmetic-bother-you/11879/5 "2019-03-12T12:23:00Z")

</div>

It’s worth to mention that standard approach to this is not use floating points numbers for price like data and use some fixed precision type like: [https://docs.oracle.com/cd/E17952\_01/mysql-5.0-en/precision-math-decimal-characteristics.html](https://docs.oracle.com/cd/E17952%5C_01/mysql-5.0-en/precision-math-decimal-characteristics.html) Unfortunatelly there isn’t anything like this in Kdb, so you have basically tradeoff between more intuitive approcach using floats and deal with possible issues depending on use case or use some integer based approach - storing price in tick for example or using some multiplier. This can be confusing though if there are users using data directly (otherwise you would can use some translation in api layer..)

---

<div class="post-metadata">

**Author:** ![\_Oz](https://avatars.discourse-cdn.com/v4/letter/_/f07891/32.png) [@\_Oz](https://forum.kx.com/u/_Oz)\
**Post date:** [March 12, 2019, 3:58pm UTC](https://forum.kx.com/t/does-floating-point-arithmetic-bother-you/11879/6 "2019-03-12T15:58:00Z")

</div>

Thing to watch out for (in my experience) is grouping by price levels (especially when calculating some of them) because ~ is using comp tolerance, while grouping (incl. by) obviously doesn’t.

Cheers,

Oleg
