{"id":3576,"date":"2012-08-28T15:50:53","date_gmt":"2012-08-28T14:50:53","guid":{"rendered":"https:\/\/blogs.mentor.com\/colinwalls\/?p=3576"},"modified":"2012-08-28T15:50:53","modified_gmt":"2012-08-28T14:50:53","slug":"floating-point","status":"publish","type":"post","link":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/2012\/08\/28\/floating-point\/","title":{"rendered":"Floating point"},"content":{"rendered":"<p>Nowadays, most embedded systems are built using 32-bit CPUs. These devices give plenty of scope for performing the arithmetical processing required for various applications. Calculations can be performed on signed or unsigned integers and 32 bits gives a good range of values: +\/- 2 billion or up to 4 billion respectively. Extending to 64 bits is reasonably straightforward.<\/p>\n<p>If you need to stray outside of these ranges of values or perform more sophisticated operations, then you need to think in terms of floating point and this presents a range of new challenges &#8230;<!--more--><\/p>\n<p>The concept of a floating point number is simple enough &#8211; the value is stored as two integers: the mantissa and the exponent. The number represented is the mantissa multiplies by 2 to the power of the exponent. Typically, these two integers are stored in bit fields in a 32-bit word, but higher precision variants are also available. The most common format is <a href=\"http:\/\/en.wikipedia.org\/wiki\/IEEE_754-1985\" target=\"_blank\" rel=\"noopener noreferrer\">IEEE 754-1985<\/a>.<\/p>\n<p>The clear benefit of using floating point is the wide range of values that may be represented, but this comes at a cost:<\/p>\n<p><strong>Performance<\/strong>. Floating point operations take a lot of time compared with integers. If the processing is done in software, the execution time can be very long indeed. Hardware floating point units speed up operations to a reasonable extent.<\/p>\n<p><strong>Precision<\/strong>. Because of the way that values are represented in floating point, a value may not be exactly what you expect. For example, you may anticipate a variable having the value 5.0, but it actually is 4.999999 This need not be a problem, but care is needed in coding with floating point.<\/p>\n<p>Obviously, code like this would be foolish:<\/p>\n<pre>if (x == 3.0)<\/pre>\n<pre>...<\/pre>\n<pre><\/pre>\n<p>as <strong>x<\/strong> may never be precisely 3.0<\/p>\n<p>Similarly, coding a loop like this might produce unexpected results:<\/p>\n<pre>for (x=0.0; x&lt;5.0; x++)<\/pre>\n<pre>...<\/pre>\n<pre><\/pre>\n<p>You would expect the loop to be performed 5 times for <strong>x<\/strong> values 0.0, 1.0. 2.0, 3.0 and 4.0 This might work, but it is quite possible that an extra iteration will occur for <strong>x<\/strong> being 4.999999<\/p>\n<p>The solution is to use an integer loop counter:<\/p>\n<pre>for (i=0,x=0.0; i&lt;5; i++,x++)<\/pre>\n<pre>...<\/pre>\n<pre><\/pre>\n<p>Broadly speaking, floating point should only be used if it is essential and only after every creative way to do the calculations using integers has been investigated and eliminated.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Nowadays, most embedded systems are built using 32-bit CPUs. These devices give plenty of scope for performing the arithmetical processing&#8230;<\/p>\n","protected":false},"author":71677,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spanish_translation":"","french_translation":"","german_translation":"","italian_translation":"","polish_translation":"","japanese_translation":"","chinese_translation":"","footnotes":""},"categories":[1],"tags":[300,484],"industry":[],"product":[],"coauthors":[],"class_list":["post-3576","post","type-post","status-publish","format-standard","hentry","category-news","tag-embedded-software","tag-floating-point"],"_links":{"self":[{"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/3576","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/users\/71677"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/comments?post=3576"}],"version-history":[{"count":0,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/3576\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/media?parent=3576"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/categories?post=3576"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/tags?post=3576"},{"taxonomy":"industry","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/industry?post=3576"},{"taxonomy":"product","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/product?post=3576"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/coauthors?post=3576"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}