计算 C++ 中的方程解

发布于 2024-10-02 17:15:26 字数 937 浏览 1 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寻梦旅人 2024-10-09 17:15:26

在论文中,等式的每个部分乘以 (MAX - min),然后重新组合元素以获得 MAX=some
在编写问题中的 C++ 函数之前,通常需要纸和铅笔。

On the paper multiply each part of the equation on (MAX - min), then regroup elements to get MAX=some.
The paper and the pencil is what you usually need before writing C++ functions like in your question.

溺渁∝ 2024-10-09 17:15:26
time = beta + (1.0f - beta) * ((MAX - curr) / (MAX - min))

让我们用 t 表示时间,用 b 表示 beta,用 c 表示 curr,用 m 表示最小值,用 x 表示 MAX;
我们有

t = b + (1-b)(x-c)/(x-m)
(x-c)/(x-m) = (t-b)/(1-b)
(x-m)(t-b) = (x-c)(1-b)
x(t-b) - x(1-b) = m(t-b) - c(1-b)
x(t-1) = m(t-b) - c(1-b)
x = (m(t-b) - c(1-b))/(t-1)

这样的函数,

float Foo(float time, float min, float curr, float beta)
{
   return (min*(time-beta) - curr*(1-beta))/(time-1);
}

我还建议不要使用标识符 mintime,因为它们可能会导致与 std::min< 冲突/code> 和 std::time

time = beta + (1.0f - beta) * ((MAX - curr) / (MAX - min))

let's denote time with t, beta with b, curr with c, min with m, and MAX with x;
we have

t = b + (1-b)(x-c)/(x-m)
(x-c)/(x-m) = (t-b)/(1-b)
(x-m)(t-b) = (x-c)(1-b)
x(t-b) - x(1-b) = m(t-b) - c(1-b)
x(t-1) = m(t-b) - c(1-b)
x = (m(t-b) - c(1-b))/(t-1)

so your function will be like this

float Foo(float time, float min, float curr, float beta)
{
   return (min*(time-beta) - curr*(1-beta))/(time-1);
}

I would also recommend refraining from using the identifiers min and time, because they may cause clashes with std::min and std::time

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文