<?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>S12 / MagniV MicrocontrollersのトピックRe: float calculation problem</title>
    <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/float-calculation-problem/m-p/218725#M9162</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi mate&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;you are right... in all you said &lt;IMG alt=":smileyhappy:" class="emoticon emoticon-smileyhappy" id="smileyhappy" src="http://freescale.i.lithium.com/i/smilies/16x16_smiley-happy.gif" title="Smiley Happy" /&gt;&lt;/P&gt;&lt;P&gt;maybe your better in guessing than you believe &lt;IMG alt=":smileywink:" class="emoticon emoticon-smileywink" id="smileywink" src="http://freescale.i.lithium.com/i/smilies/16x16_smiley-wink.gif" title="Smiley Wink" /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;it was shortly before lunch break so i posted this in a heck hoping to be solved after lunch...&lt;/P&gt;&lt;P&gt;and it works...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;changed to &amp;nbsp; sumxx += (float)x[i] * (float)x[i];&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;my array is an array of adc values in int, so there the overflow happened. with the cast it works.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;next time i will provide more informations &lt;IMG alt=":smileyhappy:" class="emoticon emoticon-smileyhappy" id="smileyhappy" src="http://freescale.i.lithium.com/i/smilies/16x16_smiley-happy.gif" title="Smiley Happy" /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 17 Aug 2010 18:07:36 GMT</pubDate>
    <dc:creator>HarryHirn</dc:creator>
    <dc:date>2010-08-17T18:07:36Z</dc:date>
    <item>
      <title>float calculation problem</title>
      <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/float-calculation-problem/m-p/218723#M9160</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;hi guys&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;i got a problem with my hc12 and a calculation...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;what i wanna to do: do a correlation of 2 signals&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;i found and codesnippet in an book for c# and hooked it over to my controller:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;int i = 0;float sumx = 0.0;float sumy = 0.0;float sumxx = 0.0;float sumyy = 0.0;float sumxy = 0.0;float rho = 0.0;for(i = 0; i &amp;lt; length; i++){&amp;nbsp; sumx += x[i];&amp;nbsp; sumy += y[i];&amp;nbsp; sumxx += x[i] * x[i];&amp;nbsp; sumyy += y[i] * y[i];&amp;nbsp; sumxy += x[i] * y[i];&amp;nbsp;&amp;nbsp;&amp;nbsp; }rho = ((length * sumxy) - (sumx * sumy)) / (sqrtf(((length * sumxx) - (sumx * sumx)) * ((length * sumyy) - (sumy * sumy))));&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;the problem starts right at the beginning by calculating sumxx...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;my x-array contains: { 1, 4, 2105,.....}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;stepping through the function gives the sumxx values { 1, 17, -25406, ...}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;the first two values are correct, but why does the third cracks up? should be something like 4431042... or something like 4.43102E06&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;because of the values the correlation becomes inverted, sometimes completely wrong and sometimes greater 1 &lt;SPAN class="lia-unicode-emoji" title=":confused_face:"&gt;&lt;LI-EMOJI id="lia_confused-face" title=":confused_face:"&gt;&lt;/LI-EMOJI&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;i got another realization of a correlation implemented which calculates correctly with same values but is far more runtime unfriendly. there the values go up toe 8,xyzE07 without a problem...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;whats the problem with summing up floats that way?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 17 Aug 2010 16:29:59 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/float-calculation-problem/m-p/218723#M9160</guid>
      <dc:creator>HarryHirn</dc:creator>
      <dc:date>2010-08-17T16:29:59Z</dc:date>
    </item>
    <item>
      <title>Re: float calculation problem</title>
      <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/float-calculation-problem/m-p/218724#M9161</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;First of all, if you want to be helped next time, please provide at least all necessary declarations. Why should I guess what type is your x array? Compileable small snippet could be even better. It would dramatically ease helping you.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I guess x[] array&amp;nbsp;is array of int's or short's. If so, then&amp;nbsp;no wonder integer 2105 * integer 2105 overflows.&amp;nbsp;Integer 2105 * 2105 is chopped to 0x9CB1 (-25423). Next -25423 is converted to float and added to&amp;nbsp;sumxx.&lt;/P&gt;&lt;P&gt;To fix it change your expressions like this&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; sumxx += (long)x[i] * x[i];&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;or this&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; sumxx += (float)x[i] * x[i];&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 17 Aug 2010 17:46:55 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/float-calculation-problem/m-p/218724#M9161</guid>
      <dc:creator>kef</dc:creator>
      <dc:date>2010-08-17T17:46:55Z</dc:date>
    </item>
    <item>
      <title>Re: float calculation problem</title>
      <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/float-calculation-problem/m-p/218725#M9162</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi mate&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;you are right... in all you said &lt;IMG alt=":smileyhappy:" class="emoticon emoticon-smileyhappy" id="smileyhappy" src="http://freescale.i.lithium.com/i/smilies/16x16_smiley-happy.gif" title="Smiley Happy" /&gt;&lt;/P&gt;&lt;P&gt;maybe your better in guessing than you believe &lt;IMG alt=":smileywink:" class="emoticon emoticon-smileywink" id="smileywink" src="http://freescale.i.lithium.com/i/smilies/16x16_smiley-wink.gif" title="Smiley Wink" /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;it was shortly before lunch break so i posted this in a heck hoping to be solved after lunch...&lt;/P&gt;&lt;P&gt;and it works...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;changed to &amp;nbsp; sumxx += (float)x[i] * (float)x[i];&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;my array is an array of adc values in int, so there the overflow happened. with the cast it works.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;next time i will provide more informations &lt;IMG alt=":smileyhappy:" class="emoticon emoticon-smileyhappy" id="smileyhappy" src="http://freescale.i.lithium.com/i/smilies/16x16_smiley-happy.gif" title="Smiley Happy" /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 17 Aug 2010 18:07:36 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/float-calculation-problem/m-p/218725#M9162</guid>
      <dc:creator>HarryHirn</dc:creator>
      <dc:date>2010-08-17T18:07:36Z</dc:date>
    </item>
  </channel>
</rss>

