- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
程序可以编译,但在运行时我没有从我的输入中得到正确的值。我一直在寻找一种制作正弦公式的方法并找到了这个公式,但我认为它不正确。
我的公式正确吗?我认为仅在 C 中运行 sin
函数也会给我错误的值。
更新:如果我输入 1.5 和 4,我仍然得到这些更改的错误值。我得到 0.000 和一个随机整数
/*
* Function mySin is using a sin formula to get the sin of x
* the function then returns the sin value computed
* Parameters -for function mySin are "x" is the value for sin
* -"n"is the number of series to run
*
*The program also uses the sin(x) function to get the real sin
*
*/
#include <stdio.h>
#include <math.h>
int mySin(double x, int n){
int i=0;
double sinx=0;
for(i=1; i<=n; i+=2){
sinx=(pow(x,i)+sinx); //
}
return sinx;
}
int main(double sinx){
double realSin=0;
double x=0;
int n=0;
printf("Please input x and n:");
scanf("%lf",&x);
scanf("%d",&n);
mySin(x,n);
realSin=sin(x);
printf("mySin= %f\n sin=%d\n",sinx,realSin);
}
最佳答案
您的 mySin
函数至少在 5 个不同的方面是错误的。用**
表示求幂(避免和xor混淆),正确的公式是
sin(x) = x - x**3/3! + x**5/5! - x**7/7! + x**9/9! ...
您的实现失败是因为
此外,main
在其他几个方面是错误的。 mySin
的返回值被完全忽略。相反,sinx
是 main
的第一个参数,它是程序收到的命令行参数的数量(包括它运行的名称)。此外,%d
用于格式字符串中的所有数字,无论类型如何,当它仅用于 int
时。
要修复 mySin
,让 i
仅遍历奇数,并让循环的每次迭代都计算 x**i/i!
并将其添加到 sinx
的当前值中。
要修复 main
,请在 main
中声明一个局部 sinx
变量并赋值 sinx = mySin(x, n)
而不是将 sinx
声明为参数。此外,使用 %lf
通过 scanf
读取 double ,使用 %f
通过 printf
写入 double 。
关于在 C 中创建一个 sin 公式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22522857/
*> 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π乘以一个非常大的数时,误差就会放大很多。
我是一名优秀的程序员,十分优秀!