This is the mail archive of the glibc-bugs@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug libc/4943] Inconsistent rounding behaviour for sprintf and IEEE doubles


------- Additional Comments From madcoder at debian dot org  2007-10-03 10:37 -------
Subject: article "A Question Of Rounding" in issue #143 (Was:  Inconsistent rounding behaviour for sprintf and IEEE doubles)

  Hi,

  I would like to report that the article "A Question Of Rounding" in
your issue #143 is completely misleading, because its author doesn't
understand how floating point works. Hence you published an article that
is particularly wrong.

  The author doesn't understand that a rounding occurs when you store a
double value. Hence statements like:

] When converting a value 3.5 (stored at a precison of 15) to text,

  or

] when converting the value 2.5, sprintf() produces not the expected
] value 3, but the value 2!

  May confuse any reader that isn't IEEE754 aware. What happens for real
is that when you store any value it is replaced with the nearest
representable IEEE754 number, hence not necessarily the number which was
written by the programmer. E.g. the example he gives in his totally
bogus "late" addendum about 5000.525, actually stores:
5000.52499999999964 in the double value. And all the program then only
works with 5000.52499999999964 (and not "5000.525 with a 15 digit
precision" like he states in the glibc bug report. His mental
representation of IEEE754 numbers isn't accurate and does not fit how it
works).


  What he also doesn't understands is that when the double value is an
exact IEEE754 value, for example 2.5 or 3.5 are really 2.5 or 3.5 for
IEEE754, then if you ask for a rounding of a value that is _just_ in the
middle of a given range, IEEE754 use round-to-even method, to dispatch
errors in the both directions[0].

  And that's why when you ask for:

    printf("%g:%.2g %g:%.2g\n", 0.12, 0.125, 0.225, 0.225);

  it yields:

    0.125:0.12 0.225:0.23

  This is totally correct and expected. What the author doesn't
understand is that when you write monetary/financial/... software,
IEEE754 are not to be used, because it _will_ generates this kind of
issues, and that fixed point with accurate rounding behaviour has to be
used instead. Floating point is not good enough for those problems,
because when you mean 5000.5250USD you definitely don't want your
program use 5000.52499999999964 instead "because it's near enough".


  I'm sorry, but this article is very wrong, and give false informations
on a matter that isn't very well understood by many programmers, hence I
beg you to remove this article, for the sake of the teacher that already
have to fight against enough preconceived ideas about ieee 754 numbers
already.

best regards,


  [0] http://en.wikipedia.org/wiki/Rounding#Round-to-even_method


-- 


http://sourceware.org/bugzilla/show_bug.cgi?id=4943

------- You are receiving this mail because: -------
You are on the CC list for the bug, or are watching someone who is.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]