PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > How to get more precision in C ?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
How to get more precision in C ?

Réponse
 
LinkBack Outils de la discussion
Vieux 11/04/2008, 04h53   #1
pereges
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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.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;

}
  Réponse avec citation
Vieux 11/04/2008, 05h02   #2
Gordon Burditt
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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.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;
>
>}

  Réponse avec citation
Vieux 11/04/2008, 05h05   #3
Walter Roberson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to get more precision in C ?

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.
  Réponse avec citation
Vieux 11/04/2008, 07h10   #4
user923005
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to get more precision in C ?

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

  Réponse avec citation
Vieux 11/04/2008, 07h54   #5
pereges
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to get more precision in C ?

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 ?
  Réponse avec citation
Vieux 11/04/2008, 08h05   #6
Richard Heathfield
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to get more precision in C ?

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
  Réponse avec citation
Vieux 11/04/2008, 08h17   #7
pereges
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to get more precision in C ?

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 ?

  Réponse avec citation
Vieux 11/04/2008, 08h20   #8
pereges
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to get more precision in C ?

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 ?

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 19h00.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,18327 seconds with 16 queries