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 > problem with double data in exponential form
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
problem with double data in exponential form

Réponse
 
LinkBack Outils de la discussion
Vieux 14/04/2008, 18h44   #1
pereges
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut problem with double data in exponential form

Hi, can anyone please tell me what is wrong in this program and why
does it keep givng nan as the output(eg. take input as 1.2e+10) ?

#include <stdio.h>

#define PLANCK 6.626068e10-34


int main(void)
{
double freq;

printf("Enter frequency");
scanf("%e",&freq);
double energy = PLANCK * freq;

printf("\n%f", energy);
return 0;

}
  Réponse avec citation
Vieux 14/04/2008, 19h00   #2
user923005
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: problem with double data in exponential form

On Apr 14, 10:44am, pereges <Brol...@gmail.com> wrote:
> Hi, can anyone please tell me what is wrong in this program and why
> does it keep givng nan as the output(eg. take input as 1.2e+10) ?
>
> #include <stdio.h>
>
> #define PLANCK 6.626068e10-34
>
> int main(void)
> {
> double freq;
>
> printf("Enter frequency");
> scanf("%e",&freq);


foo.c: (in function main)
foo.c(13,28): Format argument 1 to scanf (%e) expects float * gets
double *:

> double energy = PLANCK * freq;
>
> printf("\n%f", energy);
> return 0;
>
>
>
> }- Hide quoted text -
>
> - Show quoted text -


  Réponse avec citation
Vieux 14/04/2008, 19h05   #3
user923005
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: problem with double data in exponential form

On Apr 14, 10:44am, pereges <Brol...@gmail.com> wrote:
> Hi, can anyone please tell me what is wrong in this program and why
> does it keep givng nan as the output(eg. take input as 1.2e+10) ?
>
> #include <stdio.h>
>
> #define PLANCK 6.626068e10-34
>
> int main(void)
> {
> double freq;
>
> printf("Enter frequency");
> scanf("%e",&freq);
> double energy = PLANCK * freq;
>
> printf("\n%f", energy);
> return 0;
>
>
>
> }- Hide quoted text -
>
> - Show quoted text -


P.S.
From the C-FAQ:
12.13: Why doesn't this code:

double d;
scanf("%f", &d);

work?

A: Unlike printf(), scanf() uses %lf for values of type double,
and
%f for float. See also question 12.9.
  Réponse avec citation
Vieux 14/04/2008, 19h09   #4
Gordon Burditt
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: problem with double data in exponential form

>Hi, can anyone please tell me what is wrong in this program and why
>does it keep givng nan as the output(eg. take input as 1.2e+10) ?
>
>#include <stdio.h>
>

Is this really supposed to be a constant slightly different from
6.626068e10 ?

>#define PLANCK 6.626068e10-34


Use parentheses.

> double energy = PLANCK * freq;

This expands to:

double energy = 6.626068e10-34 * freq;
which means:
double energy = (6.626068e10) - (34 * freq);
which I don't think is what you really mean.

I recommend you use at least one pair of unnecessary parentheses:

#define PLANCK (((6.626068e10) - (34)))

  Réponse avec citation
Vieux 14/04/2008, 19h11   #5
pereges
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: problem with double data in exponential form

On Apr 14, 11:05 pm, user923005 <dcor...@connx.com> wrote:
> P.S.
> From the C-FAQ:
> 12.13: Why doesn't this code:
>
> double d;
> scanf("%f", &d);
>
> work?
>
> A: Unlike printf(), scanf() uses %lf for values of type double,
> and
> %f for float. See also question 12.9.


I changed it to:

double freq;
printf("Enter frequency");
scanf("%lf",&freq);
double energy = PLANCK * freq;
printf("\n%e", energy);


getting some negative garbage value now.
  Réponse avec citation
Vieux 14/04/2008, 19h36   #6
user923005
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: problem with double data in exponential form

On Apr 14, 11:11am, pereges <Brol...@gmail.com> wrote:
> On Apr 14, 11:05 pm, user923005 <dcor...@connx.com> wrote:
>
> > P.S.
> > From the C-FAQ:
> > 12.13: Why doesn't this code:

>
> > double d;
> > scanf("%f", &d);

>
> > work?

>
> > A: Unlike printf(), scanf() uses %lf for values of type double,
> > and
> > %f for float. See also question 12.9.

>
> I changed it to:
>
> double freq;
> printf("Enter frequency");
> scanf("%lf",&freq);
> double energy = PLANCK * freq;
> printf("\n%e", energy);
>
> getting some negative garbage value now.


From:
http://physics.nist.gov/cgi-bin/cuu/Value?h

static const double planck = 6.62606896e-34;
  Réponse avec citation
Vieux 14/04/2008, 19h50   #7
user923005
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: problem with double data in exponential form

On Apr 14, 11:36am, user923005 <dcor...@connx.com> wrote:
> On Apr 14, 11:11am, pereges <Brol...@gmail.com> wrote:
>
>
>
>
>
> > On Apr 14, 11:05 pm, user923005 <dcor...@connx.com> wrote:

>
> > > P.S.
> > > From the C-FAQ:
> > > 12.13: Why doesn't this code:

>
> > > double d;
> > > scanf("%f", &d);

>
> > > work?

>
> > > A: Unlike printf(), scanf() uses %lf for values of type double,
> > > and
> > > %f for float. See also question 12.9.

>
> > I changed it to:

>
> > double freq;
> > printf("Enter frequency");
> > scanf("%lf",&freq);
> > double energy = PLANCK * freq;
> > printf("\n%e", energy);

>
> > getting some negative garbage value now.

>
> From:http://physics.nist.gov/cgi-bin/cuu/Value?h
>
> static const double planck = 6.62606896e-34;- Hide quoted text -
>
> - Show quoted text -


#include <stdio.h>

// Standard value:
static const double planck = 6.62606896e-34;
// 2005 National Physical Laboratory value:
static const double planck2 = 6.62607095e-34;

int main(void)
{
double freq;
double energy;
int converted;

oopsie:
printf("Enter frequency: ");
converted = scanf("%le", &freq);
if (converted == 1) {
energy = planck * freq;
printf("Standard energy = %20.17g\n", energy);
energy = planck2 * freq;
printf("Alternative energy = %20.17g\n", energy);
} else
goto oopsie;
return 0;
}


  Réponse avec citation
Vieux 14/04/2008, 19h59   #8
Robert Gamble
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: problem with double data in exponential form

On Apr 14, 1:44 pm, pereges <Brol...@gmail.com> wrote:
> Hi, can anyone please tell me what is wrong in this program and why
> does it keep givng nan as the output(eg. take input as 1.2e+10) ?
>
> #include <stdio.h>
>
> #define PLANCK 6.626068e10-34


#define PLANCK 6.626068e-34

> int main(void)
> {
> double freq;
>
> printf("Enter frequency");


printf("Enter frequency: ");
fflush(stdout);

> scanf("%e",&freq);


scanf("%le", &freq);

> double energy = PLANCK * freq;
>
> printf("\n%f", energy);


printf("%e\n", energy);

> return 0;
>
> }


--
Robert Gamble
  Réponse avec citation
Vieux 15/04/2008, 00h11   #9
Keith Thompson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: problem with double data in exponential form

[Once again, I've added the attribution line that Gordon Burditt has
deliberately and rudely deleted.]

gordonb.czo5z@burditt.org (Gordon Burditt) writes:
> pereges <Broli00@gmail.com> writes:
>>Hi, can anyone please tell me what is wrong in this program and why
>>does it keep givng nan as the output(eg. take input as 1.2e+10) ?
>>
>>#include <stdio.h>
>>

> Is this really supposed to be a constant slightly different from
> 6.626068e10 ?
>
>>#define PLANCK 6.626068e10-34

>
> Use parentheses.


Lack of parentheses isn't the problem. Either this is a typo, or the
OP has misunderstood how scientific notation is represented in C.

The approximage value of Planck's constant is 6.626068 * 10**-34,
where "**" denotes exponentiation. In mathematical texts, this is
generally written as something like:

-34
6.626068 * 10

(That's not going to look right unless you use a fixed-width font.)

Since C needs to be written using fonts that don't support
superscripts, the C notation drops the constant "10" and uses:

6.626068E-34

What the OP wrote, "6.626068e10-34", would be interpreted by a
compiler as the floating-point constant 6.626068e10, a "-" operator,
and the integer constant 34, but I'm sure that's not what he intended.

So the correct definition (assuming the value is correct) would be:

#define PLANCK 6.626068e-34

No parentheses are necessary; a floating-point constant is a single
token, and no ambiguity is possible (barring ugly tricks with
token-pasting).

[...]

> I recommend you use at least one pair of unnecessary parentheses:
>
> #define PLANCK (((6.626068e10) - (34)))


That's just silly; I hope it was deliberately silly.

As always, I do not grant permission to quote this or any other
article I post to Usenet without attribution. Gordon, you're more
than welcome to respond; just leave the attribution lines alone.

--
Keith Thompson (The_Other_Keith) <kst-u@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
  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 21h24.


É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,15120 seconds with 17 queries