<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Math precision in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Math-precision/m-p/67504#M16863</link>
    <description>&lt;P&gt;I am seeing what appears to be inconsistencies in how Splunk does math in eval statements. Most of the time, it appears to be doing floating point math, but I've encountered a number of cases where it is doing something else.&lt;/P&gt;

&lt;P&gt;For example: &lt;/P&gt;

&lt;P&gt;Given fields p=8.250 and m=0.2, multiplying the values&lt;/P&gt;

&lt;BLOCKQUOTE&gt;
&lt;P&gt;| eval i = p*m&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;gives me a resulting value of 2.0 instead of the expected 1.65.&lt;/P&gt;

&lt;P&gt;However, if I add the exact function&lt;/P&gt;

&lt;BLOCKQUOTE&gt;
&lt;P&gt;| eval i = exact(p*m)&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;Splunk gives me the expected value of 1.65.&lt;/P&gt;

&lt;P&gt;In other cases, Splunk appears to be doing floating point multiplication and division without me explicitly using the exact() function.&lt;/P&gt;

&lt;P&gt;While rounding up 1.65 to 2.0 isn't a big difference, this creates a huge error when integer math results of many events are summed.&lt;/P&gt;

&lt;P&gt;The &lt;A href="http://docs.splunk.com/Documentation/Splunk/5.0.4/SearchReference/CommonEvalFunctions"&gt;documentation says&lt;/A&gt;:&lt;/P&gt;

&lt;BLOCKQUOTE&gt;
&lt;P&gt;exact(X) This function evaluates an&lt;BR /&gt;
expression X using double precision&lt;BR /&gt;
floating point arithmetic.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;So, my questions:&lt;/P&gt;

&lt;P&gt;How does Splunk do math when the exact() function is not used? Why do I sometimes get integer results instead of accurate floating point results and other times get floating point result values?&lt;/P&gt;

&lt;P&gt;Is the lack of precision documented anywhere other than the one-line description of the exact() function? If it is, I've not yet been able to find it. It seems to me that it is a huge deal to not do floating point math on real numbers without making it abundantly clear that the results are not exact.&lt;/P&gt;</description>
    <pubDate>Thu, 19 Sep 2013 06:10:29 GMT</pubDate>
    <dc:creator>joebensimo</dc:creator>
    <dc:date>2013-09-19T06:10:29Z</dc:date>
    <item>
      <title>Math precision</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Math-precision/m-p/67504#M16863</link>
      <description>&lt;P&gt;I am seeing what appears to be inconsistencies in how Splunk does math in eval statements. Most of the time, it appears to be doing floating point math, but I've encountered a number of cases where it is doing something else.&lt;/P&gt;

&lt;P&gt;For example: &lt;/P&gt;

&lt;P&gt;Given fields p=8.250 and m=0.2, multiplying the values&lt;/P&gt;

&lt;BLOCKQUOTE&gt;
&lt;P&gt;| eval i = p*m&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;gives me a resulting value of 2.0 instead of the expected 1.65.&lt;/P&gt;

&lt;P&gt;However, if I add the exact function&lt;/P&gt;

&lt;BLOCKQUOTE&gt;
&lt;P&gt;| eval i = exact(p*m)&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;Splunk gives me the expected value of 1.65.&lt;/P&gt;

&lt;P&gt;In other cases, Splunk appears to be doing floating point multiplication and division without me explicitly using the exact() function.&lt;/P&gt;

&lt;P&gt;While rounding up 1.65 to 2.0 isn't a big difference, this creates a huge error when integer math results of many events are summed.&lt;/P&gt;

&lt;P&gt;The &lt;A href="http://docs.splunk.com/Documentation/Splunk/5.0.4/SearchReference/CommonEvalFunctions"&gt;documentation says&lt;/A&gt;:&lt;/P&gt;

&lt;BLOCKQUOTE&gt;
&lt;P&gt;exact(X) This function evaluates an&lt;BR /&gt;
expression X using double precision&lt;BR /&gt;
floating point arithmetic.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;So, my questions:&lt;/P&gt;

&lt;P&gt;How does Splunk do math when the exact() function is not used? Why do I sometimes get integer results instead of accurate floating point results and other times get floating point result values?&lt;/P&gt;

&lt;P&gt;Is the lack of precision documented anywhere other than the one-line description of the exact() function? If it is, I've not yet been able to find it. It seems to me that it is a huge deal to not do floating point math on real numbers without making it abundantly clear that the results are not exact.&lt;/P&gt;</description>
      <pubDate>Thu, 19 Sep 2013 06:10:29 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Math-precision/m-p/67504#M16863</guid>
      <dc:creator>joebensimo</dc:creator>
      <dc:date>2013-09-19T06:10:29Z</dc:date>
    </item>
    <item>
      <title>Re: Math precision</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Math-precision/m-p/67505#M16864</link>
      <description>&lt;P&gt;When you run eval, the final result is rounded to a precision appropriate to the precision of the input results. The precision of the final result can be no greater than the precision of the least-precise input. Note that you will get results expressed differently if you do &lt;CODE&gt;8.250 * 0.2&lt;/CODE&gt; vs &lt;CODE&gt;8.250 * 0.20&lt;/CODE&gt; vs &lt;CODE&gt;8.250 * 0.200&lt;/CODE&gt;. This can be problem if by &lt;CODE&gt;0.2&lt;/CODE&gt;, you actually mean &lt;CODE&gt;0.2 to an arbitrary number of digits of precision&lt;/CODE&gt;. If that is what you mean, then you can express that by using &lt;CODE&gt;exact(0.2)&lt;/CODE&gt; instead of &lt;CODE&gt;0.2&lt;/CODE&gt;.&lt;/P&gt;</description>
      <pubDate>Thu, 19 Sep 2013 06:45:09 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Math-precision/m-p/67505#M16864</guid>
      <dc:creator>gkanapathy</dc:creator>
      <dc:date>2013-09-19T06:45:09Z</dc:date>
    </item>
    <item>
      <title>Re: Math precision</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Math-precision/m-p/67506#M16865</link>
      <description>&lt;P&gt;This seems to explain what I am seeing with multiplication. I just did some additional tests with a variety of precisions. It appears Splunk is using digits of precision and not digits after the decimal point. It doesn't matter where the decimal point is located. &lt;/P&gt;

&lt;P&gt;However, for division, I seem to get more digits of precision than any of the input values -- which is probably what I want it to do.&lt;/P&gt;

&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Thu, 19 Sep 2013 06:59:21 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Math-precision/m-p/67506#M16865</guid>
      <dc:creator>joebensimo</dc:creator>
      <dc:date>2013-09-19T06:59:21Z</dc:date>
    </item>
  </channel>
</rss>

