减法越接近其最大分配值 [英] Slowing Addition the Closer It Gets to Its Max Assigned Value

查看:85
本文介绍了减法越接近其最大分配值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,这个问题必须通过一些伪代码来可视化,所以我们开始:

让我说我有2个整数,一个是变量,另一个是常量.

这样说:

int current = 0; static int max = 20

考虑到这一点,我越接近20,我将如何加慢,所以基本上它实际上从未达到20. 这是一个示例:

让我们说加2需要3秒 但添加相同的2,则在前2个之后需要两倍的时间,依此类推.

我将如何去做,如果很难理解,抱歉,但是我尽了最大的努力:(

谢谢,祝你有美好的一天!

解决方案

您可以使用公式

A = Pe rt

其中,A是结果数量; P是起始主体; e是数学上的非理性常数e,它是自然对数的底,通常称为欧拉数,大约为2.718281828459045r是小数点的利率; t以年为单位的时间,通常除以360.

给出0的起始本金,结果将是0,因为0的本金不会产生任何利息.我们可以从.01的名义价值开始,然后将新的本金设置为与先前本金相加的应计利息的结果.

javascript中,可以使用do..while循环生成新的主体,在这里,我们以启动率的一半停止循环,或者.05/2do..while循环中,创建立即调用函数表达式以进行传递当前主体,推送一个在currentcurrent * duration之后返回Promise的函数,其中duration是一个正整数,利用Array.prototype.reduce()调用函数以按顺序返回一个Promise,从而增加了在setTimeout之前按currentcurrent * duration.

您还可以将公式调整为Math.exp(r * (t/n))

A = Pe r(t/n)

其中,n可以是例如360,以便以.05的比率连续从本金1复利,执行do..while循环360次,即一个会计年度;这将在1之间产生更多的总结果,即15%的比率连续复利1年,其中最大可能的结果为1.0512710963760241,应计的总利息为.0512710963760241. /p>

1传递给compound函数将返回1.0512710963760241.

 var current = 0; // principal
var max = 20; // maturity, in years
var rate = .05; // annual rate as decimal
var time = 1; // time in years, alternatively `1/360`
var N = 360; // fiscal year
var arr = []; // store functions which return a `Promise`
var duration = 500;
var output = document.querySelector("output");

function compound(p, r, t) {  
  return ((p||.01) * Math.exp(r * t)); // or `Math.exp(r * (t/N))` 
}

do {
  current = compound(current, rate, time);
   (function(curr) {
    arr.push(
      function() {
        return new Promise(function(resolve) {
          setTimeout(function() {
            output.innerHTML += `current principal: ${curr}<br>`;
            resolve();
          }, curr * duration)
        })
      }
    )
   })(current)
} while (current < max - (rate / 2));

arr.reduce(function(promise, next) {
  return promise.then(next)
}, Promise.resolve())
.then(function() {
   output.innerHTML += `<br>max - current: ${max - current}`; 
}); 

 <output></output> 

Okay, this question is going to have to be visualized with some pseudo-code, so here we go:

Lets say I have 2 integers, one is a varible, the other a constant.

With this say:

int current = 0; static int max = 20

With this in mind, how would I add slower, the closer I get to 20, so basically it never actually reaches 20. Here is an example:

lets say adding 2 takes 3 seconds but adding the same 2, after the previous 2 takes twice as long, and so on.

How would I go about doing this, sorry if its hard to understand, but I tried my best :(

Thanks, and have a great day!

解决方案

You can use the formula

A = Pert

where A is resulting amount; P is starting principal; e is the mathematical irrational constant e, the base of natural logarithms, commonly referred to as Euler's Number, approximately 2.718281828459045; r is rate of interest as a decimal; t is time in years, generally divided by 360.

Given a starting principal of 0 the result would be 0, as no interest can accrue an a principal of 0. We can begin at the nominal value of .01, then set the new principal to the result of the accrued interest added to the previous principal.

In javascript a do..while loop can be used to generate the new principal, here, we stop the loop at half the starting rate, or .05/2 Within do..while loop create an immediately invoke function expression to pass current principal, push a function which returns a Promise after current or current * duration, where duration is a positive integer, utilize Array.prototype.reduce() to call function which returns a Promise in sequential order, increasing duration of next setTimeout by current or current * duration.

You can also adjust formula to Math.exp(r * (t/n)) or

A = Per(t/n)

where n could be, for example, 360, to compound interest continuously from principal 1 at rate of .05, execute do..while loop 360 times, a fiscal year; which would produce a greater amount of total results between 1, that is 1 continuously compounded for 1 year at rate of 5% where the maximum possible result would be 1.0512710963760241, total interest accrued being .0512710963760241.

Passing 1 to compound function returns 1.0512710963760241.

var current = 0; // principal
var max = 20; // maturity, in years
var rate = .05; // annual rate as decimal
var time = 1; // time in years, alternatively `1/360`
var N = 360; // fiscal year
var arr = []; // store functions which return a `Promise`
var duration = 500;
var output = document.querySelector("output");

function compound(p, r, t) {  
  return ((p||.01) * Math.exp(r * t)); // or `Math.exp(r * (t/N))` 
}

do {
  current = compound(current, rate, time);
   (function(curr) {
    arr.push(
      function() {
        return new Promise(function(resolve) {
          setTimeout(function() {
            output.innerHTML += `current principal: ${curr}<br>`;
            resolve();
          }, curr * duration)
        })
      }
    )
   })(current)
} while (current < max - (rate / 2));

arr.reduce(function(promise, next) {
  return promise.then(next)
}, Promise.resolve())
.then(function() {
   output.innerHTML += `<br>max - current: ${max - current}`; 
});

<output></output>

这篇关于减法越接近其最大分配值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆