<?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>LPC MicrocontrollersのトピックRe: printf does not print float value correctly</title>
    <link>https://community.nxp.com/t5/LPC-Microcontrollers/printf-does-not-print-float-value-correctly/m-p/525569#M8205</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;STRONG&gt;Content originally posted in LPCWare by bavarian on Wed Jun 27 00:33:21 MST 2012&lt;/STRONG&gt;&lt;BR /&gt;&lt;SPAN&gt;Hello Jim,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;not that much information about your problem ;-)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;One thing: the behavior of printf is not related to the existence of an FPU. It is related to the library you use (KEIL MicroLib, standard libs, NewLib from GCC etc).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;At least in the NewLib (so if you use the free LPCXpresso tools) I remember a problem. Please look at the code snippet below for a workaround, plus an alternative. The alternative saves quite some memory, if you use %f then a lot of library stuff gets linked in, maybe just for one printf instruction.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Example:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;========&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;#ifdef EXTMEMORY&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Functions of the newlib require more memory, so use them if we have external SRAM&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; char latbuf[128];&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // String buffer for latitude value of a GPS coordinate&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; char longbuf[128];&amp;nbsp;&amp;nbsp;&amp;nbsp; // String buffer for longitude value of a GPS coordinate&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;#else&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Substitute the newlib functions with a more simple implementation&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; double f1, f2;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; unsigned short d1, d2, n1, n2;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;#endif&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;#ifdef EXTMEMORY&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; // Workaround for a limitation of sprintf in the newlib with %f:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; //&amp;nbsp;&amp;nbsp; Convert the float into a string using&amp;nbsp; char *gcvt(double val, int precision, char *buf)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; //&amp;nbsp;&amp;nbsp; Note: This requires some amount of memory&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; gcvt(Nav_Data.Latitude, 6, latbuf);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Nav_Data.Latitude is a floating-point value&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; gcvt(Nav_Data.Longitude, 6, longbuf);&amp;nbsp;&amp;nbsp; // Nav_Data.Longitude is a floating-point value&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; sprintf(buf, "3D fix: %sN %sE\r\n", latbuf, longbuf);&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; xDebugPutString( buf, strlen(buf) );&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;#else&amp;nbsp; // workaround for the memory consuming library version&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; d1 = Nav_Data.Latitude;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Get the integer part&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; d2 = Nav_Data.Longitude;&amp;nbsp;&amp;nbsp; // Get the integer part&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; f1 = Nav_Data.Latitude - d1;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Get the fractional part&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; f2 = Nav_Data.Longitude - d2;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Get the fractional part&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; n1 = (u16)(f1 * 10000);&amp;nbsp;&amp;nbsp;&amp;nbsp; // Turn into integer with 5 digits&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; n2 = (u16)(f2 * 10000);&amp;nbsp;&amp;nbsp;&amp;nbsp; // Turn into integer with 5 digits&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; // Print as parts, note that you need 0-padding for fractional bit.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; // Since d1 is 678 and d2 is 123, you get "678.0123".&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; sprintf (buf, "3D fix: %d.%04dN %d.%04dE\r\n", d1, n1, d2, n2);&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; xDebugPutString( buf, strlen(buf) );&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;#endif&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Best regards.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 15 Jun 2016 16:56:37 GMT</pubDate>
    <dc:creator>lpcware</dc:creator>
    <dc:date>2016-06-15T16:56:37Z</dc:date>
    <item>
      <title>printf does not print float value correctly</title>
      <link>https://community.nxp.com/t5/LPC-Microcontrollers/printf-does-not-print-float-value-correctly/m-p/525568#M8204</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;STRONG&gt;Content originally posted in LPCWare by jimmcwin on Tue Jun 26 10:08:02 MST 2012&lt;/STRONG&gt;&lt;BR /&gt;&lt;SPAN&gt;I am trying to find out why printf is having problem printing floating data. Is this because of M4 FPU ?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Jim&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 15 Jun 2016 16:56:37 GMT</pubDate>
      <guid>https://community.nxp.com/t5/LPC-Microcontrollers/printf-does-not-print-float-value-correctly/m-p/525568#M8204</guid>
      <dc:creator>lpcware</dc:creator>
      <dc:date>2016-06-15T16:56:37Z</dc:date>
    </item>
    <item>
      <title>Re: printf does not print float value correctly</title>
      <link>https://community.nxp.com/t5/LPC-Microcontrollers/printf-does-not-print-float-value-correctly/m-p/525569#M8205</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;STRONG&gt;Content originally posted in LPCWare by bavarian on Wed Jun 27 00:33:21 MST 2012&lt;/STRONG&gt;&lt;BR /&gt;&lt;SPAN&gt;Hello Jim,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;not that much information about your problem ;-)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;One thing: the behavior of printf is not related to the existence of an FPU. It is related to the library you use (KEIL MicroLib, standard libs, NewLib from GCC etc).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;At least in the NewLib (so if you use the free LPCXpresso tools) I remember a problem. Please look at the code snippet below for a workaround, plus an alternative. The alternative saves quite some memory, if you use %f then a lot of library stuff gets linked in, maybe just for one printf instruction.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Example:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;========&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;#ifdef EXTMEMORY&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Functions of the newlib require more memory, so use them if we have external SRAM&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; char latbuf[128];&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // String buffer for latitude value of a GPS coordinate&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; char longbuf[128];&amp;nbsp;&amp;nbsp;&amp;nbsp; // String buffer for longitude value of a GPS coordinate&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;#else&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Substitute the newlib functions with a more simple implementation&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; double f1, f2;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; unsigned short d1, d2, n1, n2;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;#endif&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;#ifdef EXTMEMORY&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; // Workaround for a limitation of sprintf in the newlib with %f:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; //&amp;nbsp;&amp;nbsp; Convert the float into a string using&amp;nbsp; char *gcvt(double val, int precision, char *buf)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; //&amp;nbsp;&amp;nbsp; Note: This requires some amount of memory&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; gcvt(Nav_Data.Latitude, 6, latbuf);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Nav_Data.Latitude is a floating-point value&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; gcvt(Nav_Data.Longitude, 6, longbuf);&amp;nbsp;&amp;nbsp; // Nav_Data.Longitude is a floating-point value&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; sprintf(buf, "3D fix: %sN %sE\r\n", latbuf, longbuf);&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; xDebugPutString( buf, strlen(buf) );&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;#else&amp;nbsp; // workaround for the memory consuming library version&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; d1 = Nav_Data.Latitude;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Get the integer part&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; d2 = Nav_Data.Longitude;&amp;nbsp;&amp;nbsp; // Get the integer part&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; f1 = Nav_Data.Latitude - d1;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Get the fractional part&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; f2 = Nav_Data.Longitude - d2;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Get the fractional part&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; n1 = (u16)(f1 * 10000);&amp;nbsp;&amp;nbsp;&amp;nbsp; // Turn into integer with 5 digits&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; n2 = (u16)(f2 * 10000);&amp;nbsp;&amp;nbsp;&amp;nbsp; // Turn into integer with 5 digits&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; // Print as parts, note that you need 0-padding for fractional bit.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; // Since d1 is 678 and d2 is 123, you get "678.0123".&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; sprintf (buf, "3D fix: %d.%04dN %d.%04dE\r\n", d1, n1, d2, n2);&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; xDebugPutString( buf, strlen(buf) );&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;#endif&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Best regards.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 15 Jun 2016 16:56:37 GMT</pubDate>
      <guid>https://community.nxp.com/t5/LPC-Microcontrollers/printf-does-not-print-float-value-correctly/m-p/525569#M8205</guid>
      <dc:creator>lpcware</dc:creator>
      <dc:date>2016-06-15T16:56:37Z</dc:date>
    </item>
    <item>
      <title>Re: printf does not print float value correctly</title>
      <link>https://community.nxp.com/t5/LPC-Microcontrollers/printf-does-not-print-float-value-correctly/m-p/525570#M8206</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;STRONG&gt;Content originally posted in LPCWare by jimmcwin on Fri Jun 29 16:35:41 MST 2012&lt;/STRONG&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks. I found the problem actually very simple: The current stack of any function or context within a printf call needed to be aligned by 8.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Jim&lt;/SPAN&gt;&lt;BR /&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 15 Jun 2016 16:56:38 GMT</pubDate>
      <guid>https://community.nxp.com/t5/LPC-Microcontrollers/printf-does-not-print-float-value-correctly/m-p/525570#M8206</guid>
      <dc:creator>lpcware</dc:creator>
      <dc:date>2016-06-15T16:56:38Z</dc:date>
    </item>
  </channel>
</rss>

