计算 C++ 中的方程解
我有一个 C++ 函数,具有以下签名:
float Foo(float time, float min, float curr, float beta)
在该函数中,我想确定并返回以下等式中的 MAX:
time = beta + (1.0f - beta) * ((MAX - curr) / (MAX - min))
要测试结果,您可以使用以下参数:
Foo(0.95f, 625, 800, 0.75f)
它应该返回 1500。
在纸上,我有确定 MAX 所需的步骤,但我不知道如何在代码中实现它。如果有人可以提供执行此计算的代码,我将不胜感激。
0.95 = 0.75 + (1 - 0.75) * ((max - 800) / (max - 625))
0.95 = 0.75 + 0.25 * ((max - 800) / (max - 625))
0.95 - 0.75 = 0.25 * ((max - 800) / (max - 625))
0.2 = 0.25 * ((max - 800) / (max - 625))
0.2 / 0.25 = (max - 800) / (max - 625)
0.8 = (max - 800) / (max - 625)
0.8 * (max - 625) = max - 800
(0.8 * max) - (0.8 * 625) = max - 800
(0.8 * max) - 500 = max - 800
((0.8 * max) - max) - 500 = -800
((0.8 * max) - max) = -800 + 500
((0.8 * max) - max) = -300
-0.2 * max = -300
max = -300 / -0.2
max = 1500
I have a function in C++ with the following signature:
float Foo(float time, float min, float curr, float beta)
Within the function, I want to determine and return MAX in the following equation:
time = beta + (1.0f - beta) * ((MAX - curr) / (MAX - min))
To test the results, you could use the following arguments:
Foo(0.95f, 625, 800, 0.75f)
It should return 1500.
On paper, I have the steps needed to determine MAX, but I don't know how to get that working in code. If anyone can provide the code to perform this calculation, I would be extremely grateful.
0.95 = 0.75 + (1 - 0.75) * ((max - 800) / (max - 625))
0.95 = 0.75 + 0.25 * ((max - 800) / (max - 625))
0.95 - 0.75 = 0.25 * ((max - 800) / (max - 625))
0.2 = 0.25 * ((max - 800) / (max - 625))
0.2 / 0.25 = (max - 800) / (max - 625)
0.8 = (max - 800) / (max - 625)
0.8 * (max - 625) = max - 800
(0.8 * max) - (0.8 * 625) = max - 800
(0.8 * max) - 500 = max - 800
((0.8 * max) - max) - 500 = -800
((0.8 * max) - max) = -800 + 500
((0.8 * max) - max) = -300
-0.2 * max = -300
max = -300 / -0.2
max = 1500
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在论文中,等式的每个部分乘以
(MAX - min)
,然后重新组合元素以获得MAX=some
。在编写问题中的 C++ 函数之前,通常需要纸和铅笔。
On the paper multiply each part of the equation on
(MAX - min)
, then regroup elements to getMAX=some
.The paper and the pencil is what you usually need before writing C++ functions like in your question.
让我们用 t 表示时间,用 b 表示 beta,用 c 表示 curr,用 m 表示最小值,用 x 表示 MAX;
我们有
这样的函数,
我还建议不要使用标识符
min
和time
,因为它们可能会导致与std::min< 冲突/code> 和
std::time
let's denote time with t, beta with b, curr with c, min with m, and MAX with x;
we have
so your function will be like this
I would also recommend refraining from using the identifiers
min
andtime
, because they may cause clashes withstd::min
andstd::time