C 中的简单浮点计算导致 -nan 作为值返回
Simple float calculation in C resulting in -nan coming back as a value
我正在编写一个简单的程序来计算文本的可读性,在计算平均值时,我得到了一个奇怪的结果:“-nan”。这是我代码底部的两个浮点计算函数返回的结果,当我在 main 函数中计算索引时,我得到一个负数。这应该是不可能的,因为 none 个被除数是负数。任何人都知道 -nan 是什么意思或我该如何解决这个问题?
谢谢!
#include <stdio.h>
#include <string.h>
int count_letters (string text);
int count_words (string text);
int count_sentences (string text);
float avg_letters (int lettercount, int wordcount);
float avg_sents (int wordcount, int sentcount);
int main (void)
{
string text = get_string("Text: ");
int lettercount = 0;
int wordcount = 0;
int sentcount = 0;
float S = 0;
float L = 0;
count_letters (text);
count_words (text);
count_sentences (text);
avg_letters (lettercount, wordcount);
avg_sents (wordcount, sentcount);
float index = 0.0588 * L - 0.296 * S - 15.8;
printf("%f\n", index);
}
//Counts letters in entire text
int count_letters (string text)
{
int lettercount = 0;
for (int i=0, n = strlen(text); i<n; i++)
{
if ((text[i] > 64 && text[i] < 91) || (text[i] > 96 && text[i] < 123))
{
lettercount ++;
}
}
printf ("%i\n", lettercount);
return lettercount;
}
//Counts words in text
int count_words (string text)
{
int wordcount = 0;
for (int i=0, n = strlen(text); i<n; i++)
{
if (text[i] == 32)
{
wordcount ++;
}
}
wordcount += 1;
printf ("%i\n", wordcount);
return wordcount;
}
//Counts sentences in text
int count_sentences (string text)
{
int sentcount = 0;
for (int i=0, n = strlen(text); i<n; i++)
{
if ((text[i] == 33) || (text[i] == 63) || (text[i] == 46))
{
sentcount ++;
}
}
printf ("%i\n", sentcount);
return sentcount;
}
//Averages letters per 100 words
float avg_letters (int lettercount, int wordcount)
{
float L = ((float) lettercount / wordcount) * 100;
printf("%f\n", L);
return L;
}
//Averages sentences per 100 words
float avg_sents (int wordcount, int sentcount)
{
float S = ((float) sentcount / wordcount) * 100;
printf("%f\n", S);
return S;
}
您忘记分配变量。
lettercount = count_letters (text);
wordcount = count_words (text);
sentcount = count_sentences (text);
L = avg_letters (lettercount, wordcount);
S = avg_sents (wordcount, sentcount);
由于您从未分配过它们,它们都仍然具有您初始化它们所用的 0
值,因此您在 avg_letters
中将 0
除以 0
和 avg_sents
。这会产生 nan
,代表 not a number
.
我正在编写一个简单的程序来计算文本的可读性,在计算平均值时,我得到了一个奇怪的结果:“-nan”。这是我代码底部的两个浮点计算函数返回的结果,当我在 main 函数中计算索引时,我得到一个负数。这应该是不可能的,因为 none 个被除数是负数。任何人都知道 -nan 是什么意思或我该如何解决这个问题?
谢谢!
#include <stdio.h>
#include <string.h>
int count_letters (string text);
int count_words (string text);
int count_sentences (string text);
float avg_letters (int lettercount, int wordcount);
float avg_sents (int wordcount, int sentcount);
int main (void)
{
string text = get_string("Text: ");
int lettercount = 0;
int wordcount = 0;
int sentcount = 0;
float S = 0;
float L = 0;
count_letters (text);
count_words (text);
count_sentences (text);
avg_letters (lettercount, wordcount);
avg_sents (wordcount, sentcount);
float index = 0.0588 * L - 0.296 * S - 15.8;
printf("%f\n", index);
}
//Counts letters in entire text
int count_letters (string text)
{
int lettercount = 0;
for (int i=0, n = strlen(text); i<n; i++)
{
if ((text[i] > 64 && text[i] < 91) || (text[i] > 96 && text[i] < 123))
{
lettercount ++;
}
}
printf ("%i\n", lettercount);
return lettercount;
}
//Counts words in text
int count_words (string text)
{
int wordcount = 0;
for (int i=0, n = strlen(text); i<n; i++)
{
if (text[i] == 32)
{
wordcount ++;
}
}
wordcount += 1;
printf ("%i\n", wordcount);
return wordcount;
}
//Counts sentences in text
int count_sentences (string text)
{
int sentcount = 0;
for (int i=0, n = strlen(text); i<n; i++)
{
if ((text[i] == 33) || (text[i] == 63) || (text[i] == 46))
{
sentcount ++;
}
}
printf ("%i\n", sentcount);
return sentcount;
}
//Averages letters per 100 words
float avg_letters (int lettercount, int wordcount)
{
float L = ((float) lettercount / wordcount) * 100;
printf("%f\n", L);
return L;
}
//Averages sentences per 100 words
float avg_sents (int wordcount, int sentcount)
{
float S = ((float) sentcount / wordcount) * 100;
printf("%f\n", S);
return S;
}
您忘记分配变量。
lettercount = count_letters (text);
wordcount = count_words (text);
sentcount = count_sentences (text);
L = avg_letters (lettercount, wordcount);
S = avg_sents (wordcount, sentcount);
由于您从未分配过它们,它们都仍然具有您初始化它们所用的 0
值,因此您在 avg_letters
中将 0
除以 0
和 avg_sents
。这会产生 nan
,代表 not a number
.