Rounding floats after modf() - Software Engineering Stack Exchange most recent 30 from softwareengineering.stackexchange.com 2019-09-15T23:34:28Z https://softwareengineering.stackexchange.com/feeds/question/343481 https://creativecommons.org/licenses/by-sa/4.0/rdf https://softwareengineering.stackexchange.com/q/343481 3 Rounding floats after modf() user264480 https://softwareengineering.stackexchange.com/users/264480 2017-03-04T18:15:31Z 2017-03-04T18:15:31Z <p>I am trying to make an effective modf() function that uses floats instead of doubles. An embedded application I am working on only supports the float type.</p> <p>My modf() function works as expected with the exception of float rounding errors.</p> <p>Consider the float number 12345.67. The current version of my modf() function simply subtracts the integer part from the input to get the fraction part. What I expected was 12345.67 - 12345. = 0.67 , but it doesn't work right.</p> <p>The original mantissa for 12345.67 is this:<br> 100 0000 1110 01.10 1010 1110 &lt;- 0x40E6AE decimal point = 13</p> <p>Separated into integer and fraction components is this:<br> 100 0000 1110 01.00 0000 0000 &lt;-- Integer 0x40E600<br> 000 0000 0000 00.10 1010 1110 &lt;-- Fraction </p> <p>My modf() function normalizes the fraction to:<br> 010 1011 1000 0000 0000 0000 &lt;-- 0x2B8000</p> <p>Because of the influx of 13 zeros from the right, the number is not 0.67 as expected, but instead, something like 0.6600178.</p> <p>I need to fix the rounding error so that it is closer to 0.67. The binary version of 0.67 is this:<br> 010 1011 1000 0101 0001 1111 &lt;-- 0x2B851F<br> Notice that the last 3 nibbles have data now. The first half is the same.</p> <p>If I change the 12th bit from the right to a 1, I get something like 0.67002<br> 010 1011 1000 1000 0000 0000 &lt;-- 0x2B8800</p> <p>This is good enough for my needs, but if I did that when the input float was actually 0.67 to start with, I would have the wrong value.</p> <p>So I guess the question is What is the proper way to apply bias to a float in this situation?</p>