<?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>article Class() function with floating point value in Official Support Articles</title>
    <link>https://community.qlik.com/t5/Official-Support-Articles/Class-function-with-floating-point-value/ta-p/1777927</link>
    <description>&lt;P&gt;When floating point numbers are used in the parameter Class() function expected result cannot be achieved. For example,&amp;nbsp;&lt;SPAN&gt;expected output of class(0.15, 0.05) is "&lt;/SPAN&gt;&lt;SPAN&gt;0.15 &amp;lt;= x &amp;lt; 0.2", however output is "&lt;/SPAN&gt;&lt;SPAN&gt;0.1 &amp;lt;= x &amp;lt; 0.15" which 0.15 is not included in interval.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Please refer to following to learn about more class function.&lt;/SPAN&gt;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;SPAN&gt;&lt;A href="https://help.qlik.com/en-US/sense/November2020/Subsystems/Hub/Content/Sense_Hub/Scripting/ConditionalFunctions/class.htm" target="_self"&gt;class - script and chart function&lt;/A&gt;&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;/LI&gt;
&lt;/UL&gt;
&lt;H4&gt;Environment&lt;/H4&gt;
&lt;UL&gt;
&lt;LI&gt;QlikView, any version&lt;/LI&gt;
&lt;LI&gt;Qlik Sense, any version&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;H3&gt;Resolution&lt;/H3&gt;
&lt;P&gt;It is expected behavior.&amp;nbsp;&lt;/P&gt;
&lt;H3&gt;Workaround&lt;/H3&gt;
&lt;P&gt;To get desired result, f&lt;SPAN&gt;irst convert the floating point number to an integer (by multiplication), leaving no decimal part, and thereafter round the integer accordingly and finally convert the integer back to a float (by division).&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Please check following script in the example of class(0.15, 0.05) to get desired&amp;nbsp;"0.15 &amp;lt;= x &amp;lt; 0.2" result:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;SET DecimalSep='.';T1:
Load *, dual(String, First) as DualClass;
Load *, First&amp;amp;' &amp;lt;= x &amp;lt; '&amp;amp;Second as String;
Load *, subfield(Class100,' ',1)/100 as First, subfield(Class100,' ',5)/100 as Second;
Load *, class(Value,'0.05') as Class, class(Value*100,'0.05'*100) as Class100;
Load recno() as ID, recno()/100 as Value autogenerate 20;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;H3&gt;Cause&amp;nbsp;&lt;/H3&gt;
&lt;P&gt;&lt;SPAN&gt;This is only a problem when you use a single constant floating point value. When you have a base 10 system (like ours), it can only express fractions that use a prime factor of the base. The prime factors of 10 are 2 and 5. So 1/2, 1/4, 1/5, 1/8, and 1/10 can all be expressed cleanly because the denominators all use prime factors of 10. In contrast, 1/3, 1/6, and 1/7 are all repeating decimals because their denominators use a prime factor of 3 or 7. In binary (or base 2), the only prime factor is 2. So you can only express fractions cleanly which only contain 2 as a prime factor. In binary, 1/2, 1/4, 1/8 would all be expressed cleanly as decimals. While, 1/5 or 1/10 would be repeating decimals. So 0.1 and 0.2 (1/10 and 1/5) while clean decimals in a base 10 system, are repeating decimals in the base 2 system the computer is operating in. When you do math on these repeating decimals, you end up with leftovers which carry over when you convert the computer's base 2 (binary) number into a more human readable base 10 number.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;These are not errors in QlikView or Qlik Sense. And they are not errors in IEEE 754 either.&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;Rather, they represent errors in the expectation of binary floating point numbers. It's simply that some values cannot be exactly represented as binary numbers, so result gets rounding errors.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 10 May 2022 19:01:03 GMT</pubDate>
    <dc:creator>Mukhiddin</dc:creator>
    <dc:date>2022-05-10T19:01:03Z</dc:date>
    <item>
      <title>Class() function with floating point value</title>
      <link>https://community.qlik.com/t5/Official-Support-Articles/Class-function-with-floating-point-value/ta-p/1777927</link>
      <description>&lt;P&gt;When floating point numbers are used in the parameter Class() function expected result cannot be achieved. For example,&amp;nbsp;&lt;SPAN&gt;expected output of class(0.15, 0.05) is "&lt;/SPAN&gt;&lt;SPAN&gt;0.15 &amp;lt;= x &amp;lt; 0.2", however output is "&lt;/SPAN&gt;&lt;SPAN&gt;0.1 &amp;lt;= x &amp;lt; 0.15" which 0.15 is not included in interval.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Please refer to following to learn about more class function.&lt;/SPAN&gt;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;SPAN&gt;&lt;A href="https://help.qlik.com/en-US/sense/November2020/Subsystems/Hub/Content/Sense_Hub/Scripting/ConditionalFunctions/class.htm" target="_self"&gt;class - script and chart function&lt;/A&gt;&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;/LI&gt;
&lt;/UL&gt;
&lt;H4&gt;Environment&lt;/H4&gt;
&lt;UL&gt;
&lt;LI&gt;QlikView, any version&lt;/LI&gt;
&lt;LI&gt;Qlik Sense, any version&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;H3&gt;Resolution&lt;/H3&gt;
&lt;P&gt;It is expected behavior.&amp;nbsp;&lt;/P&gt;
&lt;H3&gt;Workaround&lt;/H3&gt;
&lt;P&gt;To get desired result, f&lt;SPAN&gt;irst convert the floating point number to an integer (by multiplication), leaving no decimal part, and thereafter round the integer accordingly and finally convert the integer back to a float (by division).&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Please check following script in the example of class(0.15, 0.05) to get desired&amp;nbsp;"0.15 &amp;lt;= x &amp;lt; 0.2" result:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;SET DecimalSep='.';T1:
Load *, dual(String, First) as DualClass;
Load *, First&amp;amp;' &amp;lt;= x &amp;lt; '&amp;amp;Second as String;
Load *, subfield(Class100,' ',1)/100 as First, subfield(Class100,' ',5)/100 as Second;
Load *, class(Value,'0.05') as Class, class(Value*100,'0.05'*100) as Class100;
Load recno() as ID, recno()/100 as Value autogenerate 20;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;H3&gt;Cause&amp;nbsp;&lt;/H3&gt;
&lt;P&gt;&lt;SPAN&gt;This is only a problem when you use a single constant floating point value. When you have a base 10 system (like ours), it can only express fractions that use a prime factor of the base. The prime factors of 10 are 2 and 5. So 1/2, 1/4, 1/5, 1/8, and 1/10 can all be expressed cleanly because the denominators all use prime factors of 10. In contrast, 1/3, 1/6, and 1/7 are all repeating decimals because their denominators use a prime factor of 3 or 7. In binary (or base 2), the only prime factor is 2. So you can only express fractions cleanly which only contain 2 as a prime factor. In binary, 1/2, 1/4, 1/8 would all be expressed cleanly as decimals. While, 1/5 or 1/10 would be repeating decimals. So 0.1 and 0.2 (1/10 and 1/5) while clean decimals in a base 10 system, are repeating decimals in the base 2 system the computer is operating in. When you do math on these repeating decimals, you end up with leftovers which carry over when you convert the computer's base 2 (binary) number into a more human readable base 10 number.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;These are not errors in QlikView or Qlik Sense. And they are not errors in IEEE 754 either.&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;Rather, they represent errors in the expectation of binary floating point numbers. It's simply that some values cannot be exactly represented as binary numbers, so result gets rounding errors.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 10 May 2022 19:01:03 GMT</pubDate>
      <guid>https://community.qlik.com/t5/Official-Support-Articles/Class-function-with-floating-point-value/ta-p/1777927</guid>
      <dc:creator>Mukhiddin</dc:creator>
      <dc:date>2022-05-10T19:01:03Z</dc:date>
    </item>
  </channel>
</rss>

