非线性计数器

发布于 2024-08-24 15:39:41 字数 658 浏览 8 评论 0原文

所以我有一个柜台。它应该计算某物的当前数量。为了计算这个,我知道开始日期、开始金额以及每秒递增计数器的金额。简单易行。棘手的部分是增长并不完全是线性的。每天,增量都会增加一定的量。我需要通过算法重新创建此值 - 基本上根据起始值、随时间增加的量以及随时间增加的增量计算出当前日期的确切值。

我的目标语言是 Javascript,但伪代码也很好。

基于 AB 的解决方案:

var now = new Date();

var startDate1 = new Date("January 1 2010");
var days1 = (now - startDate1) / 1000 / 60 / 60 / 24;
var startNumber1 = 9344747520;
var startIncrement1 = 463;
var dailyIncrementAdjustment1 = .506;
var currentIncrement = startIncrement1 + (dailyIncrementAdjustment1 * days1);

startNumber1 = startNumber1 + (days1 / 2) * (2 * startIncrement1 + (days1 - 1) * dailyIncrementAdjustment1);

你们觉得这合理吗?

So I have a counter. It is supposed to calculate the current amount of something. To calculate this, I know the start date, and start amount, and the amount to increment the counter by each second. Easy peasy. The tricky part is that the growth is not quite linear. Every day, the increment amount increases by a set amount. I need to recreate this algorithmically - basically figure out the exact value at the current date based on the starting value, the amount incremented over time, and the amount the increment has increased over time.

My target language is Javascript, but pseudocode is fine too.

Based on AB's solution:

var now = new Date();

var startDate1 = new Date("January 1 2010");
var days1 = (now - startDate1) / 1000 / 60 / 60 / 24;
var startNumber1 = 9344747520;
var startIncrement1 = 463;
var dailyIncrementAdjustment1 = .506;
var currentIncrement = startIncrement1 + (dailyIncrementAdjustment1 * days1);

startNumber1 = startNumber1 + (days1 / 2) * (2 * startIncrement1 + (days1 - 1) * dailyIncrementAdjustment1);

Does that look reasonable to you guys?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

热情消退 2024-08-31 15:39:41

这是一个二次函数。如果t是经过的时间,那么通常是2+bt+c,你可以通过以下方式算出a,b,c替换前 3 秒的结果。

或者:使用算术级数求和的公式,其中a1是初始增量,d 是您所指的“设定量”。只是不要忘记将您的“起始金额”添加到公式为您提供的金额中。

如果x0是初始量,d是初始增量,e是增加增量的“设定量”,则
x0 + (t/2)*(2d + (t-1)*e)

It's a quadratic function. If t is the time passed, then it's the usual at2+bt+c, and you can figure out a,b,c by substituting the results for the first 3 seconds.

Or: use the formula for the arithmetic progression sum, where a1 is the initial increment, and d is the "set amount" you refer to. Just don't forget to add your "start amount" to what the formula gives you.

If x0 is the initial amount, d is the initial increment, and e is the "set amount" to increase the incerement, it comes to
x0 + (t/2)*(2d + (t-1)*e)

ˉ厌 2024-08-31 15:39:41

如果我正确理解你的问题,你有一个初始值x_0,每秒初始增量为d_0,每天的增量调整为e 。也就是说,第一天每秒的增量为 d_0,第二天每秒的增量为 d_0 + e,依此类推。

然后,我们注意到每秒的增量在时间 t 处,

d(t) = d_0 + floor(t / S) * e

其中 S 是每天的秒数,t 是自 t = 以来经过的秒数t_0。然后

x = x_0 + sum_{k < floor(t / S)} S * d(k) + S * (t / S - floor(t / S)) * d(t)

就是您正在寻找的公式。从这里,您可以将其简化为

x = x_0 + S * floor(t / S) d_0 + S * e * (floor(t / S) - 1) * floor(t / S) / 2.

If I understand your question correctly, you have an initial value x_0, an initial increment per second of d_0 and an increment adjustment of e per day. That is, on day one the increment per second is d_0, on day two the increment per second is d_0 + e, etc.

Then, we note that the increment per second at time t is

d(t) = d_0 + floor(t / S) * e

where S is the number of seconds per day and t is the number of seconds that have elapsed since t = t_0. Then

x = x_0 + sum_{k < floor(t / S)} S * d(k) + S * (t / S - floor(t / S)) * d(t)

is the formula that you are seeking. From here, you can simplify this to

x = x_0 + S * floor(t / S) d_0 + S * e * (floor(t / S) - 1) * floor(t / S) / 2.
王权女流氓 2024-08-31 15:39:41
use strict; use warnings;

my $start = 0;
my $stop = 100;
my $current = $start;

for my $day ( 1 ..  100 ) {
    $current += ($day / 10);
    last unless $current < $stop;
    printf "Day: %d\tLeft %.2f\n", $day, (1 - $current/$stop);
}

输出:

Day: 1  Left 1.00
Day: 2  Left 1.00
Day: 3  Left 0.99
Day: 4  Left 0.99
Day: 5  Left 0.98
...
Day: 42 Left 0.10
Day: 43 Left 0.05
Day: 44 Left 0.01
use strict; use warnings;

my $start = 0;
my $stop = 100;
my $current = $start;

for my $day ( 1 ..  100 ) {
    $current += ($day / 10);
    last unless $current < $stop;
    printf "Day: %d\tLeft %.2f\n", $day, (1 - $current/$stop);
}

Output:

Day: 1  Left 1.00
Day: 2  Left 1.00
Day: 3  Left 0.99
Day: 4  Left 0.99
Day: 5  Left 0.98
...
Day: 42 Left 0.10
Day: 43 Left 0.05
Day: 44 Left 0.01
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文