<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://community.arm.com/utility/feedstylesheets/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>Invalid answer when casting from float to int</title><link>https://community.arm.com/developer/tools-software/tools/f/keil-forum/34183/invalid-answer-when-casting-from-float-to-int</link><description> 
The following code, simple as it is, fails to work. I suspect it&amp;#39;s
a brain short 
on my part, and willingly accept the public humiliation. Perhaps it&amp;#39;s
an actual 
problem that I am just now uncovering. I am new to the toolset, so
all bets are off. </description><dc:language>en-US</dc:language><generator>Telligent Community 10</generator><item><title>RE: Invalid answer when casting from float to int</title><link>https://community.arm.com/thread/137347?ContentTypeID=1</link><pubDate>Thu, 11 Aug 2016 17:37:04 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:c7e329d3-6bf2-4de6-a611-a2089fb5e332</guid><dc:creator>John Linq</dc:creator><description>&lt;p&gt;&lt;pre&gt;
        //      On entry, gain is 27.893
        fTemp = gain * 1000.0;        //  gain - preserve three decimal places
        //      &lt;b&gt;fTemp now 27893.0&lt;/b&gt;
        lTemp = &lt;b&gt;(uint32_t)&lt;/b&gt;fTemp;
        //      lTemp now 0x6C5F (27743, while I expect 27893)
&lt;/pre&gt;

&lt;p&gt;
27893=0x6CF5&lt;br /&gt;
27743=0x6C5F&lt;br /&gt;
F and 5 are swapped.&lt;br /&gt;
Maybe the magic is at (uint32_t)?&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Invalid answer when casting from float to int</title><link>https://community.arm.com/thread/129252?ContentTypeID=1</link><pubDate>Thu, 11 Aug 2016 13:16:24 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:2d73aa0d-24ec-4fd6-9c49-c84c421663a6</guid><dc:creator>Westonsupermare Pier</dc:creator><description>&lt;p&gt;&lt;p&gt;
Seems to work fine on 4.71a, 4.73 and 4.74. both with and without
FPU on a Cortex-M4. Don&amp;#39;t have 4.72 installed.&lt;/p&gt;

&lt;p&gt;
I&amp;#39;d recommend including math.h and using the f suffix for float
constants, but either way John&amp;#39;s code functions.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Invalid answer when casting from float to int</title><link>https://community.arm.com/thread/120833?ContentTypeID=1</link><pubDate>Thu, 11 Aug 2016 10:08:07 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:5233e692-853b-4b8c-8e41-94adaa834917</guid><dc:creator>John Griswold</dc:creator><description>&lt;p&gt;&lt;p&gt;
Thanks, all. I used the double when float was clearly broken, and
that fixed the problem. That it works on Windows is irrelevant,
insofar as I am not targeting a Windows platform. But I agree - it
&lt;i&gt;should&lt;/i&gt; work.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Invalid answer when casting from float to int</title><link>https://community.arm.com/thread/108854?ContentTypeID=1</link><pubDate>Wed, 10 Aug 2016 20:59:39 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:9c699bca-99a4-4403-ae68-ba635155d9aa</guid><dc:creator>John Linq</dc:creator><description>&lt;p&gt;&lt;p&gt;
I think float is enough for your case, double is unnecessary. But
I am not so sure.&lt;br /&gt;
The below code runs on Windows:&lt;/p&gt;

&lt;pre&gt;
#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;stdint.h&amp;gt;

int main(void)
{
    float           gain = 27.893;
    float           fTemp;
    uint32_t        lTemp;
    uint16_t        temp;

    fTemp = gain * 1000.0;
    lTemp = (uint32_t)fTemp;
    temp = lTemp &amp;amp; 0xFFFF;

    printf(&amp;quot;%f, %d, %d&amp;quot;, fTemp, lTemp, temp);
}
&lt;/pre&gt;

&lt;p&gt;
And the result is:&lt;/p&gt;

&lt;pre&gt;
27893.000000, 27893, 27893
Process returned 26 (0x1A)   execution time : -0.000 s
Press any key to continue.
&lt;/pre&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Invalid answer when casting from float to int</title><link>https://community.arm.com/thread/93780?ContentTypeID=1</link><pubDate>Wed, 10 Aug 2016 11:12:01 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:a437850b-ffd8-453e-b7ff-a4d3903b9627</guid><dc:creator>&amp;#178;erik malund</dc:creator><description>&lt;p&gt;&lt;p&gt;
floats is about the only means of computing 2 * = 3.94&lt;/p&gt;

&lt;p&gt;
float is NOT precise&lt;/p&gt;

&lt;p&gt;
&lt;i&gt;I changed the floats to doubles.&lt;/i&gt;&lt;br /&gt;
waaay too many code float just because there is a decimal point
somewhere.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Invalid answer when casting from float to int</title><link>https://community.arm.com/thread/64924?ContentTypeID=1</link><pubDate>Wed, 10 Aug 2016 09:44:23 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:4e7911b3-881e-46d7-af89-d1d8b553821c</guid><dc:creator>John Griswold</dc:creator><description>&lt;p&gt;&lt;p&gt;
It worked out OK when, at the suggestion of a colleague, I changed
the floats to doubles.&lt;br /&gt;
So, I guess I&amp;#39;m off of the float wagon.&lt;/p&gt;

&lt;p&gt;
Hope this helps someone else.&lt;/p&gt;

&lt;p&gt;
John&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>