<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.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/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>convert c_float to ieee_11073_16bit_float</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/48707/convert-c_float-to-ieee_11073_16bit_float</link><description>Hello, 
 I need to convert a standard 32 bit floating point number to IEEE-11073 16-bit FLOAT with 12-bit mantissa and 4-bit exponent (source: https://infocenter.nordicsemi.com ). Is there already an implementation in the sdk or an example somewhere?</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 19 Jun 2019 13:25:49 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/48707/convert-c_float-to-ieee_11073_16bit_float" /><item><title>RE: convert c_float to ieee_11073_16bit_float</title><link>https://devzone.nordicsemi.com/thread/193695?ContentTypeID=1</link><pubDate>Wed, 19 Jun 2019 13:25:49 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:066ba0d2-3c95-40b2-b65b-d0e47aac1aec</guid><dc:creator>haakonsh</dc:creator><description>&lt;p&gt;&lt;span&gt;Given that the largest mantissa is 2^11 - 1, and the largest exponent is 7,&lt;br /&gt;&lt;/span&gt;the largest number you can represent will be (2^11 - 1) * 10^7 = &lt;span style="text-decoration:underline;"&gt;2.047 * 10^10&lt;/span&gt;.&amp;nbsp;&lt;br /&gt;&lt;br /&gt;The precision of the mantissa will cause significant errors with very large or very small values, ie&lt;br /&gt;the number 123456789: exp = 5, mantissa = 1235 --&amp;gt; 0.1235 * 10^5 = &lt;span style="text-decoration:underline;"&gt;123500000&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;or 0.000001234567: exp = -5, mantissa = 1235 --&amp;gt; 0.1235 * 10^-5 =&amp;nbsp;&amp;nbsp;&lt;span style="text-decoration:underline;"&gt;0.000001235&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;This is from my limited understanding, it might be wrong.&amp;nbsp;&lt;br /&gt;&lt;br /&gt;There are also at least 5 cases that need to be handled differently:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;input == zero.&lt;/li&gt;
&lt;li&gt;input &amp;gt;= 1.0&lt;/li&gt;
&lt;li&gt;input &amp;lt; 1.0&lt;/li&gt;
&lt;li&gt;input &amp;gt;= -1.0&lt;/li&gt;
&lt;li&gt;input &amp;lt; -1.0&lt;/li&gt;
&lt;/ol&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: convert c_float to ieee_11073_16bit_float</title><link>https://devzone.nordicsemi.com/thread/193661?ContentTypeID=1</link><pubDate>Wed, 19 Jun 2019 12:16:11 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4f651b5b-469c-48ad-b229-455b2e393b31</guid><dc:creator>alex_u</dc:creator><description>&lt;p&gt;Your right, thanks. I added an -INF and +INF limitation. For convinience, I set the max values a little bit less than the real limits of the 16 bit float. This should solve the issue.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;pre class="ui-code" data-mode="c_cpp"&gt;ieee_float16_t c_float32_to_ieee_11073_float16(float input)
{
	ieee_float16_t ret;	
	if(input != input) //check for NAN
	{
		ret.exponent = 0;
		ret.mantissa = 0x07ff;
		return ret;
	}
	else if(input &amp;gt; (float)2E10) //check for +INF
	{
		ret.exponent = 0;
		ret.mantissa = 0x07FE;
		return ret;
	}
	else if(input &amp;lt; (float)-2E10) //check for -INF
	{
		ret.exponent = 0;
		ret.mantissa = 0x0802;
		return ret;
	}
	else
	{
		ret.exponent = 0;
		while(input &amp;gt; (float)0x7ff){
			input /= 10;
			ret.exponent += 1;
		}
		while(input*10 &amp;lt; (float)0x7ff){
			input *= 10;
			ret.exponent -= 1;
		}
		ret.mantissa = ((int16_t)input) &amp;amp; 0xfff;
		ret.exponent &amp;amp;= 0xf;
		return ret;
	}
}&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: convert c_float to ieee_11073_16bit_float</title><link>https://devzone.nordicsemi.com/thread/193622?ContentTypeID=1</link><pubDate>Wed, 19 Jun 2019 11:11:41 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2fb037d9-e4bc-4965-8e27-d72cf8ff4679</guid><dc:creator>haakonsh</dc:creator><description>&lt;p&gt;That&amp;#39;s not gonna work, for larger numbers the mantissa gets truncated.&amp;nbsp;&lt;br /&gt;You need an algorithm that always returns a float with 12-bit precision.&amp;nbsp;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: convert c_float to ieee_11073_16bit_float</title><link>https://devzone.nordicsemi.com/thread/193485?ContentTypeID=1</link><pubDate>Tue, 18 Jun 2019 15:37:57 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5770c0a3-8bd2-4db1-82e7-79296bdc0307</guid><dc:creator>alex_u</dc:creator><description>&lt;p&gt;I managed to get it done.&lt;/p&gt;
&lt;p&gt;Here is the final code:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;ieee_float16_t c_float32_to_ieee_11073_float16(float input)
{
	ieee_float16_t ret;	
	ret.exponent = 0;
	while(input &amp;gt; (float)0x7ff){
		input /= 10;
		ret.exponent += 1;
	}
	while(input*10 &amp;lt; (float)0x7ff){
		input *= 10;
		ret.exponent -= 1;
	}
	ret.mantissa = (int16_t)input;
	return ret;
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Thanks for your help.&lt;/p&gt;
&lt;p&gt;Alex&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: convert c_float to ieee_11073_16bit_float</title><link>https://devzone.nordicsemi.com/thread/193469?ContentTypeID=1</link><pubDate>Tue, 18 Jun 2019 14:25:54 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3bd33af0-73f3-47b1-aff3-49e5a131f010</guid><dc:creator>alex_u</dc:creator><description>&lt;p&gt;I currently have no access to the specification. But I want to use your SDK to transmit bloodpressure measurments. It seems like this is only possible by using the 16 bit float defined in &lt;span&gt;IEEE-11073. Or is there another solution?&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: convert c_float to ieee_11073_16bit_float</title><link>https://devzone.nordicsemi.com/thread/193450?ContentTypeID=1</link><pubDate>Tue, 18 Jun 2019 13:23:43 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:cbd9b287-fe3f-48b2-947e-cf8197495e5b</guid><dc:creator>haakonsh</dc:creator><description>&lt;p&gt;I have zero experience with&amp;nbsp;&lt;span&gt;IEEE-11073, I suggest you get a copy of the specification if you intend to use it.&amp;nbsp;&lt;br /&gt;&lt;br /&gt;What the SDK devs have done is this:&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;/**@brief SFLOAT format (IEEE-11073 16-bit FLOAT, 
defined as a 16-bit vlue with 12-bit mantissa and 4-bit exponent. */
typedef struct
{
  int8_t  exponent;                                /**&amp;lt; Base 10 exponent, only 4 bits */
  int16_t mantissa;                                /**&amp;lt; Mantissa, only 12 bits */
} ieee_float16_t;



uint16_t encoded_sfloat;
ieee_float16_t sfloat;

// Taken from the Blood Pressure Service example:
encoded_sfloat = ((sfloat.exponent &amp;lt;&amp;lt; 12) &amp;amp; 0xF000) |
((sfloat.mantissa &amp;lt;&amp;lt; 0) &amp;amp; 0x0FFF);

//Now you can encode an exponent + mantissa into a uin16_t

&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: convert c_float to ieee_11073_16bit_float</title><link>https://devzone.nordicsemi.com/thread/193438?ContentTypeID=1</link><pubDate>Tue, 18 Jun 2019 12:55:16 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3c3ba941-8c21-4a4e-a6d3-a749f1c02fd6</guid><dc:creator>alex_u</dc:creator><description>&lt;p&gt;the arm_math.h and the post your referenced are using 32 bit floating numbers. I already checked those. I am also confused about the 12 bit mantissa and 4 bit exponent format. The standard half precision floating point number seems to be 5 bit exponent and 11 bit mantissa (source: &lt;a href="https://en.wikipedia.org/wiki/Half-precision_floating-point_format"&gt;wikipedia&lt;/a&gt;). Could you give me a more detailed description of the IEEE-11073 16-bit FLOAT format? I cant find anything about exponent encoding and INF and NAN definition.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: convert c_float to ieee_11073_16bit_float</title><link>https://devzone.nordicsemi.com/thread/193432?ContentTypeID=1</link><pubDate>Tue, 18 Jun 2019 12:36:54 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:fad5965c-bd9e-4b56-8bf8-4c8b69a810b1</guid><dc:creator>haakonsh</dc:creator><description>&lt;p&gt;See similar post&amp;nbsp;&lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/28727/32-bit-ieee-11073-float-data-type-parser"&gt;https://devzone.nordicsemi.com/f/nordic-q-a/28727/32-bit-ieee-11073-float-data-type-parser&lt;/a&gt;&amp;nbsp;&lt;br /&gt;&lt;br /&gt;Otherwise, I&amp;#39;d check the ARM CMSIS DSP library, arm_math.h,&amp;nbsp;&lt;a href="https://github.com/ARM-software/CMSIS/blob/master/CMSIS/Include/arm_math.h"&gt;https://github.com/ARM-software/CMSIS/blob/master/CMSIS/Include/arm_math.h&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>