• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 475
  • Last Modified:

sprintf precision bug?

Using MSDEV 6.0 environment:

Consider the following:

sprintf( Buf,"%0.1f",0.85 ) ;
sprintf( Buf,"%0.1f",0.75 ) ;

In both cases, Buf is set to: "0.8".
Is this a bug? Can anyone detect the origination of this bug?
1 Solution
gil_moAuthor Commented:
Edited text of question.
This is not necessarily a bug. Floating point representation in binary is not always accurate. Floating point numbers are represented by what is misleadingly called "binary decimal point", which works in the same way as a normal decimal point, except it is in base 2. Thus, 0.75 will be represented in binary as 0.11 (a half plus a quarter), just as 0.11 in decimal is a tenth plus a hundredth. Thus, 0.75 can be accurately represented by a float. On the other hand, no finite set of binary digits will represent 0.85, so that it ends up being represented only approximately. (This is the case with most fractions.) The internal representation of a number like 0.85 is apparently rounded slightly down (i.e., the bits are truncated from some point down), so that the actual value stored in memory is a little bit less than 0.85. (Probably about 0.8499995 if you are using float rather than double.) Thus, when the computer looks for one decimal digit to represent this fraction, it finds that 0.8 is slightly closer than 0.9.

In general, because of the aobve, one hsould not expect floating-point numbers to have this sort of hairline precision. For instance, it is very bad practice to check whether one float is equal to another; you should always check whether it is within 0.0001 of the other number, because two floats are rarely exactly equal, even if theoretically their values should be identical.

-- Slarti
Even if the values were exact, rounding to even is the preferred mode for decimal numbers anyway, so you just happened to get the right answer depite the inaccuracy.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Become an IT Security Management Expert

In today’s fast-paced, digitally transformed world of business, the need to protect network data and ensure cloud privacy has never been greater. With a B.S. in Network Operations and Security, you can get the credentials it takes to become an IT security management expert.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now