[英]why floating point numbers does not give desired answer?
嘿,我正在制作小型 C++ 程序来计算 sin(x) 的值直到小数点后 7 位,但是当我使用这个程序计算sin(PI/2)时,它给了我0.9999997
而不是1.0000000
我该如何解决这个错误? 我知道为什么我得到这个值是 output,问题是我应该用什么方法来解决这个逻辑错误?
这是我的参考代码
#include <iostream>
#include <iomanip>
#define PI 3.1415926535897932384626433832795
using namespace std;
double sin(double x);
int factorial(int n);
double Pow(double a, int b);
int main()
{
double x = PI / 2;
cout << setprecision(7)<< sin(x);
return 0;
}
double sin(double x)
{
int n = 1; //counter for odd powers.
double Sum = 0; // to store every individual expression.
double t = 1; // temp variable to store individual expression
for ( n = 1; t > 10e-7; Sum += t, n = n + 2)
{
// here i have calculated two terms at a time because addition of two consecutive terms is always less than 1.
t = (Pow(-1.00, n + 1) * Pow(x, (2 * n) - 1) / factorial((2 * n) - 1))
+
(Pow(-1.00, n + 2) * Pow(x, (2 * (n+1)) - 1) / factorial((2 * (n+1)) - 1));
}
return Sum;
}
int factorial(int n)
{
if (n < 2)
{
return 1;
}
else
{
return n * factorial(n - 1);
}
}
double Pow(double a, int b)
{
if (b == 1)
{
return a;
}
else
{
return a * Pow(a, b - 1);
}
}
我看到三个错误:
10e-7
是10*10^(-7)
,它似乎比你想要的大 10 倍。 我想你想要1e-7
。
如果t
仍然很大但为负,您的测试t > 10e-7
将变为假,并退出循环。 您可能需要abs(t) > 1e-7
。
要获得所需的精度,您需要达到n = 7
,这让您计算factorial(13)
,它会溢出 32 位int
。 (如果使用 gcc ,您可以使用-fsanitize=undefined
或-ftrapv
来捕捉它。)您可以通过使用至少 64 位的long long int
或int64_t
来获得一些喘息空间。
sin(PI/2)... 它给了我 0.9999997 而不是 1.0000000
对于[-pi/4...+pi/4]
之外的值,泰勒的 sin/cos 级数收敛缓慢,并且会遭受项的取消和int factorial(int n)
**的溢出。 留在甜蜜的范围内。
考虑使用三角属性sin(x + pi/2) = cos(x)
、 sin(x + pi) = -sin(x)
等将x
带入[-pi/4...+pi/4]
范围。
代码使用remquo ( ref2 ) 来查找余数和部分商。
// Bring x into the -pi/4 ... pi/4 range (i.e. +/- 45 degrees)
// and then call owns own sin/cos function.
double my_wide_range_sin(double x) {
if (x < 0.0) {
return -my_sin(-x);
}
int quo;
double x90 = remquo(fabs(x), pi/2, &quo);
switch (quo % 4) {
case 0:
return sin_sweet_range(x90);
case 1:
return cos_sweet_range(x90);
case 2:
return sin_sweet_range(-x90);
case 3:
return -cos_sweet_range(x90);
}
return 0.0;
}
这意味着 OP 也需要编写cos()
function。
**可以使用long long
而不是int
来稍微扩展int factorial(int n)
的有用范围,但这只会增加一些x
。 可以使用double
。
更好的方法根本不使用factorial()
,而是将每个连续项缩放1.0/(n * (n+1))
等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.