public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/49048] New: setprecision rounding fails in some cases
@ 2011-05-18 17:49 s.cecilio at gmail dot com
  2011-05-18 18:26 ` [Bug libstdc++/49048] " paolo.carlini at oracle dot com
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: s.cecilio at gmail dot com @ 2011-05-18 17:49 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49048

           Summary: setprecision rounding fails in some cases
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: major
          Priority: P3
         Component: libstdc++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: s.cecilio@gmail.com


cecilio@pp1:~$ gcc --version
gcc (Ubuntu 4.4.3-4ubuntu5) 4.4.3

I'm having problems with truncation. For instance, following code:

        cout << "test precision 5798.12500: "
                  << fixed << setprecision(2) << setfill(' ')
                  << setw(10) << 5798.12500 << endl;

        cout << "test precision 5798.87500: "
                  << fixed << setprecision(2) << setfill(' ')
                  << setw(10) << 5798.87500 << endl;

Generates inconsistent results: 5798.12 (wrong, should be 5798.13) and 5798.88
(right).
In first case rounds up and in second, down.

The same code, compiled with Microsft Visual Studio, produces the right results
in the same machine.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug libstdc++/49048] setprecision rounding fails in some cases
  2011-05-18 17:49 [Bug libstdc++/49048] New: setprecision rounding fails in some cases s.cecilio at gmail dot com
@ 2011-05-18 18:26 ` paolo.carlini at oracle dot com
  2011-05-18 20:43 ` s.cecilio at gmail dot com
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: paolo.carlini at oracle dot com @ 2011-05-18 18:26 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49048

Paolo Carlini <paolo.carlini at oracle dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID

--- Comment #1 from Paolo Carlini <paolo.carlini at oracle dot com> 2011-05-18 17:55:34 UTC ---
If anything, this is a glibc issue, because we use printf as an implementation
detail. Try 'printf("%.2f", 5798.12500)'


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug libstdc++/49048] setprecision rounding fails in some cases
  2011-05-18 17:49 [Bug libstdc++/49048] New: setprecision rounding fails in some cases s.cecilio at gmail dot com
  2011-05-18 18:26 ` [Bug libstdc++/49048] " paolo.carlini at oracle dot com
@ 2011-05-18 20:43 ` s.cecilio at gmail dot com
  2011-05-18 21:09 ` paolo.carlini at oracle dot com
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: s.cecilio at gmail dot com @ 2011-05-18 20:43 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49048

--- Comment #2 from Cecilio <s.cecilio at gmail dot com> 2011-05-18 20:23:23 UTC ---
printf output is 5798.12. I think this is the expected output. Does this
confirms your guess? (sorry, I don't understand your previous answer.  My
knowledge about GCC is just the name "GCC". I know nothing about gcc and its
internals. I can only report that there is a bug somewhere in the gcc compiler. 

What is the next step I should do to help to solve this? Have I to report the
bug in another place? Please, where?

Thanks,
Cecilio


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug libstdc++/49048] setprecision rounding fails in some cases
  2011-05-18 17:49 [Bug libstdc++/49048] New: setprecision rounding fails in some cases s.cecilio at gmail dot com
  2011-05-18 18:26 ` [Bug libstdc++/49048] " paolo.carlini at oracle dot com
  2011-05-18 20:43 ` s.cecilio at gmail dot com
@ 2011-05-18 21:09 ` paolo.carlini at oracle dot com
  2011-05-19  6:22 ` s.cecilio at gmail dot com
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: paolo.carlini at oracle dot com @ 2011-05-18 21:09 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49048

--- Comment #3 from Paolo Carlini <paolo.carlini at oracle dot com> 2011-05-18 20:47:19 UTC ---
In the ISO C++ Standard which we are implementing, the semantics of
setprecision and numeric output in general, is specified in terms of printf
behavior. In this specific implementation, in fact, printf is actually,
literally, used. Thus, if you have reasons to believe that 5798.12 is incorrect
you should report the issue to the glibc maintainers, not the libstdc++
maintainers, because printf on Linux systems is part of glibc.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug libstdc++/49048] setprecision rounding fails in some cases
  2011-05-18 17:49 [Bug libstdc++/49048] New: setprecision rounding fails in some cases s.cecilio at gmail dot com
                   ` (2 preceding siblings ...)
  2011-05-18 21:09 ` paolo.carlini at oracle dot com
@ 2011-05-19  6:22 ` s.cecilio at gmail dot com
  2011-05-19  6:51 ` jakub at gcc dot gnu.org
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: s.cecilio at gmail dot com @ 2011-05-19  6:22 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49048

--- Comment #4 from Cecilio <s.cecilio at gmail dot com> 2011-05-19 05:39:43 UTC ---
Ok Thank you, I will report to glbc. But, to try to learn and understand
something, could you please answer a question for me?:

My understanding is that printf truncates, so 
'printf("%.2f", 5798.12500) --> 5798.12
'printf("%.2f", 5798.87500) --> 5798.87

and 'cout << setprecision(2)' rounds, so

cout << fixed << setprecision(2) << 5798.12500 --> 5798.13
cout << fixed << setprecision(2) << 5798.87500 --> 5798.88

If, internally, the implementation of 'cout << setprecision' uses printf,
somehow setprecision has to take care of rounding, as print will not.

what gcc library/component is doing the rounding? Perhaps I should report there
instead of printf.

Thank for any info on this.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug libstdc++/49048] setprecision rounding fails in some cases
  2011-05-18 17:49 [Bug libstdc++/49048] New: setprecision rounding fails in some cases s.cecilio at gmail dot com
                   ` (3 preceding siblings ...)
  2011-05-19  6:22 ` s.cecilio at gmail dot com
@ 2011-05-19  6:51 ` jakub at gcc dot gnu.org
  2011-05-19  7:53 ` s.cecilio at gmail dot com
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: jakub at gcc dot gnu.org @ 2011-05-19  6:51 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49048

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org

--- Comment #5 from Jakub Jelinek <jakub at gcc dot gnu.org> 2011-05-19 06:08:50 UTC ---
printf in glibc rounds, I think you should read something about rounding.
The default rounding mode (and the only one actually implemented for printf) is
round to even.  http://www.lmgtfy.com/?q=round+to+even
So
#include <stdio.h>
int
main (void)
{
  printf ("%.2f\n", 5798.12500);
  printf ("%.2f\n", 5798.87500);
  return 0;
}
printing
5798.12
5798.88
is correct.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug libstdc++/49048] setprecision rounding fails in some cases
  2011-05-18 17:49 [Bug libstdc++/49048] New: setprecision rounding fails in some cases s.cecilio at gmail dot com
                   ` (4 preceding siblings ...)
  2011-05-19  6:51 ` jakub at gcc dot gnu.org
@ 2011-05-19  7:53 ` s.cecilio at gmail dot com
  2011-05-19  9:00 ` paolo.carlini at oracle dot com
  2011-05-19 17:22 ` s.cecilio at gmail dot com
  7 siblings, 0 replies; 9+ messages in thread
From: s.cecilio at gmail dot com @ 2011-05-19  7:53 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49048

--- Comment #6 from Cecilio <s.cecilio at gmail dot com> 2011-05-19 07:12:25 UTC ---
Thanks for the link. Funny website but a great article at wikipedia. Thanks
indeed. Now I understand the issue, so there is no bug anywere! I always
understood 'rounding' as 'rounding up'.

Thanks for your help and time.
Regards, 
Cecilio


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug libstdc++/49048] setprecision rounding fails in some cases
  2011-05-18 17:49 [Bug libstdc++/49048] New: setprecision rounding fails in some cases s.cecilio at gmail dot com
                   ` (5 preceding siblings ...)
  2011-05-19  7:53 ` s.cecilio at gmail dot com
@ 2011-05-19  9:00 ` paolo.carlini at oracle dot com
  2011-05-19 17:22 ` s.cecilio at gmail dot com
  7 siblings, 0 replies; 9+ messages in thread
From: paolo.carlini at oracle dot com @ 2011-05-19  9:00 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49048

--- Comment #7 from Paolo Carlini <paolo.carlini at oracle dot com> 2011-05-19 08:42:45 UTC ---
Glad that the issue is clear now. By the way, Cecilio, apparently in your
Comment #4 you didn't get my point about the C++ library vs printf: the ISO C++
Standard specifies the behavior of setprecision, precision, and so on, **in
terms of printf**, with no intermediate rounding, truncation, or whatelse:
really everything must go "as-if" the number were feed directly to a printf
like the one written down by myself and Jakub. Thus, really, if anything, this
PR could be *only* a glibc issue. As a matter of fact, isn't an issue at all.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug libstdc++/49048] setprecision rounding fails in some cases
  2011-05-18 17:49 [Bug libstdc++/49048] New: setprecision rounding fails in some cases s.cecilio at gmail dot com
                   ` (6 preceding siblings ...)
  2011-05-19  9:00 ` paolo.carlini at oracle dot com
@ 2011-05-19 17:22 ` s.cecilio at gmail dot com
  7 siblings, 0 replies; 9+ messages in thread
From: s.cecilio at gmail dot com @ 2011-05-19 17:22 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49048

--- Comment #8 from Cecilio <s.cecilio at gmail dot com> 2011-05-19 17:01:15 UTC ---
Thanks again, Paolo and Jackub, for your help and explanations. Believe it or
not, I a have a PhD and had to learn a lot of complex mathematics, but I never
heard about so many types of 'rounding' methods. To me, rounding had always
meant round-half-up (banker's rounding): 5.5 rounds up to 6 and 6.5 rounds up
to 7. As this is also the normally required method for financial calculations,
I can not understand the usefulness of printf sometimes rounding up and others
rounding down, as it is the behaviour of round-half-even method. But once I've
learn that (thanks for this) I know that I can no longer rely on setprecision
or printf, and that I have to implement my rounding methods to get the banker's
rounding behaviour. 

I'm wondering how many financial programs are there relying on setprecision
without having noticed its 'problematic' rounding behaviour, caused by printf
and blessed by ISO c++ standard. ;-)

It's been a pleasure taking to both and learning with your help. 
With my best regards,
Cecilio


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2011-05-19 17:21 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-05-18 17:49 [Bug libstdc++/49048] New: setprecision rounding fails in some cases s.cecilio at gmail dot com
2011-05-18 18:26 ` [Bug libstdc++/49048] " paolo.carlini at oracle dot com
2011-05-18 20:43 ` s.cecilio at gmail dot com
2011-05-18 21:09 ` paolo.carlini at oracle dot com
2011-05-19  6:22 ` s.cecilio at gmail dot com
2011-05-19  6:51 ` jakub at gcc dot gnu.org
2011-05-19  7:53 ` s.cecilio at gmail dot com
2011-05-19  9:00 ` paolo.carlini at oracle dot com
2011-05-19 17:22 ` s.cecilio at gmail dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).