非线性计数器
所以我有一个柜台。它应该计算某物的当前数量。为了计算这个,我知道开始日期、开始金额以及每秒递增计数器的金额。简单易行。棘手的部分是增长并不完全是线性的。每天,增量都会增加一定的量。我需要通过算法重新创建此值 - 基本上根据起始值、随时间增加的量以及随时间增加的增量计算出当前日期的确切值。
我的目标语言是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个二次函数。如果
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 outa,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, andd
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)
如果我正确理解你的问题,你有一个初始值
x_0
,每秒初始增量为d_0
,每天的增量调整为e
。也就是说,第一天每秒的增量为d_0
,第二天每秒的增量为d_0 + e
,依此类推。然后,我们注意到每秒的增量在时间
t
处,其中
S
是每天的秒数,t
是自t = 以来经过的秒数t_0
。然后就是您正在寻找的公式。从这里,您可以将其简化为
If I understand your question correctly, you have an initial value
x_0
, an initial increment per second ofd_0
and an increment adjustment ofe
per day. That is, on day one the increment per second isd_0
, on day two the increment per second isd_0 + e
, etc.Then, we note that the increment per second at time
t
iswhere
S
is the number of seconds per day andt
is the number of seconds that have elapsed sincet = t_0
. Thenis the formula that you are seeking. From here, you can simplify this to
输出:
Output: