<?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>atof not working in LPC 1788</title><link>https://community.arm.com/developer/tools-software/tools/f/keil-forum/34207/atof-not-working-in-lpc-1788</link><description> 
I am writing Programm using LPC1788, with RTX os . 
Through out Programm i have used atof function many times, but now if
m adding new atof function, it is not working Properly, even i dont
get any error while compilation or debugging. 

 
Alo if i</description><dc:language>en-US</dc:language><generator>Telligent Community 10</generator><item><title>RE: atof not working in LPC 1788</title><link>https://community.arm.com/thread/108091?ContentTypeID=1</link><pubDate>Wed, 18 Feb 2015 04:27:46 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:a4e843b6-454d-4998-9124-af393e2634de</guid><dc:creator>edPer Westermark</dc:creator><description>&lt;p&gt;&lt;p&gt;
No, I have no idea why your atof() doesn&amp;#39;t work.&lt;/p&gt;

&lt;p&gt;
But that function you posted is an excellent example of how you
can improve as a programmer by thinking a bit about how you write
your code.&lt;/p&gt;

&lt;p&gt;
You have a huge amount of hardcoded assumptions - but have not
documented a single assumption in the function header.&lt;/p&gt;

&lt;p&gt;
What about code like (untested written on-the-fly):&lt;/p&gt;

&lt;pre&gt;
int64_t val = 0,prev = 0;
float scale = 1.0;

if (*s == &amp;#39;-&amp;#39;) {
    scale = -1.0;
    s++;
}
if (*s &amp;lt; &amp;#39;0&amp;#39; || *S &amp;gt; &amp;#39;9&amp;#39;) {
    fail_invalid number();
}
while (*s &amp;gt;= &amp;#39;0&amp;#39; &amp;amp;&amp;amp; *s &amp;lt;= &amp;#39;9&amp;#39;) {
    val = 10*val + *s-&amp;#39;0&amp;#39;;
    if (val &amp;lt; prev) {
        fail_overflow();
    }
    prev = val;
    s++;
}
if (*s == &amp;#39;.&amp;#39;) {
    s++;
    while (*s &amp;gt;= &amp;#39;0&amp;#39; &amp;amp;&amp;amp; *s &amp;lt;= &amp;#39;9&amp;#39;) {
        val = 10*val + *s-&amp;#39;0&amp;#39;;
        if (val &amp;lt; prev) {
            fail_overflow();
        } else {
            scale *= 0.1;
        }
        s++;
    }
}
if (*s) {
    fail_extra_characters();
}
return val * scale;
&lt;/pre&gt;

&lt;p&gt;
&lt;br /&gt;
Suddenly not hard-coded to a fixed number of digits before the
decimal point.&lt;br /&gt;
And not hard-coded if there is a decimal point.&lt;br /&gt;
And not hard-coded how many decimals after the decimal point.&lt;/p&gt;

&lt;p&gt;
Assumptions? Yes, still a lot of them.&lt;br /&gt;
- No support for 1.10E12&lt;br /&gt;
- No support for a silly number of digits that that doesn&amp;#39;t fit in a
int64_t but can (with rounding) be represented in a float or
double.&lt;br /&gt;
- ...&lt;/p&gt;

&lt;p&gt;
But thinking about limitations and possible algorithms really is
quite important. And catching overflows/underflows or constructs that
breaks the assumptions is quite important. It&amp;#39;s too easy to destroy
things or hurt people if running code that doesn&amp;#39;t document and when
possible test assumptions.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: atof not working in LPC 1788</title><link>https://community.arm.com/thread/93339?ContentTypeID=1</link><pubDate>Tue, 17 Feb 2015 22:33:51 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:cbd87b60-d518-43bc-bfed-31bef762b050</guid><dc:creator>Surekha B</dc:creator><description>&lt;p&gt;&lt;p&gt;
Thanks for fast response...&lt;br /&gt;
hoping for the best..&lt;/p&gt;

&lt;p&gt;
whatever code i had posted on thred is used for manual calculation
for float conversion.&lt;/p&gt;

&lt;p&gt;
because atof library function is not working... why atof function
is not working, that i dont understand,could please answer me why
atof function gives me incorrect value , if i am converting asccii
buffer value into float usign atof...actully the same library
function i have used throughout same programm in different functions
and different files too...but nowonwards if i m adding new atof
function in code .. it gives me incorrect value...please guide
me...&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: atof not working in LPC 1788</title><link>https://community.arm.com/thread/69084?ContentTypeID=1</link><pubDate>Tue, 17 Feb 2015 21:41:51 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:83b2444c-f683-4951-9ba4-cfc0b6287d09</guid><dc:creator>edPer Westermark</dc:creator><description>&lt;p&gt;&lt;p&gt;
Sorry but you have&lt;br /&gt;
1) Posted code without reading the instructions for how to tag the
code so it gets readable (didn&amp;#39;t you spot the text directly above the
message input box?)&lt;br /&gt;
2) Written quite complex code with lots of hard-coded rules that
aren&amp;#39;t really needed.&lt;br /&gt;
3) Failed to write a proper description of exactly what you expect
the result should be of your function - including actual example
data.&lt;br /&gt;
4) You talk about floating point but make use of / and % which are
more related to fixed-point numbers.&lt;br /&gt;
5) You seem to have missed the ability to loop through digits instead
of hard-code logic depending on number of characters.&lt;br /&gt;
6) You use temporary variables for some arbitrary reason.&lt;/p&gt;

&lt;p&gt;
Note that&lt;/p&gt;

&lt;pre&gt;
(float)return_value + ((float)deci_point/1000);
&lt;/pre&gt;

&lt;p&gt;
&lt;br /&gt;
is a quite interesting construct.&lt;/p&gt;

&lt;p&gt;
The value 123.456 can be converted to the fixed-point integer
123456 with three implied decimals. And this value can then be
converted to a floating point value with:&lt;/p&gt;

&lt;pre&gt;
((float)fp)/1000;
&lt;/pre&gt;

&lt;p&gt;
&lt;br /&gt;
or&lt;/p&gt;

&lt;pre&gt;
fp*0.001;
&lt;/pre&gt;

&lt;p&gt;
What do you think your construct would do if the input number is
negative? Should you then add or subtract your fractional part?&lt;/p&gt;

&lt;p&gt;
An interesting thing here is why you have floating point all over
your program? Most embedded programs runs much faster if written with
fixed-point logic instead of floating point, since lots of
microcontrollers doesn&amp;#39;t have any floating point support and so
requires all floating point calculations to be emulated by a rather
large emulation library. Another advantage with fixed point is the
ability to work with huge precisions - it&amp;#39;s possible to use a
big-number library and work with hundreds of digits without losing
important value digits.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>