为什么我得到的值是1.#INF
Why am I getting values of 1.#INF?
我在这里附上了我的代码。
我为"tempval"添加了一个打印语句,它正在打印1.#INF和-1.INF.的值
我查了一下,发现#INF是在除以0时返回的,但我不明白为什么它会在这里这样做。
有什么建议吗?
/*
PROGRAM: sinx.cpp
PURPOSE: This program uses the infinite series for sin(x) and evaluates the value of the function sin(x)
until the next term in the sum is less than 1E-6.
It also compares the value calculated by the program using the infinite series to the value of sin(x)
as determined by the pre-defined functions inside C++'s <cmath>.
*/
#include <iostream>
#include <ciso646>
#include <cmath>
#include <string>
#include <fstream>
using namespace std;
static int factorial(int); //performs a factorial on the quantity passed, takes an int, returns an int
static string promptForString(string prompt); //prompts the user for input, takes the input, and returns the input as a string (takes/returns string)
static void introduction(); //Runs promptForString, and depending on input answer, outputs instructions. (takes/returns string)
int main()
{
//-------------------------------
//----------EXPLANATION----------
//-------------------------------
cout << "-----" << endl;
cout << "This program will calculate the value of sin(x), with your input of x," << endl;
cout << "using the infinite series of sin(x), and will stop calculations" << endl;
cout << "when the next term in the series is less that 1E-6." << endl << endl;
introduction();
//------------------------------
//----------USER-INPUT----------
//------------------------------
double x;
cout << "-----" << endl;
cout << "Enter a value for x: "; // User-input for the value to calculate the sin of
cin >> x;
cout << endl;
//--------------------------------
//----------CALCULATIONS----------
//--------------------------------
double sinx = 0; //initialize sinx so it is available inside the loop
bool done = false;
while (done == false)
{
for (int i = 0; i >= 0; i++) //While i>0 (infinite loop) --- ends due to conditions inside the loops
{
double tempval = (pow(-1, i))*( (pow(x, (2.0 * i) + 1.0)) / (factorial((2 * i) + 1))); //term "i" calculation
cout << tempval << endl;
if (fabs(tempval) < (1 * (pow(10, -6)))) //if it is precise enough, end the loops
{
i = -2; //low enough so that it will add 1 ( i = -1 ) at the end of the loop, then fail to meet the correct for loop conditions
done = true;
}
else if (fabs(tempval) >= (1 * (pow(10, -6)))) //if it is not precise enough yet, add the term in the series, and the loop will loop
{
sinx += tempval;
}
}
}
//At this point, the calculation for sin(x) with the series is done, and the value is stored in the double "sinx"
//------------------------------
//----------COMPARISON----------
//------------------------------
double cmathsinx = sin(x); //cmath's interpretation
double difference = abs(cmathsinx - sinx); //Difference between calculated and function values
//--------------------------
//----------OUTPUT----------
//--------------------------
cout << "-----" << endl;
cout << "The program used the infinite series for sin(x) and" << endl
<< "calculated for sin(" << x << ")." << endl
<< "The value was: " << sinx << "." << endl << endl;
cout << "The sin function in the math header file for C++ calculated" << endl
<< "that sin(" << x << ") = " << cmathsinx << "." << endl << endl;
cout << "The difference between those two numbers is: " << difference << "."
<< endl << endl;
cout << "-----" << endl << endl << endl;
return 0;
}
static int factorial(int number)
{
int temp;
if (number <= 1)
{
return 1;
}
temp = number * factorial(number - 1);
return temp;
//return (n == 1 || n == 0) ? 1 : factorial(n - 1) * n;
}
static string promptForString(string prompt)
{
cout << prompt; //prompts user for a string and returns the string to the main program
string input;
cin >> input;
return input;
}
void introduction()
{
string prompt = "-----nDo you need instructions? Enter "y" for yes and "n" for no: ";
string yesno = promptForString(prompt);
string instructions = "n Enter a value for x, and then press enter.";
if (yesno == "y")
cout << instructions << endl << endl;
else if (yesno == "n")
cout << "OK, here we go." << endl << endl;
}
编辑:我运行了一个测试,只使用for循环和tempval计算。一开始,它返回正确的值,但不久之后,它开始返回+/-1。#INF
您可能遇到整数溢出问题。13!超过了int可以保持的最大值(6227020800>21474833647),因此当i>=6时,无穷级数将返回错误的值。
使用更大的数据类型(std::uint64_t)会有所帮助,但不会有多大帮助——21!也会超过它的极限,所以你需要在第10次迭代之前收敛。
相关文章:
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- 为什么在全局范围内使用"extern int a"似乎不行?
- 为什么在popback()操作之后,它仍然打印完整的矢量
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 为什么会发生堆损坏
- 为什么使用 "this" 指针调用派生成员函数?
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 为什么比较运算符如此快速
- 为什么 Serial.println(<char[]>);返回随机字符?
- 为什么这个运算符<重载函数对 STL 算法不可见?
- 为什么不;名字在地图上是按顺序排列的吗
- 我的字符计数代码计算错误.为什么
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 为什么在C++中使用私有复制构造函数与删除复制构造函数
- 为什么野牛仍在使用"int yylex(void)",却找不到"int yylex(YYS
- 为什么当我尝试计算平均绝对偏差时,我的程序返回 inf
- 为什么“inf”表示浮点数溢出
- 为什么我得到的值是1.#INF