- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
在我学习 C 的过程中,我遇到了一个给我带来一些问题的任务。我需要对公式 n! 的近似值做一个等式,可以描述为:
n! = n^n*e^(-n)*sqrt(2(2*n+1/3)*PI),但是我根本无法让我的值与实际值相符。 5! = 120ish
我可以得到大约 148ish 的值
无法找出我的代码哪里错了:
#include <stdio.h>
#include <math.h>
#define PI 3.14156
#define E_CONST 2.7828
int main ()
{
double num;
double calc, first, second, third, fourth;
printf("Give an int: ");
scanf("%lf", &num);
first = pow(num , num);
second = pow(E_CONST, -num);
third = (2 * num + 1/3);
fourth = sqrt(2*third*PI);
//calc = first * second * fourth;
calc = pow(num, num) * pow(E_CONST, -num) * sqrt(2*(2*num+(1/3))*PI);
printf("Input: %f", num);
printf("1: %.2f\n2: %.10f\n3: %.8f\n4: %.2f\n", first, second, third, fourth);
printf("\nInt was: %.2f\n\nApproximate number: %.5f", num, calc);
return 0;
}
感觉我什么都试过了。代码有点乱,但这是因为我现在已经乱七八糟了。
最佳答案
3.14156
是 PI
的错误值:最好使用 3.1416
,或 3.14159
,或 4 * atan(1)
,或者,对于 POSIX 实现,M_PI
。
2.7828
是 e
的非常差值:最好使用 2.7183
或 exp(1)
,或者,对于 POSIX 实现,M_E
。
1/3
为整数除法,结果为0:最好使用1.0/3
。
您的近似值也不正确。 correct approximation是
n^n * e^(-n) * sqrt((2*n+1/3)*PI)
关于c - 解决 n!用C(某处方程错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7359935/
在我的一门类(class)中,我接到了一项家庭作业,要求我们在谷歌上搜索 Metapost 语言并找到该语言中方程求解功能的用途。 在浏览了 Metapost 用户手册的前十多页后,我发现只有一个原因
你能帮我在 this page 上的谷歌图表上隐藏趋势线上的工具提示(方程)吗? ? 谢谢 以下是我正在使用的图表选项: var options = { title: 'Weight
我正在尝试将 TeXWorks 编辑器配置为使用与 TeXMaker 相同的语法着色。但是,TexWorks 使用正则表达式来指定应该着色的内容。不幸的是,它没有数学的默认设置。 我想匹配 $ 之间的
我刚开始玩 GHCi。我看到列表生成器基本上解决了给定集合中的方程式: Prelude> [x | x [0.01,0.2..2.0] [1.0e-2,0.2,0.39,0.580000000000
是否有可以使用的图形表达式生成器或方程编辑器的 Java 开源实现? 最好有在线演示,或者至少有屏幕截图。 最佳答案 取决于方程的类型。 如果您正在考虑简单的多项式,您可以尝试 Java Expres
我有四个文本输入字段,在用户输入相关值后,我必须进行 JavaScript 计算以将它们全部相加 我使用: var total = Number(value1) + Number(value2) +
为什么这段代码有两个不同的输出(GCC 4.5.1)(我已经评论了重要的行): int main() { bool a = 1; bool b = 1; bool c = 1;
如果标题含糊不清,我深表歉意,但我不知道如何为我的情况命名。我正在为使用 GPS 的 iPhone 编写一个应用程序。在 didUpdateLocations: 方法中,我针对任意大小的变量测试位置的
我正在尝试计算表中学生的 BMI,四舍五入到三位数: +-------+--------+--------+ | fname | weight | height | +-------+--------
我们可以使用 deSolve R 中的常微分方程 (ODE) 包,但是,我找不到解决两个嵌套 ODE 方程的方法,假设` b'(t) = beta - k*b(t); a'(t) = alpha -b
我有一个 boolean 方程,想简化它。帮忙解决一下。 bool needLoad = isA || (!isA && !isB); 之后我使用 if (needLoad){ if (
我很感兴趣,建模工具(在我的例子中是 OpenModelica 和 Dymola - 建模语言 Modelica)如何求解方程组(线性和/或非线性)。这些工具专为求解微分代数方程而设计。我知道一点将微
Julia:当我有绘图时如何找到最佳拟合曲线/方程?我有一个用 map 绘制的图,但我需要找到一个适合它的二次方程? 最佳答案 正如评论中所说,有一个情节在这里并不真正相关;只有数据本身是。您可以使用
我一直在尝试将像 100, 45 这样的输入放入文本框中,并通过单击按钮通过我的方程式运行它,但我不知道该怎么做。单击按钮后,它应该发布答案作为警报。请帮忙。谢谢。 function Rad(a, b
Julia:当我有绘图时如何找到最佳拟合曲线/方程?我有一个用 map 绘制的图,但我需要找到一个适合它的二次方程? 最佳答案 正如评论中所说,有一个情节在这里并不真正相关;只有数据本身是。您可以使用
有人可以向我解释为什么下面的代码会打印字符“u”吗? int p = 9; int q = 5; int r = p - q; double x = p; double y = q; St
我想以某种方式缩短我的 ODE 方程,因为否则代码会变得困惑。我尝试过使用辅助函数,例如这里的 fe() ,但这不起作用。下面的代码只是一个例子,欢迎任何建议!谢谢! # Import the req
我无法创建正确的文件。程序中的方程不会迭代,它只会根据请求的数量写入相同的总和。 for 循环。 #include #include #include #define LEN 256 int m
我有 2 个指向一些 Point 结构的指针。我想计算两点之间的距离(我不需要计算它的根)所以我有这个: w[0]=X[l]; w[1]=X[l+1]; d=m(w[0]->x
我有一个具有 CSV 上传功能的网站,它将 CSV 中的所有内容推送到临时表,然后分成较小的表。 目前,我有一个显示页面,在 HTML 表格中显示所有这些信息。然而,有些部分需要有公式化的表示。换句话
我是一名优秀的程序员,十分优秀!