• C

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?
gil_moAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gil_moAuthor Commented:
Edited text of question.
0
SlartiCommented:
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
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ozoCommented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.