<?xml-stylesheet type="text/xsl" href="https://community.element14.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>division operation with float variable mplabx v6.00 xc16 (v2.00)</title><link>/technologies/embedded/b/blog/posts/division-operation-with-float-variable-mplabx-v6-00-xc16-v2-00</link><description>hi every one.
I have some problem trying make some division with float variable.
some one can help me please.


main.c

while(0)
{
 menu_state = PARAM_MENU;
 param_menu = PARAM_MENU_FREQ;
 pwmq_freq_ui16 = encoder_knob_read();
 set_PWM1_F...</description><dc:language>en-US</dc:language><generator>Telligent Community 12</generator><item><title>RE: division operation with float variable mplabx v6.00 xc16 (v2.00)</title><link>https://community.element14.com/technologies/embedded/b/blog/posts/division-operation-with-float-variable-mplabx-v6-00-xc16-v2-00</link><pubDate>Mon, 20 May 2024 09:56:51 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:176c10a8-4bb9-49aa-97ff-7103d9376ef2</guid><dc:creator>jc2048</dc:creator><slash:comments>1</slash:comments><description>&lt;p&gt;Can the value returned by the read encoder routine be zero? If so, you&amp;#39;re going to hit the problem of dealing with a divide by zero error at some point when doing the floating point reciprocal.&lt;/p&gt;
&lt;p&gt;It&amp;#39;s possible that that might be what&amp;#39;s going on here. The watch variables going to zero hints at a reset, which might be coming from the floating routines throwing an exception (it&amp;#39;s a long time since I worked with PICs, and I never used the float stuff much for anything, anyway - they waste too much time in an embedded system where there&amp;#39;s a lot going on - so I can&amp;#39;t tell you directly).&lt;/p&gt;
&lt;p&gt;Even if it isn&amp;#39;t the problem here, it&amp;#39;s still a good idea to anticipate it and deal with it in your code.&lt;/p&gt;
&lt;p&gt;You could a) ensure the read encoder never returns zero; b) test for zero just before doing the division and substitute whatever answer you want under those circumstances; or c) understand properly what happens after the float division by zero and deal with it there, either dealing with the trap, if there is one, or handling a NaN or &amp;#39;infinity&amp;#39; value handed back, if that&amp;#39;s what the floating routines do.&lt;/p&gt;
&lt;p&gt;The last may not be very portable - I don&amp;#39;t think floating point libraries are very consistent in how they deal with the outcome of a divide by zero.&lt;/p&gt;&lt;img src="https://community.element14.com/aggbug?PostID=27979&amp;AppID=7&amp;AppType=Weblog&amp;ContentType=0" width="1" height="1"&gt;</description></item><item><title>RE: division operation with float variable mplabx v6.00 xc16 (v2.00)</title><link>https://community.element14.com/technologies/embedded/b/blog/posts/division-operation-with-float-variable-mplabx-v6-00-xc16-v2-00</link><pubDate>Sat, 18 May 2024 19:33:36 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:176c10a8-4bb9-49aa-97ff-7103d9376ef2</guid><dc:creator>genebren</dc:creator><slash:comments>2</slash:comments><description>&lt;p&gt;You need to declare Freq_PWM1_F outside of the function.&amp;nbsp; If it is inside a function (as it is now) it is declared on the stack frame, which is not guaranteed to be available after the function call.&amp;nbsp; I am a little confused to how this was allowed to compile without an error.&amp;nbsp; &amp;nbsp;&lt;/p&gt;&lt;img src="https://community.element14.com/aggbug?PostID=27979&amp;AppID=7&amp;AppType=Weblog&amp;ContentType=0" width="1" height="1"&gt;</description></item><item><title>RE: division operation with float variable mplabx v6.00 xc16 (v2.00)</title><link>https://community.element14.com/technologies/embedded/b/blog/posts/division-operation-with-float-variable-mplabx-v6-00-xc16-v2-00</link><pubDate>Sat, 18 May 2024 17:08:50 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:176c10a8-4bb9-49aa-97ff-7103d9376ef2</guid><dc:creator>genebren</dc:creator><slash:comments>1</slash:comments><description>&lt;p&gt;You might want to try:&lt;/p&gt;
&lt;p&gt;&lt;span&gt;float&lt;/span&gt;&lt;span&gt; Freq_PWM1_F = &lt;/span&gt;&lt;span&gt;1.0/(float)&lt;/span&gt;&lt;span&gt;Freq_PWM1;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;&lt;img src="https://community.element14.com/aggbug?PostID=27979&amp;AppID=7&amp;AppType=Weblog&amp;ContentType=0" width="1" height="1"&gt;</description></item><item><title>RE: division operation with float variable mplabx v6.00 xc16 (v2.00)</title><link>https://community.element14.com/technologies/embedded/b/blog/posts/division-operation-with-float-variable-mplabx-v6-00-xc16-v2-00</link><pubDate>Sat, 18 May 2024 16:12:24 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:176c10a8-4bb9-49aa-97ff-7103d9376ef2</guid><dc:creator>javagoza</dc:creator><slash:comments>1</slash:comments><description>&lt;p&gt;&lt;span&gt;&lt;/span&gt;I don&amp;#39;t understand what you are asking. It seems to me that you are comparing two different statements.&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;pre&gt;&lt;span&gt;float&lt;/span&gt; Freq_PWM1_F = (&lt;span&gt;float&lt;/span&gt;)&lt;span&gt;1&lt;/span&gt;/&lt;span&gt;1000&lt;/span&gt;;
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;This is a constant expression, so the compiler can optimize it.&lt;/p&gt;
&lt;p&gt;In the other case:&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;pre&gt;&lt;span&gt;float&lt;/span&gt; Freq_PWM1_F = (&lt;span&gt;float&lt;/span&gt;)&lt;span&gt;1&lt;/span&gt;/Freq_PWM1;
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;the computed value for Freq_PWM1_F cannot be shown because Freq_PWM1 has not been evaluated yet.&lt;/p&gt;&lt;img src="https://community.element14.com/aggbug?PostID=27979&amp;AppID=7&amp;AppType=Weblog&amp;ContentType=0" width="1" height="1"&gt;</description></item></channel></rss>