- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
template<unsigned I>
struct factorial{
enum{
value = I * factorial<I -1>::value
};
};
template<>
struct factorial<0>
{
enum{ value = 1};
};
template<unsigned pow>
inline double power(double const& value){
return value * power<pow-1>(value);
}
template<>
inline double power<1>(double const& value){
return value;
}
template<>
inline double power<0>(double const& value){
return 1;
}
template<unsigned term>
inline double taylor_polynomial(double const& x){
return power<term>(x) / factorial<term>::value;
}
template <unsigned term>
inline double taylor_sine_term(double const& x) {
return (power<term>(-1) / factorial<(2*term)+1>::value) * power<(2*term)+1>(x);
}
template<unsigned terms>
inline double taylor_sine(double const& x){
return taylor_sine_term<terms-1>(x) + taylor_sine_term<terms>(x);
}
template <>
inline double taylor_sine<0>(double const& x) {
return taylor_sine_term<0>(x);
}
使用下面的代码,我尝试基于 N 项泰勒级数实现 sin()
函数,但是当我比较函数的结果时,结果不正确,我不是确定为什么。运行以下代码:
std::cout<<sin(2 * M_PI * 0.5)<<" "<<taylor_sine<13>(2 * M_PI * 0.5);
结果在 1.22465e-16 -16546.9
据我所知,我正在正确计算系列,所以我不确定出了什么问题。
最佳答案
你调用了错误的函数来递归:
template<unsigned terms>
inline double taylor_sine(double const& x){
return taylor_sine_term<terms-1>(x) + taylor_sine_term<terms>(x);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
}
您要添加项 n
和项 n-1
而不是添加项 n
和 n- 的泰勒级数1
条款。
关于c++ - 基于泰勒级数的基于模板的 Sin() 实现产生不正确的结果 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31323961/
*> sin sin 0.5 :10:1: Non type-variable argument in the constraint: Floating (a -> a) (Use Flexible
我一直在尝试实现一个快速但更重要的是准确的自定义 sin 函数(我不能在我的项目中使用 math.h sin)。我不是这类数学方面的专家,所以请和我一起工作 XD。在网上稍作搜索后,我发现了以下代码,
我编写了一个 Prolog 程序来求解简单的三角方程。我写它是为了获取三角函数的值。例如,我可以获得 sin(45) 的值,但我无法将 sin(45) 的值赋给术语 sin(45 )。我尝试了 =,=
这是我的代码: # point of intersection between opposite and hypotenuse x,y = pygame.mouse.get_pos() # u
我有一个简单的 C++ 代码,它在一个值 vector 上运行一个默认的 sin 函数。 static void BM_sin() { int data_size = 10000000
有什么区别,如何让 WebGL 的 sin 产生与 Math.sin 相同的结果? 编辑:我的顶点着色器中有一些代码(这不是全部代码),它计算球体周围的斐波那契点,并且应该将顶点放置在这个新点上: a
我有一个客户试图在一个过时的编译器上编译,该编译器似乎没有来自 c++11 的 std::sin 和 std::cos。 (而且他们不能升级)我正在寻找某种快速修复方法来插入标题的顶部以使 std::
#include #include const int TERMS = 7; const float PI = 3.14159265358979; int fact(int n) { r
不幸的是,标准 C++ 库没有对 sincos 的单一调用,这为这个问题提供了空间。 第一个问题: 如果我想计算 sin 和 cos,计算 sin 和 cos 更便宜,还是先计算 sin 再计算 sq
我正在实时渲染 500x500 点。我必须使用 atan() 和 sin() 函数计算点的位置。通过使用 atan() 和 sin(),我得到了 24 fps(每秒帧数)。 float thetaC
我知道 Math.sin() 可以工作,但我需要自己使用 factorial(int) 实现它 我已经在下面有一个阶乘方法是我的 sin 方法,但我无法获得与 Math.sin() 相同的结果: pu
我想知道,当我在 Reddit thread 中发现问题时,为什么 Math.sin(double) 委托(delegate)给 StrictMath.sin(double) .提到的代码片段如下所示
为什么 Pytorch 和 Numpy 的三角函数在以 Pi 的整数倍计算时会导致数量级上如此巨大的差异? >>> torch.sin(torch.ones(1)*2*np.pi) tensor([1
这是一个很简单的问题,让我很困惑。 我收到一个源文件的以下错误,但另一个没有: 4 src/Source2.cpp:1466: error: no matching function for cal
我在 JavaScript 中发现了一个有趣的异常现象。其中重点是我尝试通过预先计算 sin(x) 和 cos(x) 并简单地引用预先计算的值来加速三 Angular 变换计算。 直觉上,预计算比每次
我正在尝试用 Python 对方程 x=a*sin(x) 进行数值求解,其中 a 是某个常数。我已经尝试先用符号求解方程,但似乎这种特殊的表达形式并没有在 sympy 中实现。我也尝试过使用 symp
我在使用 matlab 计算时遇到问题。我知道“pi”是一个 float ,并不精确。因此,在 matlab 中 sin(pi) 不完全为零。我的问题是,如果“pi”不准确,那么为什么 sin(pi/
如何只使用 sin 或 cos 而不是 Math.sin 或 Math.cos?我尝试导入 Math.* 但我想我可能需要对命名空间做一些事情? 最佳答案 import static java.lan
测试代码: #include #include const int N = 4096; const float PI = 3.1415926535897932384626; float cosin
我在其他问题中读到,例如由于浮点表示,sin(2π) 不为零,但非常接近。这个非常小的错误在我的代码中不是问题,因为例如我可以四舍五入 5 位小数。 但是当2π乘以一个非常大的数时,误差就会放大很多。
我是一名优秀的程序员,十分优秀!