How to get more precision in C ?

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

    How to get more precision in C ?

    I would like to have precision upto atleast 8 digits in my numerical
    computation program. I have tried using doubles but I keep getting
    results only till 6 places after decimal. eg.

    #include <stdio.h>
    #define M_PI 3.1415926535897 9323846 /* M_PI is not defined in math.h
    according to my compiler*/

    int main(void)
    {
    double s;
    s = M_PI;
    printf("%f", s);


    return 0;

    }
  • Gordon Burditt

    #2
    Re: How to get more precision in C ?

    >I would like to have precision upto atleast 8 digits in my numerical
    >computation program. I have tried using doubles but I keep getting
    >results only till 6 places after decimal. eg.
    You only *print* 6 digits after the decimal. You may be getting greater
    precision in the actual computation.

    Try: printf("%300.20 0f", s);
    although this is primarily useful for debugging. A double won't
    have nearly this many digits of precision.
    >#include <stdio.h>
    >#define M_PI 3.1415926535897 9323846 /* M_PI is not defined in math.h
    >according to my compiler*/
    >
    >int main(void)
    >{
    > double s;
    s = M_PI;
    printf("%f", s);
    >
    >
    >return 0;
    >
    >}

    Comment

    • Walter Roberson

      #3
      Re: How to get more precision in C ?

      In article <bdd6faac-50e6-4052-9699-35517b3955e9@q2 7g2000prf.googl egroups.com>,
      pereges <Broli00@gmail. comwrote:
      >I would like to have precision upto atleast 8 digits in my numerical
      >computation program. I have tried using doubles but I keep getting
      >results only till 6 places after decimal. eg.
      >#include <stdio.h>
      >#define M_PI 3.1415926535897 9323846 /* M_PI is not defined in math.h
      >according to my compiler*/
      >int main(void)
      >{
      > double s;
      s = M_PI;
      printf("%f", s);
      >return 0;
      >}
      Specify a precision with your %f format, such as %.60f . The default
      for %f is 6.

      Also, to be safe, ensure your output ends in a newline. For example,

      printf("%.9f\n" , s);

      --
      Q: Why did the chicken cross the Mobius strip?

      A: There were manifold reasons.

      Comment

      • user923005

        #4
        Re: How to get more precision in C ?

        On Apr 10, 8:53 pm, pereges <Brol...@gmail. comwrote:
        I would like to have precision upto atleast 8 digits in my numerical
        computation program. I have tried using doubles but I keep getting
        results only till 6 places after decimal. eg.
        >
        #include <stdio.h>
        #define M_PI 3.1415926535897 9323846 /* M_PI is not defined in math.h
        according to my compiler*/
        >
        int main(void)
        {
                double s;
                s = M_PI;
                printf("%f", s);
        >
        return 0;
        >
        >
        >
        }
        Try it this way:

        #include <stdio.h>
        #include <float.h>
        static const double pi_approximatio n =
        3.1415926535897 932384626433832 795;

        int main(void) {
        printf("My pi approximation is %.*g\n", DBL_DIG + 1,
        pi_approximatio n);
        return 0;
        }

        Comment

        • pereges

          #5
          Re: How to get more precision in C ?

          On Apr 11, 11:10 am, user923005 <dcor...@connx. comwrote:
          On Apr 10, 8:53 pm, pereges <Brol...@gmail. comwrote:
          >
          >
          >
          I would like to have precision upto atleast 8 digits in my numerical
          computation program. I have tried using doubles but I keep getting
          results only till 6 places after decimal. eg.
          >
          #include <stdio.h>
          #define M_PI 3.1415926535897 9323846 /* M_PI is not defined in math.h
          according to my compiler*/
          >
          int main(void)
          {
          double s;
          s = M_PI;
          printf("%f", s);
          >
          return 0;
          >
          }
          >
          Try it this way:
          >
          #include <stdio.h>
          #include <float.h>
          static const double pi_approximatio n =
          3.1415926535897 932384626433832 795;
          >
          int main(void) {
          printf("My pi approximation is %.*g\n", DBL_DIG + 1,
          pi_approximatio n);
          return 0;
          >
          }
          why declare pi_approximatio n as a static const ?

          Comment

          • Richard Heathfield

            #6
            Re: How to get more precision in C ?

            pereges said:

            <snip>
            why declare pi_approximatio n as a static const ?
            Were you planning on changing it?


            --
            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

            • pereges

              #7
              Re: How to get more precision in C ?

              On Apr 11, 12:05 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
              pereges said:
              >
              <snip>
              >
              why declare pi_approximatio n as a static const ?
              >
              Were you planning on changing it?
              >

              if you are not planning to change it, then declare it as const. what
              is the purpose behind declaring it as static ?

              Comment

              • pereges

                #8
                Re: How to get more precision in C ?

                On Apr 11, 12:05 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
                pereges said:
                >
                <snip>
                >
                why declare pi_approximatio n as a static const ?
                >
                Were you planning on changing it?
                >

                if you are not planning to change it, then declare it as const. what
                is the purpose behind declaring it as static ?

                Comment

                • Richard Heathfield

                  #9
                  Re: How to get more precision in C ?

                  pereges said:
                  On Apr 11, 12:05 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
                  >pereges said:
                  >>
                  ><snip>
                  >>
                  why declare pi_approximatio n as a static const ?
                  >>
                  >Were you planning on changing it?
                  >>
                  >
                  >
                  if you are not planning to change it, then declare it as const. what
                  is the purpose behind declaring it as static ?
                  I would imagine that Dann did that to avoid the possibility of a name clash
                  with other file scope objects, either in other translation units or
                  perhaps in a third-party library. It wouldn't really matter in a toy
                  program like this, of course.

                  --
                  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

                  • Philip Potter

                    #10
                    Re: How to get more precision in C ?

                    pereges wrote:
                    On Apr 11, 11:10 am, user923005 <dcor...@connx. comwrote:
                    >On Apr 10, 8:53 pm, pereges <Brol...@gmail. comwrote:
                    >>#define M_PI 3.1415926535897 9323846 /* M_PI is not defined in math.h
                    >>
                    >static const double pi_approximatio n =
                    >3.141592653589 793238462643383 2795;
                    >>
                    why declare pi_approximatio n as a static const ?
                    #defines and consts are different beasts which can often serve a similar
                    purpose. Each has their own advantages and disadvantages - for example,
                    consts will do type-checking, while #defined constants can be used in
                    array dimensions (without creating a VLA).

                    There's a much more rigorous discussion of the differences between
                    #define and const here:


                    Comment

                    • jacob navia

                      #11
                      Re: How to get more precision in C ?

                      Richard Heathfield wrote:
                      pereges said:
                      >
                      <snip>
                      >
                      >why declare pi_approximatio n as a static const ?
                      >
                      Were you planning on changing it?
                      >
                      >
                      Ahh you never know when pi will change and circles
                      become squares. Specially after two or three Martinis,
                      I get those wobbling feelings...

                      :-)

                      Comment

                      • user923005

                        #12
                        Re: How to get more precision in C ?

                        On Apr 11, 12:17 am, pereges <Brol...@gmail. comwrote:
                        On Apr 11, 12:05 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
                        >
                        pereges said:
                        >
                        <snip>
                        >
                        why declare pi_approximatio n as a static const ?
                        >
                        Were you planning on changing it?
                        >
                        if you are not planning to change it, then declare it as const. what
                        is the purpose behind declaring it as static ?
                        If I intended to use it in another module, it would not be static. If
                        the complete system does not need the value to be exposed for other
                        translation units, then it must become static (my rule, not imposed by
                        C at all). Lint will tell you if that is the case. There is no need
                        to pollute the end-user's namespace unless you really have to.

                        I don't like macros for floating point constants. I do tend to use
                        them for array dimentions and integral constants of that nature. It's
                        a personal choice and certainly not any better than using a macro.

                        Comment

                        • Joe Wright

                          #13
                          Re: How to get more precision in C ?

                          pereges wrote:
                          I would like to have precision upto atleast 8 digits in my numerical
                          computation program. I have tried using doubles but I keep getting
                          results only till 6 places after decimal. eg.
                          >
                          #include <stdio.h>
                          #define M_PI 3.1415926535897 9323846 /* M_PI is not defined in math.h
                          according to my compiler*/
                          >
                          int main(void)
                          {
                          double s;
                          s = M_PI;
                          printf("%f", s);
                          >
                          >
                          return 0;
                          >
                          }
                          The "%f" format string dooms you to six decimals after the point. If you
                          want to display our common 64-bit double at full precision, use..

                          printf("%.16e", s);

                          ...will print..

                          3.1415926535897 931e+00

                          ...which is all the precision there is in the 64-bit double.

                          This is good for any double. You can coerce printf to print more that 17
                          digits but the 'more' doesn't count.

                          --
                          Joe Wright
                          "Everything should be made as simple as possible, but not simpler."
                          --- Albert Einstein ---

                          Comment

                          • user923005

                            #14
                            Re: How to get more precision in C ?

                            On Apr 11, 3:04 pm, Joe Wright <joewwri...@com cast.netwrote:
                            pereges wrote:
                            I would like to have precision upto atleast 8 digits in my numerical
                            computation program. I have tried using doubles but I keep getting
                            results only till 6 places after decimal. eg.
                            >
                            #include <stdio.h>
                            #define M_PI 3.1415926535897 9323846 /* M_PI is not defined in math.h
                            according to my compiler*/
                            >
                            int main(void)
                            {
                               double s;
                                    s = M_PI;
                                    printf("%f", s);
                            >
                            return 0;
                            >
                            }
                            >
                            The "%f" format string dooms you to six decimals after the point. If you
                            want to display our common 64-bit double at full precision, use..
                            >
                               printf("%.16e", s);
                            >
                            ..will print..
                            >
                               3.1415926535897 931e+00
                            >
                            ..which is all the precision there is in the 64-bit double.
                            >
                            This is good for any double. You can coerce printf to print more that 17
                            digits but the 'more' doesn't count.
                            Assuming 8 byte doubles with 52-53 bits in the mantissa that is true
                            (OK, every implementation I can think of is like that).

                            DBL_DIG must be _at least_ 10, but it could be arbitrarily large on a
                            hypothetical C compiler.
                            Some available floating point in hardware:


                            It's interesting that AIX implements 128 bit floating point in
                            software:


                            A compiler vendor could certainly use that technique and supply (for
                            instance):

                            float = 64 bit
                            double = 128 bit
                            long double = 256 bit

                            if they had the notion to do so.

                            Now, I do not know of any machine with doubles larger than 8 bytes but
                            I do know of 16 byte floating point that is 20+ years old (DEC VAX).

                            Comment

                            • Anonymous

                              #15
                              Re: How to get more precision in C ?

                              if you are not planning to change it, then declare it as const. what is
                              the purpose behind declaring it as static ?
                              A number of reasons. As others have already pointed out, static variables
                              will not cause name conflicts with other files.

                              Also, nonstatic global variables can be far slower than static ones,
                              especially in a shared library, as they may be indirected through a
                              global offset table. Making a variable global and nonstatic may also make
                              it difficult for the compiler to do certain optimizations.

                              Comment

                              Working...