Rounding double

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David Harmon

    #16
    Re: Rounding double

    On Thu, 22 Nov 2007 09:12:02 +0000 in comp.lang.c++, Richard
    Heathfield <rjh@see.sig.in validwrote,
    >Right - but of course a cross-posted article should "work" in all the
    >groups into which it's posted.
    But saying that "double &value" is illegal doesn't "work" in c.l.c++

    Comment

    • jacob navia

      #17
      Re: Rounding double

      James Kuyper wrote:
      jacob navia wrote:
      >Richard Heathfield wrote:
      >>jacob navia said:
      >>>
      >><snip>
      >>>
      >>>#include <stdio.h>
      >>>#include <math.h>
      >>>>
      >>>void RoundMyDouble (long double *value, short numberOfPrecisi ons)
      >>>>
      >>>{
      >>> long long p = powl(10.0L, numberOfPrecisi ons);
      >>> *value = (long long)(*value * p + 0.5L) / (double)p;
      >>>}
      >>>>
      >>>int main(void)
      >>>{
      >>> long double v = 3.1415926535897 932384626L;
      >>> short int ndp = 0;
      >>> while(ndp < 20)
      >>> {
      >>> long double newv = v;
      >>> RoundMyDouble(& newv, ndp);
      >>> printf("%.21Lf, \"rounded\" to %hd decimals, is %.21Lf\n",
      >>> v,
      >>> ndp++,
      >>> newv);
      >>> }
      >>>>
      >>> return 0;
      >>>}
      >>>>
      >>>You will need a modern C compiler to use this (C99 standard).
      >>>Using lcc-win I get:
      >>>>
      >><snip>
      >>>
      >>>>3.141592653 5897932390: 19 decimals 1.0000000000000 000000
      >>>
      >>Do you think that's a compiler bug, or a programmer bug?
      >>>
      >>
      >output of gcc:
      >3.141592653589 7932385: 0 decimals 3.0000000000000 000000
      >[snip]
      >3.141592653589 7932385: 17 decimals 3.1415926535897 932400
      >3.141592653589 7932385: 18 decimals 3.1415926535897 932389
      >3.141592653589 7932385: 19 decimals 1.0000000000000 000000
      >>
      >If it is a compiler bug, it is a shared compiler bug.
      >>
      >It isn't a programmer bug either. Long long has a finite
      >number of decimal digits as you may know.
      >
      Which makes use of long long a programming error.
      Why?
      I get 18 digits, and this is more than enough for double precision
      that gives 16 digits at best!
      You should have used
      one of the rounding functions like floorl(), nearbyintl(), rintl(), or
      roundl(), instead of conversion to long long. Even with that fix, the
      algorithm you choose needlessly results in overflow (or underflow, for
      negative values of numberOfPrecisi ons) in some cases.
      >
      I did not choose any algorithm. It was proposed by somebody else,
      and I merely adapted it to give enough decimal digits for
      double precision. Note that I did NOT change the cast in the
      denominator of the division from double to long double.

      I say that it's needless (and therefore a programming error) because you
      can avoid both problems by using sprintf() and sscanf(). There are more
      efficient ways of doing it, but that one is sufficient.
      And this one is sufficient for double precision too.


      --
      jacob navia
      jacob at jacob point remcomp point fr
      logiciels/informatique
      http://www.cs.virginia.edu/~lcc-win32

      Comment

      • James Kuyper

        #18
        Re: Rounding double

        jacob navia wrote:
        James Kuyper wrote:
        ....
        >Which makes use of long long a programming error.
        >
        Why?
        I get 18 digits, and this is more than enough for double precision
        that gives 16 digits at best!
        That assertion depends upons unportable (and more importantly,
        unnecessary) assumptions about the width of long long and the precision
        of long double.

        Comment

        • jacob navia

          #19
          Re: Rounding double

          James Kuyper wrote:
          jacob navia wrote:
          >James Kuyper wrote:
          ...
          >>Which makes use of long long a programming error.
          >>
          >Why?
          >I get 18 digits, and this is more than enough for double precision
          >that gives 16 digits at best!
          >
          That assertion depends upons unportable (and more importantly,
          unnecessary) assumptions about the width of long long and the precision
          of long double.
          Probably. Your solution is always the best since you did not
          propose any

          :-)


          --
          jacob navia
          jacob at jacob point remcomp point fr
          logiciels/informatique
          http://www.cs.virginia.edu/~lcc-win32

          Comment

          • Marco Manfredini

            #20
            Re: Rounding double

            jacob navia wrote:
            James Kuyper wrote:
            >jacob navia wrote:
            >>If it is a compiler bug, it is a shared compiler bug.
            >>>
            >>It isn't a programmer bug either. Long long has a finite
            >>number of decimal digits as you may know.
            >>
            >Which makes use of long long a programming error.
            >
            Why?
            I get 18 digits, and this is more than enough for double precision
            that gives 16 digits at best!
            I'm curious. What does your machine print for 6.0221415e23 ?

            Comment

            • jacob navia

              #21
              Re: Rounding double

              Marco Manfredini wrote:
              jacob navia wrote:
              >James Kuyper wrote:
              >>jacob navia wrote:
              >
              >>>If it is a compiler bug, it is a shared compiler bug.
              >>>>
              >>>It isn't a programmer bug either. Long long has a finite
              >>>number of decimal digits as you may know.
              >>>
              >>Which makes use of long long a programming error.
              >>
              >Why?
              >I get 18 digits, and this is more than enough for double precision
              >that gives 16 digits at best!
              >
              I'm curious. What does your machine print for 6.0221415e23 ?
              The same as 6.0221415e23+0. 5

              Interesting no?


              --
              jacob navia
              jacob at jacob point remcomp point fr
              logiciels/informatique
              http://www.cs.virginia.edu/~lcc-win32

              Comment

              • Marco Manfredini

                #22
                Re: Rounding double

                jacob navia wrote:
                Marco Manfredini wrote:
                >jacob navia wrote:
                >>James Kuyper wrote:
                >>>jacob navia wrote:
                >>
                >>>>If it is a compiler bug, it is a shared compiler bug.
                >>>>>
                >>>>It isn't a programmer bug either. Long long has a finite
                >>>>number of decimal digits as you may know.
                >>>>
                >>>Which makes use of long long a programming error.
                >>>
                >>Why?
                >>I get 18 digits, and this is more than enough for double precision
                >>that gives 16 digits at best!
                >>
                >I'm curious. What does your machine print for 6.0221415e23 ?
                >
                The same as 6.0221415e23+0. 5
                >
                Interesting no?
                I mean when it comes out of RoundMyDouble

                Comment

                • Richard Heathfield

                  #23
                  Re: Rounding double

                  David Harmon said:
                  On Thu, 22 Nov 2007 09:12:02 +0000 in comp.lang.c++, Richard
                  Heathfield <rjh@see.sig.in validwrote,
                  >>Right - but of course a cross-posted article should "work" in all the
                  >>groups into which it's posted.
                  >
                  But saying that "double &value" is illegal doesn't "work" in c.l.c++
                  Touche'. :-) Nevertheless, agreeing that it's legal doesn't work in clc.

                  --
                  Richard Heathfield <http://www.cpax.org.uk >
                  Email: -http://www. +rjh@
                  Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                  "Usenet is a strange place" - dmr 29 July 1999

                  Comment

                  • jacob navia

                    #24
                    Re: Rounding double

                    Richard Heathfield wrote:
                    David Harmon said:
                    >
                    >On Thu, 22 Nov 2007 09:12:02 +0000 in comp.lang.c++, Richard
                    >Heathfield <rjh@see.sig.in validwrote,
                    >>Right - but of course a cross-posted article should "work" in all the
                    >>groups into which it's posted.
                    >But saying that "double &value" is illegal doesn't "work" in c.l.c++
                    >
                    Touche'. :-) Nevertheless, agreeing that it's legal doesn't work in clc.
                    >
                    Who cares?

                    That works in lcc-win.

                    Lcc-win gives you the best of C and C++

                    --
                    jacob navia
                    jacob at jacob point remcomp point fr
                    logiciels/informatique
                    http://www.cs.virginia.edu/~lcc-win32

                    Comment

                    • Richard Heathfield

                      #25
                      Re: Rounding double

                      jacob navia said:
                      Richard Heathfield wrote:
                      >David Harmon said:
                      >>
                      >>On Thu, 22 Nov 2007 09:12:02 +0000 in comp.lang.c++, Richard
                      >>Heathfield <rjh@see.sig.in validwrote,
                      >>>Right - but of course a cross-posted article should "work" in all the
                      >>>groups into which it's posted.
                      >>But saying that "double &value" is illegal doesn't "work" in c.l.c++
                      >>
                      >Touche'. :-) Nevertheless, agreeing that it's legal doesn't work in clc.
                      >>
                      >
                      Who cares?
                      People who don't wish to mislead to OP by giving incorrect advice.

                      <spam snipped>

                      --
                      Richard Heathfield <http://www.cpax.org.uk >
                      Email: -http://www. +rjh@
                      Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                      "Usenet is a strange place" - dmr 29 July 1999

                      Comment

                      • jacob navia

                        #26
                        Re: Rounding double

                        Richard Heathfield wrote:
                        jacob navia said:
                        >
                        >Richard Heathfield wrote:
                        >>David Harmon said:
                        >>>
                        >>>On Thu, 22 Nov 2007 09:12:02 +0000 in comp.lang.c++, Richard
                        >>>Heathfield <rjh@see.sig.in validwrote,
                        >>>>Right - but of course a cross-posted article should "work" in all the
                        >>>>groups into which it's posted.
                        >>>But saying that "double &value" is illegal doesn't "work" in c.l.c++
                        >>Touche'. :-) Nevertheless, agreeing that it's legal doesn't work in clc.
                        >>>
                        >Who cares?
                        >
                        People who don't wish to mislead to OP by giving incorrect advice.
                        >
                        <spam snipped>
                        >
                        Incorrect advice happens only when they read your posts.



                        --
                        jacob navia
                        jacob at jacob point remcomp point fr
                        logiciels/informatique
                        http://www.cs.virginia.edu/~lcc-win32

                        Comment

                        • Richard Heathfield

                          #27
                          Re: Rounding double

                          jacob navia said:
                          Richard Heathfield wrote:
                          >jacob navia said:
                          >>
                          >>Richard Heathfield wrote:
                          >>>David Harmon said:
                          >>>>
                          >>>>On Thu, 22 Nov 2007 09:12:02 +0000 in comp.lang.c++, Richard
                          >>>>Heathfiel d <rjh@see.sig.in validwrote,
                          >>>>>Right - but of course a cross-posted article should "work" in all
                          >>>>>the groups into which it's posted.
                          >>>>But saying that "double &value" is illegal doesn't "work" in c.l.c++
                          >>>Touche'. :-) Nevertheless, agreeing that it's legal doesn't work in
                          >>>clc.
                          >>>>
                          >>Who cares?
                          >>
                          >People who don't wish to mislead [the] OP by giving incorrect advice.
                          >>
                          ><spam snipped>
                          >>
                          >
                          Incorrect advice happens only when they read your posts.
                          Wrong again. I'm not going to argue this point with you, however. If anyone
                          is silly enough to believe you, that's their problem.

                          --
                          Richard Heathfield <http://www.cpax.org.uk >
                          Email: -http://www. +rjh@
                          Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                          "Usenet is a strange place" - dmr 29 July 1999

                          Comment

                          • Walter Roberson

                            #28
                            Re: Rounding double

                            In article <fi4tao$n96$2@a ioe.org>, jacob navia <jacob@nospam.o rgwrote:
                            >Richard Heathfield wrote:
                            >jacob navia said:
                            >>Who cares?
                            >People who don't wish to mislead to OP by giving incorrect advice.
                            >Incorrect advice happens only when they read your posts.
                            Incorrect advice also happens when people read some of *my* posts,
                            so your "only" would appear to be incorrect.

                            I don't know of -any- poster here who has a perfect advice posting
                            record (though some average much higher quality than others.)
                            --
                            "No one has the right to destroy another person's belief by
                            demanding empirical evidence." -- Ann Landers

                            Comment

                            • jacob navia

                              #29
                              Re: Rounding double

                              Walter Roberson wrote:
                              In article <fi4tao$n96$2@a ioe.org>, jacob navia <jacob@nospam.o rgwrote:
                              >Richard Heathfield wrote:
                              >>jacob navia said:
                              >
                              >>>Who cares?
                              >
                              >>People who don't wish to mislead to OP by giving incorrect advice.
                              >
                              >Incorrect advice happens only when they read your posts.
                              >
                              Incorrect advice also happens when people read some of *my* posts,
                              so your "only" would appear to be incorrect.
                              >
                              I don't know of -any- poster here who has a perfect advice posting
                              record (though some average much higher quality than others.)
                              And there are others that do not give ANY solution,
                              limiting themselves to say why the solutions presented in some
                              cases not asked for would not work

                              I used the original solution and fixed it for double
                              precision, it is surely not the best solution.

                              But instead of proposing a better solution this people limit
                              to talking nonsense without ever proposing anything else.

                              Then I get angry start getting mad at heathfield and this
                              degrades.

                              --
                              jacob navia
                              jacob at jacob point remcomp point fr
                              logiciels/informatique
                              http://www.cs.virginia.edu/~lcc-win32

                              Comment

                              • Richard Heathfield

                                #30
                                Re: Rounding double

                                jacob navia said:
                                Walter Roberson wrote:
                                >In article <fi4tao$n96$2@a ioe.org>, jacob navia <jacob@nospam.o rg>
                                >wrote:
                                >>Richard Heathfield wrote:
                                >>>jacob navia said:
                                >>
                                >>>>Who cares?
                                >>
                                >>>People who don't wish to mislead to OP by giving incorrect advice.
                                >>
                                >>Incorrect advice happens only when they read your posts.
                                >>
                                >Incorrect advice also happens when people read some of *my* posts,
                                >so your "only" would appear to be incorrect.
                                >>
                                >I don't know of -any- poster here who has a perfect advice posting
                                >record (though some average much higher quality than others.)
                                >
                                And there are others that do not give ANY solution,
                                Instead, they have given appropriate explanations of why no solution to the
                                question as asked is possible, and suggestions as to how to deal with this
                                problem, which is inherent in the general representation of fractions.
                                limiting themselves to say why the solutions presented in some
                                cases not asked for would not work
                                The question as asked is: "Does any body know, how to round a double value
                                with a specific number of digits after the decimal points?" No specific
                                cases were asked for, so any criticism of any broken "solution", if it
                                addresses any cases at all, is bound to address cases not asked for.
                                I used the original solution and fixed it for double
                                precision, it is surely not the best solution.
                                That, at least, is true. The best solution is to understand the inherent
                                limitations in representing fractions, and to recognise that the
                                appropriate way to deal with this is to deal with the rounding at the
                                display stage.
                                But instead of proposing a better solution this people limit
                                to talking nonsense without ever proposing anything else.
                                Ah, we're back to "nonsense" as your synonym for "correct solution that
                                Jacob Navia doesn't understand", are we?
                                Then I get angry start getting mad at heathfield and this
                                degrades.
                                If you get angry every time you're wrong, you must be angry a *lot*. But
                                the degradation comes from your posting of incorrect solutions and
                                misconceptions about C. If you were to take the trouble to learn C
                                properly, you might find yourself winning more arguments (and getting the
                                support of others in this newsgroup, instead of constantly facing their
                                corrections).

                                --
                                Richard Heathfield <http://www.cpax.org.uk >
                                Email: -http://www. +rjh@
                                Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                                "Usenet is a strange place" - dmr 29 July 1999

                                Comment

                                Working...