重要なお知らせ

「教えて! goo」は2025年9月17日(水)をもちまして、サービスを終了いたします。詳細はこちら>

電子書籍の厳選無料作品が豊富!

以下のコードをコンパイル実行すると、printfで表示する変数が全て『+NAN』と表示されます。

-------------------------------

#include<stdio.h>

int main(void){
double Vin,Vref,Vout,dv,P,I,PI,nextV,m;

printf("input Vref : ");
scanf("%f",&Vref);
printf("\ninput Vin : ");
scanf("%f",&Vin);
printf("\ninput Vout : ");
scanf("%f",&Vout);

dv=0;
P=0;
I=0;
m=0;
nextV=0;
printf("dv | P | I | PI | nextV | m | Vout |\n");
for(;;){
dv=Vref-Vout;
printf("%f ",dv);
P=dv*0.1;
printf("%f ",P);
I=I+dv*0.01;
printf("%f ",I);
PI=P + I;
printf("%f ",PI);
nextV = Vout + PI;
printf("%f ",nextV);
m=nextV/(Vin * 7.2);
printf("%f ",m);
Vout = nextV + 2;
printf("%f \n",Vout);
}
return 0;
}
-------------------------

なにがなんだか、何がNANだかわかりません(涙
よろしくお願いします。

A 回答 (1件)

NaNってのは Not a Number で、数じゃない浮動小数点数データのこと。



第5章 浮動小数点
http://web.hc.keio.ac.jp/~fujimura/2002/lang/lec …

の 5.2 あたりを参照してください。
で、計算式のからくりはよくわからんのだけど、

double Vin,Vref,Vout,dv,P,I,PI,nextV,m;

printf("input Vref : ");
scanf("%f",&Vref);
printf("\ninput Vin : ");
scanf("%f",&Vin);
printf("\ninput Vout : ");
scanf("%f",&Vout);

ここの scanf の書式指定は %lf でないとダメです。

>gcc -Wall nan.c
nan.c: In function `main':
nan.c:7: warning: float format, double arg (arg 2)
nan.c:9: warning: float format, double arg (arg 2)
nan.c:11: warning: float format, double arg (arg 2)
nan.c:36:2: warning: no newline at end of file

ということで書式を修正して再度試してみてください。
    • good
    • 0
この回答へのお礼

scanf() の書式が『%lf』だったのですね☆
解決できました。
ありがとうございました。

お礼日時:2008/01/05 15:50

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!