|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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.14159265358979323846 /* 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; } |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
>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.200f", 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.14159265358979323846 /* 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; > >} |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
In article <bdd6faac-50e6-4052-9699-35517b3955e9@q27g2000prf.googlegroups.com>,
pereges <Broli00@gmail.com> 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.14159265358979323846 /* 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. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
On Apr 10, 8:53pm, pereges <Brol...@gmail.com> 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.14159265358979323846 /* 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_approximation = 3.1415926535897932384626433832795; int main(void) { printf("My pi approximation is %.*g\n", DBL_DIG + 1, pi_approximation); return 0; } |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
On Apr 11, 11:10 am, user923005 <dcor...@connx.com> wrote:
> On Apr 10, 8:53 pm, pereges <Brol...@gmail.com> 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.14159265358979323846 /* 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_approximation = > 3.1415926535897932384626433832795; > > int main(void) { > printf("My pi approximation is %.*g\n", DBL_DIG + 1, > pi_approximation); > return 0; > > } why declare pi_approximation as a static const ? |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
pereges said:
<snip> > why declare pi_approximation 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 |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
On Apr 11, 12:05 pm, Richard Heathfield <r...@see.sig.invalid> wrote:
> pereges said: > > <snip> > > > why declare pi_approximation 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 ? |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
On Apr 11, 12:05 pm, Richard Heathfield <r...@see.sig.invalid> wrote:
> pereges said: > > <snip> > > > why declare pi_approximation 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 ? |
|
![]() |
| Outils de la discussion | |
|
|